How to install npm package from python script?

前端 未结 2 512
野趣味
野趣味 2021-01-18 00:28

How to install npm package from a python script?

When I use subprocess.Popen([\"node\", \"app.js\"]) it is OK.

2条回答
  •  太阳男子
    2021-01-18 00:41

    On Windows, many of the Node.js "binaries" are actually suffixed with the .cmd filename extension, which for whatever reason during the invocation through subprocess.Popen, it doesn't expand (even though PATHEXT might contain .cmd).

    So for a proper solution (that does not use shell=True), try append .cmd to the Node.js binaries needed:

    Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (In
    tel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import subprocess
    >>> subprocess.Popen(['npm.cmd', 'install'])
    
    >>> npm ERR! install Couldn't read dependencies
    

    Of course it throws an error because I don't have a package.json in that directory. Try again using some other commonly used programs, such as webpack:

    >>> subprocess.Popen(['webpack'])
    Traceback (most recent call last):
      File "", line 1, in 
    ...
    FileNotFoundError: [WinError 2] The system cannot find the file specified
    

    Oh right, add that .cmd:

    >>> subprocess.Popen(['webpack.cmd'])
    
    >>> No configuration file found and no output filename configured via CLI option
    

提交回复
热议问题