Python virtual environment

Table of Contents

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.
This article will explain you how to:

  • Create a virtual environment
  • Install a new python package
  • List all installed python packages

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

Virtualenv creation on Linux OS
Virtualenv creation on Linux OS

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.
In this case please check your environment variables.
Anyway, if you want to be sure that the command will work, specify the python path like showed in the code snippet above.

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
Virtualenv activation
Virtualenv activation

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.

Installation of openpyxl package
Installation of openpyxl package

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

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
List of installed packages
List of installed packages

As exepected there are two installed packages in my virtualenv.

Conclusion

So, to summarize, the steps to take to use a virtualenv are: create it, activate it and use pip to install the necessary packages.

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!

Leave a Comment

Your email address will not be published. Required fields are marked *