How to install npm package from python script?
When I use subprocess.Popen(["node", "app.js"])
it is OK.
When I use subprocess.Popen(["npm", "install", "open"])
it is throwing an error.
Sorry, but Google and DuckDuckGo are not my friends today(
The main problem — automatic local installation required packages for my small utility, because global packages are not working in windows.
PS. I'm have to ask this question because I’m trying to develop plugin for Sublime Text 2.
This is the error in Sublime python console:
Reloading plugin …\stsync.py
Traceback (most recent call last):
File ".\sublime_plugin.py", line 103, in create_application_commands
cmds.append(class_())
File ".\stsync.py", line 16, in __init__
File ".\subprocess.py", line 633, in __init__
File ".\subprocess.py", line 842, in _execute_child
WindowsError: [Error 2]
line 16: subprocess.Popen(["node", "npm", "install", "open"])
If I will change 16 line to subprocess.Popen(["node", "npm", "install", "open"])
then python script will successfully invoke nodejs terminal, but then nodejs terminal will fail with error:
cannot find npm module
On Windows, many of the Node.js "binaries" are actually suffixed with the
.cmd
filename extension, which for whatever reason during the invocation throughsubprocess.Popen
, it doesn't expand (even thoughPATHEXT
might contain.cmd
).So for a proper solution (that does not use
shell=True
), try append.cmd
to the Node.js binaries needed:Of course error because I don't have a
package.json
in that directory. Try again using some other commonly used programs, such aswebpack
:Oh right, add that
.cmd
:set the
shell
argument to True