简体   繁体   中英

How to activate/deactivate a virtualenv from python code?

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?

From part of the VirtualEnv homepage .

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))

如果你想运行virtualenv之外的程序,只需运行你的系统python可执行文件(例如/usr/bin/python )而不是virtualenv中的那个。

This souds like bad idea. You are trying to modify environment of your script within this script. Please explain why?

Can't you do it hierarchical? Use one script to run different scripts in different virtualenvs.

在命令行中,键入“停用”一词

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM