[英]subprocess.Popen raise child Exception
我在python文件中添加了一個操作:
subprocess.Popen(['notify-send', message])
並且終端上的錯誤是:
subprocess.Popen(['notify-send', message])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__ errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
如何防止這個錯誤?
Popen使用默認的無環境,因此不使用PATH。 有幾種解決方案:
subprocess.Popen(args=['notify-send', message], env={'PATH': os.getenv('PATH')}
進程中使用環境subprocess.Popen(args=['notify-send', message], env={'PATH': os.getenv('PATH')}
notify-send
subprocess.Popen(['/full/path/here/notify-send', message])
根據設計,默認情況下,啟動其他程序的程序不使用PATH環境變量。 過去,這是通過更改PATH或通過在正常PATH中安裝比真實孩子更高優先級的惡意程序來破壞合法程序的常見攻擊方式。 結果,可以代表用戶執行惡意程序,而無需他/她采取任何異常動作。
有成千上萬種強制使用PATH的方法,對於簡單的操作來說很好(參數shell = TRUE
是其中之一)。 但是對於更嚴重的腳本,它的使用效果與使用皺眉system
而不是使用C或C ++語言的fork
+ exec
。
TL / DR:最正確的方法是傳遞子程序的完整PATH。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.