Advertisement
  1. Code
  2. Python

Understanding Virtual Environments in Python

Scroll to top
Read Time: 9 min

In this tutorial, you'll learn about virtual environments. You'll learn about the importance of using virtual environments in Python and how to get started with using virtual environments.

What Is a Virtual Environment?

A virtual environment is a tool to maintain separate space for a project, with its dependencies and libraries in one place. This environment is specific to the particular project and doesn't interfere with other projects' dependencies.

For example, you can work on project X which is using version 1.0 of library Z and also maintain project Y which is using version 2.0 of library Z.

How Do Virtual Environments Work?

The virtual environment tool creates a folder inside the project directory. By default, the folder is called venv, but you can give it a custom name too. It keeps Python and pip executable files inside the virtual environment folder. When the virtual environment is activated, the packages installed after that are installed inside the project-specific virtual environment folder.

Getting Started With Venv

The venv module is the recommended way to install a virtual environment, and it comes with Python 3. To get started, first make sure you have pip installed on your system. You can install pip using the following commands:

1
sudo apt update
2
sudo apt install python3-pip

To start using venv, you need to initialize and activate it. Let's start by creating a new Python project directory PythonApp.

1
mkdir PythonApp

Navigate to the project directory PythonApp and initialize the virtual environment by typing the following command:

1
python3 -m venv PythonAppVenv

The above command will set up the virtual environment for the project PythonApp.

It creates a folder called PythonAppVenv inside the project directory PythonApp. It keeps the Python and pip executables inside the virtual environment folder. Any new packages installed for the project after activating the virtual environment are placed inside the virtual environment folder. Here is the folder structure:

folder structurefolder structurefolder structure

To start using the virtual environment, you need to activate it using the following command:

1
source PythonAppVenv/bin/activate

Once it's activated, you should be able to see the PythonAppVenv name on the left side of the name prompt.

Let's try to install a new package to the project PythonApp.

1
pip install flask

The new package should get installed in the virtual environment folder. Check the virtual environment folder inside lib64/python3.9/site-packages, and you should be able to find the newly installed flask package. You can learn more about Flask on the project page.

newly installed flask packagenewly installed flask packagenewly installed flask package

Once you are done with the virtual environment, you can deactivate it using the following command:

1
deactivate 

Easier to Track Packages

While working with Python programs, you install different packages required by the program. You keep working, and the list of packages installed keeps on piling up. Now the time arrives when you need to ship the Python code to the production server. Oops... You really don't know what packages you need to have installed for the program to work.

All you can do is open the Python program, check for all the packages that you have imported in your program, and install them one by one.

A virtual environment provides an easier method to keep track of the packages installed in the project. Once you have activated the virtual environment, it provides the facility to freeze the current state of the environment packages.

You can achieve this by using the following command:

1
pip freeze > requirements.txt

The above command creates a file called requirements.txt which has details about the packages with versions in the current environment. Here is how it looks:

Requirements FileRequirements FileRequirements File

Now this file would be really helpful for deploying the project on a different platform since all the project dependencies are already at your disposal in the requirements.txt file. To install the project dependencies using the requirements.txt file, execute the following command:

1
pip install -r requirements.txt

virtualenvwrapper to Make Things Easier

The venv tool is really a boon for developers. But it gets really complicated when you have to deal with more than one virtual environment. To manage multiple virtual environments, there is an extension to the virtualenv tool called virtualenvwrapper.

virtualenvwrapper is a wrapper around the virtualenv tool which provides the functionality to manage multiple virtual environments.

Let's get started by installing virtualenvwrapper using pip.

1
sudo pip3 install virtualenvwrapper

Once you have installed virtualenvwrapper, you need to set the working directory where the virtual environments will be stored. Execute the following command to set the working directory for virtualenvwrapper:

1
export WORKON_HOME=.virtualenvs

The above command sets the working directory for virtualenvwrapper to the .virtualenvs folder in the home directory.

You can either source the virtualenvwrapper commands to run from the terminal or add the virtualenvwrapper commands to the .bashrc.

1
source /usr/local/bin/virtualenvwrapper.sh
Source virtualenvwrapperSource virtualenvwrapperSource virtualenvwrapper

Now the commands will be accessible in the current terminal by pressing the Tab key. Create a new project folder called PythonProject. Navigate to the project directory. Earlier, when you used venv, you first created the virtual environment and then activated it. Using virtualenvwrapper, you can complete both of these tasks using a single command.

1
mkvirtualenv PythonVenv

The above command creates the virtual environment and then activates it.

Activate Virtual Environment Using virtualenvwrapperActivate Virtual Environment Using virtualenvwrapperActivate Virtual Environment Using virtualenvwrapper

To deactivate the virtual environment, you need to type in the deactivate command.

1
deactivate

Now suppose in certain scenarios you need to switch between the different virtual environments you are working in. virtualenvwrapper provides a workon method to switch virtual environments. The command to switch the virtual environment is:

1
workon PythonV

In the above command, PythonV is the name of the virtual environment. Here is an image where the workon command is shown in action:

Switching Virtual Environment Using workon CommandSwitching Virtual Environment Using workon CommandSwitching Virtual Environment Using workon Command

virtualenvwrapper also provides a command to list the virtual environments in your environment.

1
ls $WORKON_HOME

The above command displays a list of virtual environments that exist in the environment.

Listing Existing Virtual EnvironmentsListing Existing Virtual EnvironmentsListing Existing Virtual Environments

To remove an existing virtual environment, you can use the rmvirtualenv command.

1
rmvirtualenv PV

Remove the Virtual EnvironmentRemove the Virtual EnvironmentRemove the Virtual Environment

There is a command which creates a project directory and its associated virtual environment. Navigate to the terminal and execute the following command:

1
mkproject NewPro

The above command should create the project and its associated virtual environment.

Create Project Directory and Associated Virtual DirectoryCreate Project Directory and Associated Virtual DirectoryCreate Project Directory and Associated Virtual Directory

There are a few more commands that you can use in virtualenvwrapper. You can find the list of commands available by typing the following command:

1
virtualenvwrapper
List of Commands Available in virtualenvwrapperList of Commands Available in virtualenvwrapperList of Commands Available in virtualenvwrapper

Virtual Environments for Data Science With Anaconda

Anaconda is an open-source Python distribution platform that empowers data science applications. It comes with conda, an open-source package, and an environment manager.

Conda allows you to create environments quickly. You can also create and switch environments as needed.

The first step is to install Anaconda, which is available in Windows, macOS, and Linux. You can get the installer from the Anaconda website.

Anaconda is a relatively large file and will take up a lot of space. Luckily, you can also install miniconda, a small version of Anaconda that comes with Python and conda installed. Unlike Anaconda, it doesn't come with a graphical interface, but it's still sufficient and will work the same as Anaconda.

Once Anaconda is installed, you can create conda environments and install packages with the conda command. The syntax for creating a new environment is shown below:

1
conda create -n env_name [python=version] 
  • where env_name is the name of your environment
  • python=version will be the Python version, e.g. python=3.10

For example, let's create an environment called env  that uses Python 3.10:

1
conda create -n env python=3.10 

Once the environment is created, activate the environment.

1
conda activate env

The terminal prompt should change like this:

1
(env) earth@Yoga:~$

You can now install packages for your data science projects like pandas, numpy, or Jupyter notebooks. Let's install Jupyter notebook in the env conda environment.

1
 conda install jupyter notebook

If you want to install more than one package, separate them as shown below:

1
 conda install pandas, numpy

You can also install a package by specifying the version number as follows:

1
conda install pandas==1.1. 3 

If you are unsure about the version, you can use conda to search for the correct package and package version. For example, let's search for pandas:

1
conda search '*pandas*'
2
Loading channels: done
3
# Name                       Version           Build  Channel             

4
geopandas                      0.3.0          py27_0  pkgs/main           
5
geopandas                      0.3.0  py27h5233db4_0  pkgs/main           
6
geopandas                      0.3.0  py35h2f9c1c0_0  pkgs/main           
7
geopandas                      0.3.0          py36_0  pkgs/main           
8
geopandas                      0.3.0  py36h2f9c1c0_0  pkgs/main           
9
geopandas                      0.3.0          py37_0  pkgs/main           
10
geopandas                      0.4.0          py27_1  pkgs/main           
11
geopandas                      0.4.0          py36_1  pkgs/main           
12
geopandas                      0.4.0          py37_1  pkgs/main           
13
geopandas                      0.4.1            py_0  pkgs/main           
14
geopandas                      0.6.1            py_0  pkgs/main           
15
geopandas                      0.8.1            py_0  pkgs/main           
16
geopandas                      0.8.1    pyhd3eb1b0_0  pkgs/main           
17
geopandas                      0.9.0            py_1  pkgs/main           
18
geopandas-base                 0.9.0            py_1  pkgs/main           
19
pandas                        0.20.3  py27h820b67f_2  pkgs/main           
20
pandas                        0.20.3  py27hfd1eabf_2  pkgs/main           
21
pandas                        0.20.3  py35h85c2c75_2  pkgs/main           
22
pandas                        0.20.3  py35hd2a53da_2  pkgs/main

The search command will get all the packages with the word pandas and the package versions. You can also remove and update packages as follows:

1
# update a package

2
conda update pandas
3
4
# remove a package

5
conda remove pandas

You can also view all the packages installed in your conda environment.

1
conda list

Anaconda already comes with preinstalled packages. You should see something like this:

1
# packages in environment at /home/earth/miniconda3/envs/env:

2
#

3
# Name                    Version                   Build  Channel

4
_libgcc_mutex             0.1                        main  
5
_openmp_mutex             4.5                       1_gnu  
6
argon2-cffi               21.3.0             pyhd3eb1b0_0  
7
argon2-cffi-bindings      21.2.0           py39h7f8727e_0  
8
asttokens                 2.0.5              pyhd3eb1b0_0  
9
attrs                     21.4.0             pyhd3eb1b0_0  
10
backcall                  0.2.0              pyhd3eb1b0_0  
11
blas                      1.0                         mkl  
12
bleach                    4.1.0              pyhd3eb1b0_0  
13
bottleneck                1.3.4            py39hce1f21e_0  
14
bzip2                     1.0.8                h7b6447c_0  
15
ca-certificates           2022.4.26            h06a4308_0  
16
certifi                   2021.10.8        py39h06a4308_2

To deactivate an environment:

1
conda deactivate env

Sharing Environments With Anaconda

Another useful feature of anaconda is the ability to share environments so that another person can install the same packages in your environment. To do that, use the conda export command. Let's see all the packages installed in the env conda environment.

1
conda export

You should see something like this:

1
name: env

2
channels:
3
  - defaults
4
dependencies:
5
  - _libgcc_mutex=0.1=main
6
  - _openmp_mutex=4.5=1_gnu
7
  - argon2-cffi=21.3.0=pyhd3eb1b0_0
8
  - argon2-cffi-bindings=21.2.0=py39h7f8727e_0
9
  - asttokens=2.0.5=pyhd3eb1b0_0
10
  - attrs=21.4.0=pyhd3eb1b0_0
11
  - backcall=0.2.0=pyhd3eb1b0_0
12
  - blas=1.0=mkl
13
  - bleach=4.1.0=pyhd3eb1b0_0
14
  - bottleneck=1.3.4=py39hce1f21e_0
15
  - bzip2=1.0.8=h7b6447c_0
16
  - ca-certificates=2022.4.26=h06a4308_0
17
  - certifi=2021.10.8=py39h06a4308_2
18
  - cffi=1.15.0=py39hd667e15_1
19
  - dbus=1.13.18=hb2f20db_0
20
  - debugpy=1.5.1=py39h295c915_0
21
  - decorator=5.1.1=pyhd3eb1b0_0
22
  - defusedxml=0.7.1=pyhd3eb1b0_0
23
  - entrypoints=0.4=py39h06a4308_0
24
  - executing=0.8.3=pyhd3eb1b0_0
25
  - expat=2.4.4=h295c915_0

Let's export the packages to a YAML file.

1
conda env export > environment.yaml

The environment.yaml file will be saved in your current directory. You can then share it with a team member, who can create a matching environment as follows:

1
conda env create -f environment.yaml

Wrapping Up

In this tutorial, you saw how to get started with using virtual environments in Python. You learnt the importance of using a virtual environment and how it works. You also had a look at virtualenvwrapper, a wrapper in the virtualenv tool for managing multiple virtual environments. You also learned how to install Anaconda and use the conda package manager to manage environments and Python packages for data science.

Have you ever used virtual environments in Python? Do let us know your thoughts on the forum.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.