virtualenv
的文档说要从python内部激活环境,请使用
activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
但是在Python 3中不存在execfile
。如果我尝试使用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?
答案 0 :(得分:3)
在全局中传递__file__
:
exec(open("venv/bin/activate_this.py").read(), {'__file__': "venv/bin/activate_this.py"})