Popen 错误:[Errno 2] 没有这样的文件或目录

2022-01-18 00:00:00 python subprocess popen

问题描述

我有一些自定义命令.

# works 
subprocess.Popen(['python'], stdout=subprocess.PIPE)

但如果我有自己的系统命令,例如 deactivate,我会收到该错误

But if I have my own system commands like deactivate, I get that error

Traceback (most recent call last):
  File "runner2.py", line 21, in <module>
    main() 
  File "runner2.py", line 18, in main
    subprocess.Popen(['deactivate',], stdout=subprocess.PIPE)
  File "/usr/lib/python2.6/subprocess.py", line 633, in __init__
    errread, errwrite)
  File "/usr/lib/python2.6/subprocess.py", line 1139, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

更不用说我需要在我的沙盒 virtualenv 下执行这个了.

Let alone I need to execute this under my sandbox virtualenv.


解决方案

尝试在 Popen 调用中添加一个额外的参数 shell=True.

Try add an extra parameter shell=True to the Popen call.

相关文章