virtualenv
的文档说要从 python 内部激活环境,请使用
activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
但是 execfile
在 Python 3 中不存在。如果我尝试使用 exec(open("venv/bin/activate_this.py").read())
, 它提示
AssertionError: You must run this like execfile('path/to/activate_this.py', dict(__file__='path/to/activate_this.py'))
这是有道理的,因为 activate_this.py
引用了 __file__
。
如何从 python 3 中激活 virtualenv?
最佳答案
在全局变量中传递 __file__
:
exec(open("venv/bin/activate_this.py").read(), {'__file__': "venv/bin/activate_this.py"})
关于python - 从正在运行的 Python3 session 中激活 virtualenv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46009735/