Python Virtual Environments: A radiography

When you start learning about Python, you hear a lot about ‘virtual environments’, but what’s that?

Environments, an explanation:

Python applications normally uses packages and modules that are not part of the standard library. That means that for some programs you have to install packages that are required to run a program.

With every version new stuff is added, but as old code is dropped, refactored or changes its functionality, backwards compatibility is not always assured.

Let’s take for an example, Django. Django is one of the best Python frameworks for web creation and it is constantly evolving, releasing a new version every 8 months.

With that in mind, you install Django 1.11 in your computer and you create websites with this Django version. Then the new Django version  2.0 hits and you upgrade it to the new one.

You keep working as usual but you spot a bug in a webapp you did 6 months ago. Trying to fix it, you open your text editor, run the server but…the server won’t run. Why is that?

Every package or module version needs and calls different stuff that you cannot see, so upgrading your packages doesn’t mean that the code you wrote 1, 2 or 5 years ago is compatible with the new version.

How to solve it:

Instead of installing everything in your computer configuration, we create ‘virtual environments’.

Think about ‘virtual’ drawers where we keep our dependencies: If you put socks in your top drawer, only your top drawer will have socks, no other drawers are involved. Even so, you can have socks 1.2 in your top drawer and socks 1.56b in your second drawer, and you can easily find the new ones.

How to create virtual environments

Once you have Python installed it is easy to install anything, as it comes with ‘pip’, a package management system used to install Python packages.

To do so, open your OS terminal and navigate to your project’s folder. First we need to install the virtual environment creator. There are a few ones, but we are going to use ‘pipenvFirst, we need to install it. Type in your terminal:

pip install pipenv

Once it is done, you’ll have pipenv in your computer. Now, we need to create a virtual environment (in our example, a new drawer).

pipenv shell

Wait a few seconds and your virtual environment is created and loaded. For now on, everything you install it will be in that file and that file only. You can have multiple diferent virtual environments for different purposes: One for websites, other for working with .csv files, other one for creating videogames (yes, you can do that with PyGame). Even multiples environments with different versions of the same package.

Let’s install anything, for example, Django:

pip install Django

Now we have a virtual environment, in the desired folder, with only Django 2.1 version installed. This won’t affect other virtual environments. Let’s see the result:

If few steps you have created your own virtual environment and installed packages in it. Any project you create with this environment will use the same packages.

Let’s view some useful commandos:

  • pipenv shell – Creates a new environment in a folder without one
  • pipenv shell – Loads an existing environment in a folder with one
  • pip install NAME – Installs NAME package
  • pip uninstall NAME – Uninstalls NAME package
  • pip list – Displays a list with the installed packages
  • pip freeze – Same as pip, without the basic packages
  • exit – Deactivates the current environment

The best way to learn this is to play with it. Create a new environment, install packages, list them to check you have them installed, uninstall them then exit.

It confused me a bit when I started learning Python, but over time it will be second nature, you just need to practise.

Congratulations! Now you know that a virtual environment is a ‘box’ for all Python dependencies, how to create one, install packages and how to deactivate it.