如何从 python 代码激活/停用 virtualenv? [英] How to activate/deactivate a virtualenv from python code?
问题描述
对于激活,有一个脚本可以使用 execfile('C:/path/to/virtualev/Scripts/activate_this.py', dict(__file__='C:/path/to/virtualev/Scripts/activate_this.py'))
.但是,由于我仍然可以从当前的 python 脚本中导入不在 virtualenv 中的包,我对它的工作方式感到困惑.
对于停用,根本没有 python 脚本.
我该怎么办?
For activation there is a script that activates a virtualenv from an already running python interpeter using execfile('C:/path/to/virtualev/Scripts/activate_this.py', dict(__file__='C:/path/to/virtualev/Scripts/activate_this.py'))
. However since I can still import packages that are not in the virtualenv from the current python script I am confused about how it works.
For deactivation there is no python script at all.
What should I do?
推荐答案
您必须使用自定义 Python 解释器来安装库.但要使用库,您只需要确保路径正确.一种脚本可用于更正路径.你可以设置环境喜欢:
You must use the custom Python interpreter to install libraries. But to use libraries, you just have to be sure the path is correct. A script is available to correct the path. You can setup the environment like:
activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
这篇关于如何从 python 代码激活/停用 virtualenv?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!