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
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:
sudo apt update 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
Navigate to the project directory
PythonApp and initialize the virtual environment by typing the following command:
python3 -m venv PythonAppVenv
The above command will set up the virtual environment for the project
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:
To start using the virtual environment, you need to activate it using the following command:
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
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.
Once you are done with the virtual environment, you can deactivate it using the following command:
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:
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:
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:
pip install -r requirements.txt
virtualenvwrapper to Make Things Easier
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 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.
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
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
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.
The above command creates the virtual environment and then activates it.
To deactivate the virtual environment, you need to type in the
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:
In the above command,
PythonV is the name of the virtual environment. Here is an image where the
workon command is shown in action:
virtualenvwrapper also provides a command to list the virtual environments in your environment.
The above command displays a list of virtual environments that exist in the environment.
To remove an existing virtual environment, you can use the
There is a command which creates a project directory and its associated virtual environment. Navigate to the terminal and execute the following command:
The above command should create the project and its associated virtual environment.
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:
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:
conda create -n env_name [python=version]
env_nameis the name of your environment
python=versionwill be the Python version, e.g.
For example, let's create an environment called
env that uses Python 3.10:
conda create -n env python=3.10
Once the environment is created, activate the environment.
conda activate env
The terminal prompt should change like this:
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.
conda install jupyter notebook
If you want to install more than one package, separate them as shown below:
conda install pandas, numpy
You can also install a package by specifying the version number as follows:
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:
conda search '*pandas*' Loading channels: done # Name Version Build Channel geopandas 0.3.0 py27_0 pkgs/main geopandas 0.3.0 py27h5233db4_0 pkgs/main geopandas 0.3.0 py35h2f9c1c0_0 pkgs/main geopandas 0.3.0 py36_0 pkgs/main geopandas 0.3.0 py36h2f9c1c0_0 pkgs/main geopandas 0.3.0 py37_0 pkgs/main geopandas 0.4.0 py27_1 pkgs/main geopandas 0.4.0 py36_1 pkgs/main geopandas 0.4.0 py37_1 pkgs/main geopandas 0.4.1 py_0 pkgs/main geopandas 0.6.1 py_0 pkgs/main geopandas 0.8.1 py_0 pkgs/main geopandas 0.8.1 pyhd3eb1b0_0 pkgs/main geopandas 0.9.0 py_1 pkgs/main geopandas-base 0.9.0 py_1 pkgs/main pandas 0.20.3 py27h820b67f_2 pkgs/main pandas 0.20.3 py27hfd1eabf_2 pkgs/main pandas 0.20.3 py35h85c2c75_2 pkgs/main 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:
# update a package conda update pandas # remove a package conda remove pandas
You can also view all the packages installed in your conda environment.
Anaconda already comes with preinstalled packages. You should see something like this:
# packages in environment at /home/earth/miniconda3/envs/env: # # Name Version Build Channel _libgcc_mutex 0.1 main _openmp_mutex 4.5 1_gnu argon2-cffi 21.3.0 pyhd3eb1b0_0 argon2-cffi-bindings 21.2.0 py39h7f8727e_0 asttokens 2.0.5 pyhd3eb1b0_0 attrs 21.4.0 pyhd3eb1b0_0 backcall 0.2.0 pyhd3eb1b0_0 blas 1.0 mkl bleach 4.1.0 pyhd3eb1b0_0 bottleneck 1.3.4 py39hce1f21e_0 bzip2 1.0.8 h7b6447c_0 ca-certificates 2022.4.26 h06a4308_0 certifi 2021.10.8 py39h06a4308_2
To deactivate an environment:
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.
You should see something like this:
name: env channels: - defaults dependencies: - _libgcc_mutex=0.1=main - _openmp_mutex=4.5=1_gnu - argon2-cffi=21.3.0=pyhd3eb1b0_0 - argon2-cffi-bindings=21.2.0=py39h7f8727e_0 - asttokens=2.0.5=pyhd3eb1b0_0 - attrs=21.4.0=pyhd3eb1b0_0 - backcall=0.2.0=pyhd3eb1b0_0 - blas=1.0=mkl - bleach=4.1.0=pyhd3eb1b0_0 - bottleneck=1.3.4=py39hce1f21e_0 - bzip2=1.0.8=h7b6447c_0 - ca-certificates=2022.4.26=h06a4308_0 - certifi=2021.10.8=py39h06a4308_2 - cffi=1.15.0=py39hd667e15_1 - dbus=1.13.18=hb2f20db_0 - debugpy=1.5.1=py39h295c915_0 - decorator=5.1.1=pyhd3eb1b0_0 - defusedxml=0.7.1=pyhd3eb1b0_0 - entrypoints=0.4=py39h06a4308_0 - executing=0.8.3=pyhd3eb1b0_0 - expat=2.4.4=h295c915_0
Let's export the packages to a YAML file.
conda env export > environment.yaml
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:
conda env create -f environment.yaml
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.