subprocess.run failure vs subprocess.check_call success

时间:2017-09-13 13:08:09

标签: python node.js subprocess

我在通过python脚本启动npm命令时遇到问题。

来自Run npm commands using Python subprocess我发现以下内容应该有效:

subprocess.check_call('start npm run features:chrome:server', shell=True)

它确实(!)。

从文档(https://docs.python.org/3/library/subprocess.html)中,我读到subprocess.check_call相当于run(..., check=True)

由于我以前使用subprocess.run成功启动外部应用程序(newman),我尝试了以下内容:

subprocess.run("start npm run features:chrome:server", check=True)

但最终会出现错误:

Traceback (most recent call last):
  File "test_differentiel.py", line 73, in <module>
    subprocess.run("start npm run features:chrome:server")
  File "C:\Users\a.joly\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 403, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\Users\a.joly\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 707, in __init__
    restore_signals, start_new_session)
  File "C:\Users\a.joly\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 990, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] Le fichier spécifié est introuvable

为什么我不能使用subprocess.run? (顺便说一句,也不是check_output ......)

1 个答案:

答案 0 :(得分:0)

正确的执行是 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="app"></div>

subprocess.run(['start', 'npm', 'run', 'features:chrome:server'], check=True)因为您调用了shell而起作用。您可以找到here的更多信息。

subprocess.check_call('start npm run features:chrome:server', shell=True)也应该有用。