我通過python腳本啓動npm命令時出現問題。subprocess.run失敗vs subprocess.check_call成功
從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)
正如我previousely用於subprocess.run啓動外部應用程序(紐曼)成功,我嘗試以下:
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
爲什麼我不能使用subpr任何想法ocess.run? (也不check_output,順便......)
從錯誤消息中,你忘記在'subprocess.run()'中添加'shell = True',因此完整的字符串''start npm run features:chrome:server''被假定爲目標可執行文件。我認爲這是安全的假設該文件不存在... – dhke
好點,我沒有設置shell = True,讓我們試試吧...!太好了!有用 ! :) 謝謝。 –
'shell = True'分割參數字符串(通過傳遞給shell)。 'shell = False'嘗試運行一個名爲''start npm run features:chrome:server''的可執行文件(帶有空格的完整字符串,而不僅僅是'start.exe')。 – dhke