简体   繁体   English

如何从python脚本安装npm包?

[英]How to install npm package from python script?

How to install npm package from a python script? 如何从python脚本安装npm包?

When I use subprocess.Popen(["node", "app.js"]) it is OK . 当我使用 subprocess.Popen(["node", "app.js"]) ,就可以了
When I use subprocess.Popen(["npm", "install", "open"]) it is throwing an error . 当我使用 subprocess.Popen(["npm", "install", "open"]) ,会抛出错误

Sorry, but Google and DuckDuckGo are not my friends today( 抱歉,但是Google和DuckDuckGo今天不是我的朋友(

The main problem — automatic local installation required packages for my small utility because global packages are not working in windows. 主要问题 -自动本地安装需要我的小型实用程序使用的软件包,因为全局软件包在Windows中不起作用。

PS. PS。 I have to ask this question because I'm trying to develop a plugin for Sublime Text 2. 我不得不问这个问题,因为我正在尝试为Sublime Text 2开发一个插件。

This is the error in Sublime python console : 这是Sublime python控制台中错误

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"]) 第16行: subprocess.Popen(["node", "npm", "install", "open"])


If I change line 16 to subprocess.Popen(["node", "npm", "install", "open"]) then the python script will successfully invoke the nodejs terminal, but then it will fail with error: 如果我将第16行更改subprocess.Popen([“ node”,“ npm”,“ install”,“ open”]),则python脚本将成功调用nodejs终端,但随后将失败并显示错误:
cannot find npm module
nodejs错误

shell参数设置为True

subprocess.Popen(["node", "npm", "install", "open"], shell=True)

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 ). 在Windows上,许多Node.js“二进制文件”实际上都带有.cmd文件扩展名后缀,无论通过subprocess.Popen调用时是出于什么原因,它都不会扩展(即使PATHEXT可能包含.cmd )。

So for a proper solution (that does not use shell=True ), try append .cmd to the Node.js binaries needed: 因此,对于适当的解决方案(不使用shell=True ),请尝试将.cmd附加到所需的Node.js二进制文件中:

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'])
<subprocess.Popen object at 0x005E18B0>
>>> npm ERR! install Couldn't read dependencies

Of course it throws an error because I don't have a package.json in that directory. 当然会抛出错误,因为该目录中没有package.json Try again using some other commonly used programs, such as webpack : 使用其他一些常用程序,例如webpack ,再试一次:

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

Oh right, add that .cmd : 哦,对,加上.cmd

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM