Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 324
the_drow Avatar asked Jul 20 '12 05:07

the_drow


People also ask

How do I reactivate a virtual environment in Python?

To install virtualenv, just use pip install virtualenv . To create a virtual environment directory with it, type virtualenv /path/to/directory . Activating and deactivating the virtual environment works the same way as it does for virtual environments in Python 3 (see above).

How do I activate Virtualenv activation?

To activate virtualenv on Windows, first, install the pip. For this purpose, you can download and execute the latest Python installer. Next, install and create virtualenv on Windows using the pip package manager. Then, activate it using the “venvironment\Scripts\activate” command.

How do I turn off virtual environment in Python?

You can exit from the virtualenv using exit command, or by pressing Ctrl+d.

How do I know if Virtualenv is activated?

Note: Before installing a package, look for the name of your virtual environment within parentheses just before your command prompt. In the example above, the name of the environment is venv . If the name shows up, then you know that your virtual environment is active, and you can install your external dependencies.


1 Answers

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))
like image 175
brechin Avatar answered Oct 27 '22 01:10

brechin