A Python virtual environment is a package that makes it possible to have several virtual installations of Python with their own packages. Programs running in one virtual environment do not interfere with the main installation of Python and other installations of Python. The ability to have virtual installations of Python with their own packages makes virtual environments similar to containers; however, unlike containers, virtual environments do not make it possible to assign specific resources to different environments.
Why you should use virtual environments
The ability to have several installations of virtual environments presents the user with certain advantages, such as:
Keeping track of the packages required for a particular project
Having a single installation of Python means that the packages for each of your projects will be installed in the same location. The ability to have a clean installation where project-specific packages can be found makes it easy to keep track of those packages, and makes it easy for several developers to work on the same project. For example, the packages in a virtual environment can be stored in a file using the following command:
$ pip freeze > requirements.txt
This command will save all the packages in your virtual environment to the file named requirements.txt in the directory where the command was run. The requirements file can then be used to install the project-specific packages in other virtual environments, thereby simplifying the process of collaborating on the same project. Note that this command should be run after the virtual environment has been activated, otherwise pip will freeze the packages in your main installation of Python. (Activating virtual environments and installing packages from the requirements file will be covered under the How to use virtual environments section.
Working with packages that conflict with each other
Once in a while, you will come across the awkward situation where some Python packages cannot be installed side-by-side because they conflict. The conflict may arise because some packages depend on different versions of the same package. This can be easily resolved by installing the conflicting packages in different virtual environments, provided they are not needed for the same project.
Using different versions of the same package
If for whatever reason you need to use different versions of the same package on different projects, virtual environments will allow you to have both packages on the same system without hassle. Just install the required package in the virtual environment of your respective projects. Make sure you activate the environment pertaining to your desired project before you start working on it, and you’re good to go.
How to use virtual environments
To use virtual environments, the virtual environment package must first be installed using the command:
$ pip install virtualenv
Next, navigate to your project directory and create a virtual environment by typing
$ virtualenv myvenv
This will create a new virtual environment in the directory myvenv within your project directory. You can specify the version of Python you want to use in the virtual environment with
$ virtualenv -p /path/to/python myvenv
Activate the virtual environment while in your project directory by typing
$ source ./myvenv/bin/activate
For Unix based systems. On windows, type
After activating the virtual environment, its name will be seen in the command line prompt, like this:
After activating the virtual environment, calling Python in the shell will run Python in the virtual environment. All packages imported in Python programs will also be imported from the virtual environment. For example, typing:
(myvenv) $ python myfile.py
Will run myfile.py in the virtual environment and import all packages imported in myfile.py from the virtual environment. You can also install packages in your virtual environment with pip simply by typing:
(myvenv) $ pip install
You can also install packages from a requirements.txt file generated with pip freeze by navigating to the directory with the requirements.txt file and typing the following command:
(myvenv) $ pip install -r /path/to/requirements.txt
Finally a virtual environment can be deactivated by typing
(myvenv) $ deactivate
Even though using a single installation of Python is usually sufficient, it is advisable to keep packages for different projects separate, especially for the sake of collaboration. The benefits of using virtual environments typically outweigh the overhead of learning to use them and setting them up.