5 minutes read

Introduction

If you spend a lot of time programming, the chances are that you work with different Python versions quite often. That's why you need to install additional packages and modules outside the standard library. Most packages are updated regularly, and there are many versions, older and newer. It can present an issue if you need to maintain an outdated project of yours, as you cannot have two versions of one package at the same time. Another example would be when you want to deploy your code to another machine. How can you make sure that the program will work as planned? The solution is to create a virtual environment. With its help, you can manage different versions of packages and modules, recreate your environment on another machine or create an environment with required settings on your own machine to develop and test your code.

Roughly speaking, a virtual environment is a directory that contains a particular Python version and packages. You can create a separate virtual environment for every project that requires something that comes in conflict with what you use habitually. Very convenient, isn't it?

Creating virtual environment

In this topic, we'll refer to a tool from the standard Python library. It is venv. You can interact with it through the command line. To create a virtual environment, you can type either:

python -m venv new_project

or:

python3 -m venv new_project

The Python command defines the Python version you'd like to use. The -m flag stands for the module-name.

We have created the new_project directory that contains a bunch of other directories along with the Python interpreter, the standard library, and various supporting files. Let's change our current directory for convenience:

cd new_project

To start working with our virtual environment, we need to activate it.

On Windows, you can run:

Scripts\activate.bat

On Unix or macOS, run:

source bin/activate

After this, you'll see the virtual environment in your shell:

Virtual environment shell

Working with packages

You can do many things with pip in your virtual environment:

  • you can install, upgrade, and remove packages with pip install, pip install --upgrade and pip uninstall respectively:

    Shell pip commands
  • pip install package_name==package_version specifies a particular version of a package:

    pip install command
  • pip show package_name shows the detailed information about a package, including the version, summary, author, location, and so on:

    pip show command
  • pip list displays the installed packages:

    pip list installed packages
  • pip freeze displays the installed packages in the requirements format. To create a requirements.txt file, type pip freeze > requirements.txt:

    pip freeze command

When you're done with the virtual environment, deactivate it:

Virtual environment deactivate

Once you've created an environment, it will be stored on your machine. You can activate and deactivate it at any time. If you don't need the environment, delete the directory. For example, for Unix or macOS run:

rm -r new_project

for Windows:

rmdir new_project

Tip: Before deleting a directory, deactivate the environment first.

Other libraries

As you already know, venv is a part of the standard Python library, but there are quite a few external packages for the same purpose. Each has its own peculiarities and additional tools. We will try to cover them briefly:

  • virtualenv is probably the most popular external library for working with virtual environments. Since recently, the subset has been integrated under the venv module. Virtualenv addresses several issues of venv, such as slower performance, upgradability, inability to create separate environments for arbitrary Python versions, and several others.
  • pyenv is another external library for managing virtual environments. It makes working with many different Python versions easier and is ideal for testing across versions.

Summary

In this topic, we've learned what a virtual environment is and how to work with it using the venv module from the standard Python library. Now you know how to create, activate, manage, and deactivate packages in the environment. We have also touched upon other options that can help you manage virtual environments.

436 learners liked this piece of theory. 8 didn't like it. What about you?
Report a typo