Welcome to the Info TEST server!

Skip to content. | Skip to navigation

Sections

Python Virtualenv

Usage of the virtualenv program with Python at the NRAO

The NRAO provides the virtualenv package to simplify the use of multiple and alternative runtime environments with the Python programming language.

The virtualenv command takes a single argument, the name of a directory to contain the new virtual environment.
$ virtualenv example
This will create a directory named "example" in your current directory that contains the virtual environment. You can create a virtual environment with any arbitrary python interpreter using the -p option.
$ virtualenv -p /usr/bin/python example2.6
To use a virtual environment, you must activate it by sourcing the activate script inside the environment.
$ source example/bin/activate
Once you have activated the virtual environment you can install python packages into it as if you were the administrator of the machine. This differs from installing python packages into your account in that the new package only exists in the virtual environment, and not in all instances of python that you invoke outside of the virutal environment.

While the virtual environment is active your prompt will change to indicate you are inside a virtual environment.

(example)$ 
When you are finished with your virtual environment, you can deactivate it with the deactivate command.
(example)$ deactivate
This should return your environment to normal. If you activate a second virtual environment without deactivating the first you may find yourself needing the close the shell to fully deactivate both environments.

To delete a virtual environment, just delete its folder.

Name your virtual environments wisely, so that you can remember what they are days, weeks, or even months later.