Run npm commands using Python subprocess

被刻印的时光 ゝ 提交于 2020-01-03 07:18:11

问题


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

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, this is sort of a similar question to this question.


回答1:


I needed to specify shell=True in the check_call.

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

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


来源:https://stackoverflow.com/questions/28891053/run-npm-commands-using-python-subprocess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!