从正在运行的Python3会话中激活virtualenv

时间:2017-09-02 01:41:57

标签: python python-3.x virtualenv

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?

1 个答案:

答案 0 :(得分:3)

在全局中传递__file__

exec(open("venv/bin/activate_this.py").read(), {'__file__': "venv/bin/activate_this.py"})
相关问题