如何从python代码激活/停用virtualenv?

the*_*row 9 python virtualenv

对于激活,有一个脚本可以激活已经运行的python interpeter使用的virtualenv execfile('C:/path/to/virtualev/Scripts/activate_this.py', dict(__file__='C:/path/to/virtualev/Scripts/activate_this.py')).但是,因为我仍然可以从当前的python脚本导入不在virtualenv中的包,所以我很困惑它是如何工作的.
对于停用,根本没有python脚本.
我该怎么办?

小智 6

来自VirtualEnv主页的一部分.

您必须使用自定义Python解释器来安装库.但是要使用库,您必须确保路径正确.脚本可用于更正路径.您可以设置以下环境:

activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
Run Code Online (Sandbox Code Playgroud)

  • @Pieter http://stackoverflow.com/questions/436198/what-is-an-alternative-to-execfile-in-python-3-0 (2认同)