h3.*_*h3. 79 python virtualenv
我想从python脚本激活virtualenv实例.
我知道这很容易做到,但我见过的所有例子都使用它在env中运行命令然后关闭子进程.
我想要的只是激活virtualenv并返回shell,就像bin/activate一样.
像这样的东西:
$me: my-script.py -d env-name
$(env-name)me:
Run Code Online (Sandbox Code Playgroud)
这可能吗 ?
相关:
http://groups.google.com/group/python-virtualenv/browse_thread/thread/15f8a9b0506abeae
Lie*_*yan 71
如果你想在virtualenv下运行一个Python子进程,你可以通过使用生活在virtualenv的/ bin /目录中的python解释器运行脚本来做到这一点:
# Path to a Python interpreter that runs any Python script
# under the virtualenv /path/to/virtualenv/
python_bin = "/path/to/virtualenv/bin/python"
# Path to the script that must run under the virtualenv
script_file = "must/run/under/virtualenv/script.py"
subprocess.Popen([python_bin, script_file])
Run Code Online (Sandbox Code Playgroud)
但是,如果要在当前python解释器而不是子进程下激活virtualenv,可以使用以下activate_this.py
脚本:
# Doing execfile() on this file will alter the current interpreter's
# environment so you can import libraries in the virtualenv
activate_this_file = "/path/to/virtualenv/bin/activate_this.py"
execfile(activate_this_file, dict(__file__=activate_this_file))
Run Code Online (Sandbox Code Playgroud)
naf*_*esi 23
在virtualenv的解释器下运行脚本的最简单的解决方案是将默认的shebang行替换为virtualenv的解释器的路径,就像在脚本的开头一样:
#!/path/to/project/venv/bin/python
Run Code Online (Sandbox Code Playgroud)
使脚本可执行:
chmod u+x script.py
Run Code Online (Sandbox Code Playgroud)
运行脚本:
./script.py
Run Code Online (Sandbox Code Playgroud)
瞧!
h3.*_*h3. 20
事实证明,问题并不简单,但解决方案是.
首先,我必须创建一个shell脚本来包装"source"命令.那说我用的是"." 相反,因为我已经读过,使用它比使用bash脚本的源更好.
#!/bin/bash
. /path/to/env/bin/activate
Run Code Online (Sandbox Code Playgroud)
然后从我的python脚本我可以简单地这样做:
import os
os.system('/bin/bash --rcfile /path/to/myscript.sh')
Run Code Online (Sandbox Code Playgroud)
整个技巧都在--rcfile参数中.
当python解释器退出时,将当前shell保留在激活的环境中.
赢!
Mar*_*uiz 11
要根据官方的Virtualenv文档运行另一个Python环境,在命令行中可以指定可执行python二进制文件的完整路径(不需要之前激活virtualenv):
/path/to/virtualenv/bin/python
Run Code Online (Sandbox Code Playgroud)
如果您想使用virtualenv从命令行调用脚本,则同样适用,您不需要在之前激活它:
me$ /path/to/virtualenv/bin/python myscript.py
Run Code Online (Sandbox Code Playgroud)
对于Windows环境也是如此(无论是来自命令行还是来自脚本):
> \path\to\env\Scripts\python.exe myscript.py
Run Code Online (Sandbox Code Playgroud)
小智 8
最佳答案仅适用于 Python 2.x
对于 Python 3.x,请使用以下命令:
activate_this_file = "/path/to/virtualenv/bin/activate_this.py"
exec(compile(open(activate_this_file, "rb").read(), activate_this_file, 'exec'), dict(__file__=activate_this_file))
Run Code Online (Sandbox Code Playgroud)
参考:Python 3 中 execfile 的替代品是什么?
只是一个适合我的简单解决方案.我不知道为什么你需要bash脚本基本上做了一个无用的步骤(我错了吗?)
import os
os.system('/bin/bash --rcfile flask/bin/activate')
Run Code Online (Sandbox Code Playgroud)
基本上你需要的是:
[hellsing@silence Foundation]$ python2.7 pythonvenv.py
(flask)[hellsing@silence Foundation]$
Run Code Online (Sandbox Code Playgroud)
然后只需按Ctrl + D或退出,而不是停用venv.
这是一个可能的解决方案还是不是你想要的?
子进程环境在它不复存在的那一刻就丢失了,并且将环境内容从那里移动到父进程有些棘手。
您可能需要生成一个 shell 脚本(您可以动态生成一个到 /tmp),该脚本会将 virtualenv 环境变量输出到一个文件,然后您在父 Python 进程中读取该文件并将其放入 os.environ 中。
或者您只需解析 use for open("bin/activate") 行中的激活脚本,手动提取内容,然后放入 os.environ 中。这很棘手,但并非不可能。