简体   繁体   English

从 Python 脚本运行 npm 脚本。 获得不一致的行为

[英]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.

相关问题 glob文件,用作python脚本中python脚本的输入。 - glob files to use as input for a python script from a python script. Python:对脚本运行命令行应用程序。 在文件中发送参数 - Python: Running command line application to the script. Sending parameters in files 在一个脚本中运行两个版本的Python。 可能? - Running two versions of Python in one script. Possible? 使用Python ssh然后运行bash脚本。 我需要在屏幕上显示正在运行的bash脚本的输出 - Using Python to ssh then run bash script. The output of the running bash script i need to display on the screen 用python脚本激活root用户。 (OSX) - Activating the root user with a python script. (OSX) 基本的Python加法脚本问题。 - Problems with a basic Python addition script. 从ajax调用python脚本得到:来自脚本的格式错误的标题。 标头不好 - calling python script from ajax got: malformed header from script. Bad header AWS Lambda Python 脚本。 如何返回对象并保持 scipt 在后台运行? - AWS Lambda Python script. How do I return an object and keep the scipt running in the background? 在 Python 上运行 AppleScript:“语法错误:预期行尾,但发现脚本结束。” - Running AppleScript on Python : “Syntax Error: Expected end of line but found end of script.” 在 SQL Server 中运行 xp_cmdshell 以运行 python 脚本。 Xlwings 部分抛出“访问被拒绝”错误 - Running xp_cmdshell in SQL Server to run python script. Xlwings portion throws 'Access is denied' error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM