# Understanding Virtual Environments in Python

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:

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

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

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:

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 PythonApp.

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:

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:

## 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.

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:

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.

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 deactivate command.

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 rmvirtualenv command.

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:

• 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:

Once the environment is created, activate the environment.

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.

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

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

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:

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:

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

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

To deactivate an environment:

### 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:

Let's export the packages to a YAML file.

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:

## 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.