Python virtual environment

Introduction

A virtual environment (or venv) is an isolated python environment in which you can use a specific Python version and install packages.

At this point you may ask:

Why should I create a virtualenv since I can already install everything I need?

Imagine running several projects and maybe each of them requires a different version of python.
How could you make python and package versions match together?
The risk of having conflicts is very high and this would break your applications.

This is the reason why virtualenvs exist and why you should definitely use them.

• Create a virtual environment (Python 3 and Python 2)
• Install a new python package
• List all installed python packages
• Delete a virtualenv

Python 3 virtual environment creation

Create a Python virtual environment is very simple. The only prerequisite is to have a Python version installed on your computer.

To create a virtualenv you should open a terminal/cmd and run the following command:

python3 -m venv /path/to/new/virtual/environment

If you are working on Windows you should run a slightly different command:

c:\>c:\Python35\python -m venv c:\path\to\new\virtual\environment

Where c:\Python35\python is the python.exe in the Python installation folder.

In this example I created a virtualenv called my_new_venv in dev_in_simple_words folder.

Regardless of the operating system used, this command will create the virtualenv folder in the specified path.

Pay attention that if you have installed more than one python versions on your computer, you have to choose the right one.
This means that if you have installed Python38 and Python35 and you want to create a virtualenv using Python38, the command will be:

c:\>c:\Python38\python -m venv c:\path\to\new\virtual\environment

The same logic is applicable to Linux operating system.

Can happen that when you run this command, an error message is thrown telling you that the command python3 or python is not recognized.
Anyway, if you want to be sure that the command will work, specify the python path like showed in the code snippet above.

Python 2 virtual environment creation

Although Python 2 is no longer supported, many applications and systems still use this version.

For this reason, in this chapter we will see how to create a virtualenv with Python 2.
You can create a virtualenv with this command:

virtualenv /path/to/new/virtual/environment

Obviously this command assumes that you have Python 2 installed in your local environment.
The command is the same whether you’re using Linux or Windows.

After creating a virtualenv with Python 2 or with Python 3 it is important to upgrade pip.
This allows us to always install the correct and up-to-date modules.

To upgrade pip, you must first activate the virtualenv.

# linux version
source path/venv/bin/activate
# windows version
path\venv\Scripts\activate.bat

After that run this command:

python -m pip install --upgrade pip

If you want to check the version of pip installed use this command:

pip --version

How to install a new Python package in a virtualenv

If you reach this section it means that you have a working virtualenv and you have some packages to install.

The first thing to do is to activate the virtualenv using this command:

source path/venv/bin/activate

Or if you are using Windows

path\venv\Scripts\activate.bat

To install a new python package we will use pip that is a package installer for Python.
Creating a virtualenv, pip is already included and so you can use it running the following command:

pip install <python_package_name>

At the end of this command, if no errors have been raised, you have the package installed in your virtualenv.

I decided to install openpyxl that is a cool package to read and write Excel files using Python.
As you can see from the output of the command there is a dependecies and it is installed automatically.

Last but not least, it is possible to specify also the version of the package during the installation.
Just use this command:

pip install <python_package_name>==version
Example:
pip install openpyxl==1.0.0

At the end of your operations with the virtualenv, it is possible to deactivate it.
To do this, just use this command if you work on Linux:

deactivate

If this is not working try with this command:

source deactivate

Check which packages have been installed in the python virtualenv

If you want to be sure that all the packages have been installed or simply you want to do a check on what is present, you can use this command:

pip freeze

As exepected there are two installed packages in my virtualenv.

Delete Python virtual environment

Maybe you need to delete a virtualenv because maybe you are no longer working on a certain application or maybe you just need to start over from a clean environment.

To delete a Python virtual environment, just delete its folder.
You can of course do this from File Explorer or using this command from the terminal:

rm -rf path/venv

Conclusion

So, to summarize, the steps to take to use a virtualenv are: create it, activate it, upgrade pip and use it to install the necessary packages.
Last but not least we saw also how to delete the Python virtual environment .

At this point you know all the basic commands for working with virtualenv.
Pip and venv have several additional commands, if you are interested look at the documentation or write me in the comments what you want to know more!

If this topic is clear to you, take a look at the latest posts!