我可以使用单个 python 脚本来创建 virtualenv 并安装 requirements.txt 吗?
问题描述
我正在尝试创建一个脚本,如果尚未创建 virtualenv,我将在其中创建它,然后在其中安装 requirements.txt.
I am trying to create a script where i create a virtualenv if it has not been made, and then install requirements.txt in it.
我无法调用正常源/env/bin/activate并激活它,然后使用pip安装requirements.txt.有没有办法激活 virtualenv,然后从单个 python 脚本安装我的要求?
I can't call the normal source /env/bin/activate and activate it, then use pip to install requirements.txt. Is there a way to activate the virtualenv and then install my requirements from a single python script?
我现在的代码:
if not os.path.exists(env_path):
call(['virtualenv', env_path])
else:
print "INFO: %s exists." %(env_path)
try:
call(['source', os.path.join(env_path, 'bin', 'activate')])
except Exception as e:
print e
错误是没有这样的文件目录"
the error is "No such file directory"
谢谢
解决方案
source
是 shell 内置命令,不是程序.它不能也不应该用 subprocess
执行.您可以通过在当前进程中执行 activate_this.py
来激活新的虚拟环境:
source
is a shell builtin command, not a program. It cannot and shouldn't be executed with subprocess
. You can activate your fresh virtual env by executing activate_this.py
in the current process:
if not os.path.exists(env_path):
call(['virtualenv', env_path])
activate_this = os.path.join(env_path, 'bin', 'activate_this.py')
execfile(activate_this, dict(__file__=activate_this))
else:
print "INFO: %s exists." %(env_path)
相关文章