Saturday, April 20, 2024
HomeTechPython Virtual Environment, what they are and how they are managed

Python Virtual Environment, what they are and how they are managed

If you follow this blog you will know that I have been studying Python for a few months . Consequently, this year I will write some small guides to remind me of the notions I learn, but these contents will only be a brief parenthesis in the SEO world, which I will continue to deal with with main focus.


·        Why do we need a virtual environment?

·        When and where to use a virtual environment?

·        How are virtual environments created?

·        How to manage a virtual environment

·        There is not only VENV

o   PyPI packages not present in the standard library

o   Standard library

o   Recommendation for beginners

  • Python is often regarded as the programming language of the future. According to the most recent figures, Python is the primary coding language used by about 80% of developers.
  • Python’s vast libraries make artificial intelligence, data science, and machine learning operations easier.

A virtual environment in Python is a tool that helps keep the dependencies required by different projects separate by creating isolated virtual environments for them. This is one of the most important tools used by most Python developers.

Why do we need a virtual environment?

Applications developed with Python will often use packages and modules that are not part of the standard library. Sometimes applications need a specific version of a library, as the application may need to fix a certain bug or the application may be written using an outdated version of the library interface.

This means that it may not be possible for a Python installation to meet the requirements of every application. If application A requires version 1.0 of a particular module but application B requires version 2.0, the requirements conflict and installing version 1.0 or 2.0 does not allow an application to run.

The solution to this problem is to create a virtual environment , a self-contained directory tree that contains a Python installation for a particular version of Python, as well as a number of additional packages.

Different applications can therefore use different virtual environments. To resolve the above example of conflicting requirements, application A can have its own virtual environment with version 1.0 installed while application B has another virtual environment with version 2.0. If application B requires a library update to version 3.0, this will not affect the environment of application A.

Imagine a scenario where you are working on two web based Python projects and one of them uses a Django 1.9 and the other uses Django 1.10 and so on. In such situations, the virtual environment can be really helpful in maintaining the dependencies of both projects.

When and where to use a virtual environment?

By default, every project on the system will use the same directories to store and retrieve site packages (third-party libraries).

Who cares? Now, in the above example of two projects, you have two versions of Django. This is a real problem for Python as it cannot distinguish between versions in the “site-packages” directory. So both v1.9 and v1.10 would reside in the same directory with the same name. This is where virtual environments come into play. To solve this problem, we just need to create two separate virtual environments for both projects. The great thing about this is that there is no limit to the number of environments you can have as they are just directories that contain some scripts.

The virtual environment should be used whenever you are working on any Python-based project. It’s generally good to have a new virtual environment for every Python-based project you work on. So the dependencies of each project are isolated from the system and from the other.

How are virtual environments created?

We use a module called venv which is a tool for creating isolated Python environments. Venv creates a folder that contains all the executables needed to use the packages a Python project would need.

How to manage a virtual environment

The module used to create and manage virtual environments is called venv .

venv usually installs the latest version of Python you have available. If you have multiple versions of Python on your system, you can select a specific version of Python by running python3 or whatever version you want.

To create a virtual environment with venv, enter the folder that must contain it and type:

The command just seen creates a virtual environment called “env1” which contains a structure of files and folders similar to this:

Here’s what each folder contains:

·        bin: files that interact with the virtual environment

·        include: C headers that compile Python packages

·        lib: A copy of the Python version along with a site packages folder where each dependency is installed

After creating it you need to activate the virtual environment:

While to deactivate it type:

To view the active virtual environment, type:

To view all installed packages and export a list file, navigate to the virtual env folder and type (if you want to see packages venv activate venv):

To copy a virtual environment venv move the requirements.txt file to the new folder and type:

To completely remove a virtual environment, type:

There is not only VENV

Python 3.3 includes the new venv package in its standard library. What does it do and how does it differ from all other packages for managing virtual environments? Let’s see the main differences.

PyPI packages not present in the standard library

·        virtualenvis a very popular tool that creates isolated Python environments for Python libraries. If you are not familiar with this tool, I highly recommend that you learn it, as it is a very useful tool and I will be comparing it for the rest of this answer. It works by installing a bunch of files in a directory (for example: env /), and then modifying the PATH environment variable to prefix it with a custom bin directory (for example: env / bin /). An exact copy of the python or python3 binary is placed in this directory, but Python is programmed to first look for the libraries relative to its path, in the environment directory. It is not part of the standard Python library, but is officially accepted by the PyPA (Python Packaging Authority). Once activated,

·        pyenv is used to isolate Python versions. For example, you may want to test your code with Python 2.6, 2.7, 3.3, 3.4, and 3.5, so you’ll need a way to switch between environments. Once activated, prefigure the PATH environment variable with ~ / .pyenv / shims, where there are special files corresponding to Python commands (python, pip). These are not copies of the commands provided by Python; are special scripts that decide on the fly which version of Python to run based on the PYENV_VERSION environment variable, the .python-version file, or the ~ / .pyenv / version file. pyenv also simplifies the download and installation process of multiple versions of Python by using the pyenv install command.

·        pyenv-virtualenv is a plugin for pyenv from the same author as pyenv, to allow you to comfortably use pyenv and virtualenv at the same time. However, if you are using Python 3.3 or later, pyenv-virtualenv will attempt to run python -m venv if available, instead of virtualenv.

·        virtualenvwrapper is a set of extensions to virtualenv. It gives you commands like mkvirtualenv, lssitepackages, which is useful for switching between virtual directories.

·        pyenv-virtualenvwrapper is a plugin for pyenv from the same author as pyenv, to conveniently integrate virtualenvwrapper into pyenv.

·        pipenv , by Kenneth Reitz, is the most recent project on this list. It aims to combine Pipfile, pip and virtualenv into one command. The virtualenv directory is typically placed in ~ / .local / share / virtualenvs / XXX, with XXX as the hash of the project directory path. This differs from virtualenv, where the directory is typically located in the current working directory.

The Python Certification List Packaging Guide recommends pipenv when developing Python applications (as opposed to libraries). There appears to be no plan to support venv instead of virtualenv. One confusing aspect is that its –venv command line option refers to the virtualenv directory, not venv, and likewise, the PIPENV_VENV_IN_PROJECT environment variable affects the location of the virtualenv directory, not the venv directory.

Standard library

·        pyvenv is a script shipped with Python 3 but deprecated in Python 3.6 as it had problems. In Python 3.6+, the exact equivalent is python3 -m venv.

·        venv is a package that comes with Python 3, which you can run using python3 -m venv (although for some reason some distros separate it into a separate package, like python3-venv on Ubuntu and Debian). It has a similar purpose to virtualenv and works just as well or badly in the same way, but it doesn’t need to copy Python binaries (except on Windows). Use it if you don’t need to support Python 2. As of this writing, the Python community seems to be happy with virtualenv and I haven’t heard much about venv.

Most of these tools complement each other. For example, pipenv integrates pip, virtualenv, and even pyenv if desired. The only tools that are true alternatives to each other here are venv and virtualenv.

Recommendation for beginners

This is my personal recommendation for beginners: start by learning venv or virtualenv and pip, tools that work with Python 2 and 3 and in a variety of situations, and move on to the other tools when you start to need them


Most Popular

Recent Comments