Virtual Environments in Python

Why Use Virtual Environments

When working with Python projects, you may find yourself in need of installing libraries that aren’t included in the base installation of Python. Often, these libraries depend on other libraries, and sometimes very specific versions of them. This can result in the dependencies for one library conflicting with those of another. To avoid this, it is recommended that you use virtual environments for each of your projects in order to ensure there are no conflicting libraries. 

Additionally, using virtual environments allows you to install Python libraries via Pip without needing root access or requesting the IT staff to install the libraries for you. 

Creating Virtual Environments

The process of creating a virtual environment differs between versions of Python.

Python 3

To create a virtual environment with Python 3, run the following in the terminal:

python3 -m venv <name>

This will create a new folder in the current directory called “<name>” and will become the location where your libraries are stored after entering the virtual environment. 

Python 2 

Please note that Python 2 will enter end-of-life on 1 January 2020. It is recommended to use Python 3 whenever possible.

To create a virtual environment with Python 2, run the following in the terminal:

virtualenv -p python2 <name>

This will create a new folder in the current directory called “<name>” and will become the location where your libraries are stored after entering the virtual environment. 

If you receive an error that virtualenv is not found, you can install it for your current user by running:

pip2 install --user virtualenv

Using Virtual Environments

After creating a virtual environment, you can enter it by running:

Where “/path/to/environment” is the location of the virtual environment folder and “<name>” is the name of your virtual environment. When you enter a virtual environment, you will see the name of your virtual environment in front of the prompt in your terminal. It should look similar to one of the following:

It may look different depending on your environment and your computer settings.

For example, suppose we created a virtual environment named “my_env” in our home directory. To enter the virtual environment, we would run:

If you’re activating the virtual environment for the first time, you should upgrade pip by running: 

When finished using Python in the virtual environment, or if you need to set up a new one, you should exit the current virtual environment by running:

Adding Pip Packages to Virtual Environments

Once you have entered your virtual environment using the source command, you will be able to install packages through pip as normal. Please remember that since these packages are installed in a virtual environment, only those who can access the virtual environment directory will be able to use those packages. You can search for any packages by running:

Once you have the name of your package, you can install it by running:

After you have installed the package, you can use it with any Python script or the Python interpreter directly as long as you are still inside your virtual environment. 

Deleting a Virtual Environment

If you no longer need a virtual environment you have created and wish to delete it, you can do so by deleting the directory which contains your virtual environment. You can do this through the terminal by running: