子进程打开('source venv/bin/activate'),没有这样的文件?

subprocess open ('source venv/bin/activate'),no such file?

我想进入 python 中的虚拟环境 files.But 它不会引发此类文件。

import subprocess 
subprocess.Popen(['source', '/Users/XX/Desktop/mio/worker/venv/bin/activate'])

Traceback (most recent call last): File "/Users/Ru/Desktop/mio/worker/run.py", line 3, in subprocess.Popen(['source', '/Users/Ru/Desktop/mio/worker/venv/bin/activate'])

File"/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in init errread, errwrite)

File"/usr/local/Cellar/python/2.7.11/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

我认为您的代码不起作用,因为您将 'source' 命令与 virtualenv 路径参数、documentation:

分开

"Note in particular that options (such as -input) and arguments (such as eggs.txt) that are separated by whitespace in the shell go in separate list elements, while arguments that need quoting or backslash escaping when used in the shell (such as filenames containing spaces or the echo command shown above) are single list elements."

您应该尝试以下两种方法之一: 首先,将源和 virtualenv 文件路径写为单个字符串参数:

import subprocess 
subprocess.Popen(['source '/Users/XX/Desktop/mio/worker/venv/bin/activate'])

我正在研究 OSX,但这似乎不起作用,但这可能是由于您使用的 shell。为确保这有效,您可以使用 shell=True 标志:

import subprocess
subprocess.Popen(['source '/Users/XX/Desktop/mio/worker/venv/bin/activate'],shell=True)

这将默认使用 /bin/sh shell。同样,您可以在文档中阅读更多内容。

汤姆.

还有另一种更简单的方法来做你想做的事。 如果你想让 python 脚本使用 virtualenv,你总是可以使用来自 virualenv 本身的 python 解释器。

/Users/Ru/Desktop/mio/worker/venv/bin/python my_python_file.py

这将 运行 my_python_file.py 与 virtualenv 的 properties/libraries。

如果您想 运行 子进程中的那个文件,您可以执行类似于我上面描述的方法的操作:

import subprocess 
subprocess.Popen(['/Users/Ru/Desktop/mio/worker/venv/bin/python my_python_file.py])

并让 my_python_file.py 导入鼠兔并执行您希望执行的其他操作。

汤姆.