[英]Running npm script from Python script. Getting inconsistent behavior
I have a directory containing some python scripts.我有一个包含一些 python 脚本的目录。 In this directory there are subdirectories in the form of 'a/b/c1', 'a/b/c2', ..., 'a/b/cn', etc. Each of these directories is an npm package with a script in the
package.json
file called my_script
.在这个目录下有'a/b/c1', 'a/b/c2', ..., 'a/b/cn'等形式的子目录。每个目录都是一个npm package
package.json
文件中名为my_script
的脚本。
From the directory with my python scripts I can run the following command from the terminal fine:从带有我的 python 脚本的目录中,我可以从终端正常运行以下命令:
npm run my_script --prefix a/b/c1
However in my python script I doing something along the lines the following:但是,在我的 python 脚本中,我按照以下方式进行操作:
bashCmd = ['npm', 'run', 'my_script', '--prefix', 'a/b/c1']
process = subprocess.Popen(bashCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, error = process.communicate()
print(output)
print(error)
The result of output
is the message from npm telling you instructions on arguments to pass to it. output 的结果是来自
output
的消息,告诉您 arguments 上的指令传递给它。
Usage: npm <command>
where <command> is one of:
access, adduser, audit, bin, bugs, c, cache, ci, cit,...
From what I can tell, I am running the same set of instructions, one from Python, and one from npm, but getting different results.据我所知,我正在运行同一组指令,一个来自 Python,一个来自 npm,但得到的结果不同。
Can anybody help me shed some light on this?有人可以帮我解释一下吗?
It seems I might have been looking at some older documentation or plain misinterpreted what I was reading for subprocess.Popen()
.看来我可能一直在查看一些较旧的文档,或者完全误解了我为
subprocess.Popen()
阅读的内容。 I was under the impression the input passed to this function was supposed to be a list of strings where each string would normally be separated by a space if you were to type the command directly into the terminal.我的印象是传递给这个 function 的输入应该是一个字符串列表,如果您要直接在终端中键入命令,每个字符串通常用空格分隔。 If you just take the normal command and pass this as input like below, the program executes correctly.
如果您只是使用普通命令并将其作为输入传递,如下所示,程序将正确执行。
bashCmd = ['npm run my_script --prefix a/b/c1']
process = subprocess.Popen(bashCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, error = process.communicate()
print(output)
print(error)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.