使用 Python 子进程运行 npm 命令 [英] Run npm commands using Python subprocess

查看:103
本文介绍了使用 Python 子进程运行 npm 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 YUIDOC 自动生成文档,但我有一个大量使用 python 的服务器端框架,因此我正在尝试从 python 脚本中自动执行所有操作.我能够让 node 命令正常运行,但是每当我尝试使用 npm 安装的东西时,python 都不高兴.我的项目使用 Buildout 而不是 virtualenv,但理想情况下,我希望能够从独立的 Python 文件中运行这些命令.

I'm trying to automate the generation of documentation using YUIDOC, but I have a server side framework that heavily uses python, so I'm trying to automate everything from within a python script. I'm able to get the node command to run fine, but whenever I try something I installed using npm, python isn't happy. My project uses Buildout instead of virtualenv, but ideally I'd like to be able to just run these commands from a standalone python file.

也许一些代码可以帮助解释我的情况:

Perhaps some code would help explain my situation:

import subprocess
subprocess.check_call('node --help')

#SUCCESS

import subprocess
subprocess.check_call('npm --help')

#FAIL
#WindowsError: [Error 2] The system cannot find the file specified

import subprocess
subprocess.check_call('yuidoc --help')

#FAIL
#WindowsError: [Error 2] The system cannot find the file specified

我已经尝试将 yuidoc 和 npm 内容所在的文件夹添加到 python 的 sys.path 中,但这不起作用.

I already tried adding the folder where the yuidoc and npm stuff lives to the sys.path of python, but that didn't work.

ps,这与这个问题类似.

推荐答案

我需要在 check_call 中指定 shell=True.

I needed to specify shell=True in the check_call.

subprocess.check_call('npm --help', shell=True)

subprocess.check_call('yuidoc --help', shell=True)

这篇关于使用 Python 子进程运行 npm 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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