2017-03-12 26 views
5

Tôi đã triển khai trình khách web trăn mà tôi muốn kiểm tra.Cách setup.py install npm module?

Máy chủ được lưu trữ trong đăng ký npm. Máy chủ được chạy cục bộ với nút trước khi chạy thử nghiệm chức năng của tôi.

Tôi làm cách nào để cài đặt đúng mô-đun npm từ tập lệnh setup.py?

Dưới đây là giải pháp hiện tại của tôi lấy cảm hứng từ post này:

class CustomInstallCommand(install): 
    def run(self): 
     arguments = [ 
      'npm', 
      'install', 
      '--prefix', 
      'test/functional', 
      'promisify' 
     ] 
     subprocess.call(arguments, shell=True) 
     install.run(self) 

setup(
    cmdclass={'install': CustomInstallCommand}, 

Trả lời

5
from setuptools.command.build_py import build_py 

class NPMInstall(build_py): 
    def run(self): 
     self.run_command('npm install --prefix test/functional promisify') 
     build_py.run(self) 

HOẶC

from distutils.command.build import build 

class NPMInstall(build): 
    def run(self): 
     self.run_command("npm install --prefix test/functional promisify") 
     build.run(self) 

cuối cùng:

setuptools.setup(
    cmdclass={ 
     'npm_install': NPMInstall 
    }, 
    # Usual setup() args. 
    # ... 
) 

Ngoài ra nhìn here

1

Bạn đang rất gần, đây là một chức năng đơn giản mà không chỉ vậy, bạn có thể loại bỏ "--global" lựa chọn là bạn muốn cài đặt gói cho các dự án hiện tại mà thôi, hãy ghi nhớ các lệnh shell = True thể trình bày an ninh rủi ro

import subprocess 
def npm_install(args=["npm","--global", "install", "search-index"]) 
    subprocess.Popen(args, shell=True) 
Các vấn đề liên quan