Understanding Python 3 virtual environments different approaches

A bit of light over the usage of virtualenv, pip, venv, pyvenv and pipenv.

Published:
Last modified:

Overview

Virtual environments has evolved and many packages and different approaches has appeared in last years to solve the same situation for isolating each project set of dependencies.

We will take a look at the different alternatives and how it evolved to try to make it clear the confusion caused by all the similar project names.

Definitions

First of all, let’s see some useful concepts.

Virtual environment

What is a Virtual environment?

A cooperatively isolated runtime environment that allows Python users and applications to install and upgrade Python distribution packages without interfering with the behaviour of other Python applications running on the same system.

Pip

What is Pip?

A tool for installing Python packages. 1

It is recommended by The Python Packaging Authority (PyPA), a working group that maintains many of the relevant projects in Python packaging. 2

Understanding differences

Following is a list of the recommended ways of handling virtual environments for each Python version to see how they relate to each other:

  • Before Python 3.3:
    • Ian Bicking’s virtualenv package
    • virtualenvwrapper is a set of extensions to Ian Bicking’s virtualenv tool.
  • Python 3.3:
    • recommends pyvenv script
    • a subset of virtualenv was integrated into the standard library under the venv module
  • Python 3.6: venv module
    • deprecates pyvenv to avoid confusion as to what Python interpreter pyvenv is connected to and thus what Python interpreter will be used by the virtual environment 3
    • recommends: python3 -m venv /path/to/new/virtual/environment
      • In Python -m <module-name> searchs sys.path for the named module and execute its contents as the __main__ module.4
      • If you work with many virtualenvironments it is helpful to use the same system site-packages and symlinks to save space as: python3 -m venv –system-site-packages –symlinks /path/to/new/virtual/environment
  --system-site-packages
                        Give the virtual environment access to the system
                        site-packages dir.
  --symlinks            Try to use symlinks rather than copies, when symlinks
                        are not the default for the platform.

Note that this is a raw summary mostly based in recommendations of the official docs, you may still use any of them.

Nowadays there is also the pipenv project which is a more integral approach to handle not only virtual environments but also Pipfiles, and pip. It will create virtualenvs at ~/.local/share/virtualenvs.

Pipfile and Pipfile.lock are a replacement for the existing standard pip’s requirements.txt file 5 which provides more control over package’s versions.

References

Uruguay
Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus


An overview of the different virtual environment solutions for Python, including virtualenv, virtualenvwrapper, pip, venv, pyvenv and pipenv.

Clutter-free software concepts.
Translations English Español

Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.

Powered by SimpleIT Hugo Theme

·