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

查看:35
本文介绍了如何从python脚本安装npm包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 python 脚本安装 npm 包?

当我使用 subprocess.Popen(["node", "app.js"])OK.
当我使用 subprocess.Popen(["npm", "install", "open"])抛出错误.

抱歉,Google 和 DuckDuckGo 今天不是我的朋友(

主要问题 — 我的小型实用程序需要自动本地安装包,因为全局包在 Windows 中不起作用.

PS.我不得不问这个问题,因为我正在尝试为 Sublime Text 2 开发一个插件.

这是 Sublime python 控制台中的错误:

重新加载插件...\stsync.py回溯(最近一次调用最后一次):文件.\sublime_plugin.py",第 103 行,在 create_application_commands 中cmds.append(class_())文件.\stsync.py",第 16 行,在 __init__ 中文件.\subprocess.py",第 633 行,在 __init__ 中文件.\subprocess.py",第 842 行,在 _execute_child 中Windows 错误:[错误 2]

第 16 行:subprocess.Popen(["node", "npm", "install", "open"])

<小时>

如果我将第 16 行更改为 subprocess.Popen(["node", "npm", "install", "open"])然后python脚本将成功调用nodejs终端,但随后将失败并显示错误:
找不到 npm 模块

解决方案

shell 参数设置为 True

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

How to install npm package from a 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 have to ask this question because I’m trying to develop a 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 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:
cannot find npm module

解决方案

set the shell argument to True

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

这篇关于如何从python脚本安装npm包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆