What is virtualenv for in Python?

Asked

Viewed 242 times

3

What is virtualenv for in Python?

Do you have any alternative to non-use or is it more advisable to use it?

2 answers

8


Python virtualenv is used to isolate the version of Python and the libraries used on a given system.

If you do not use virtualenv, all the necessary libraries for your system would be installed in the operating system environment.

Setting

You have been hired to develop a data analysis system by company A and for this you will use Python 3.7.4 and the libraries pandas and numpy.

This same company hires you to build a registration system and you choose to use the same Python 3.7.4, however, as you will make this system available on their intranet, you use the Flask, psycopg2 (for access to Postgresql) and marshmallow.

You have the hobby of creating games and decide to study the pygame, however, the version of Python chosen is 3.8.1

A friend asked you to make a Crapping web and you decided to test Python 3.9. Also, you will use the libraries requests and bs4 (Beautifulsoup)

Issues

Should you nay utilize virtualenv for each project, you would have to use Python installed on your system and have to put all the libraries on it. This would cause a library management problem.

The passage "for each project" was highlighted, because if you use a virtualenv for all projects, you solve only the isolation of the environment in front of the operating system Python, but does not solve the management of the libraries used and would not have the possibility to use different versions of Python.

Upside

If you use the same library in two different projects and need to upgrade it in one of the projects, this is possible with virtualenv. The same case would be risky as you could "break" the system that does not require the upgrade.

Other

A virtual environment works in conjunction with a package manager. You can perfectly work with the virtualenv and the pip. However, there are other solutions such as pipenv and the Poetry

The purpose of these two tools is to manage both the creation of the virtual environment and the installation of libraries. It is worth taking a look.

Another tool is the pyenv which allows the installation of multiple versions of Python on the operating system and, with a simple configuration, choose which one you want to work with.

In time

In Python 3.3 and above you create the virtual environment with:

python -m venv /caminho/para/o/ambiente/virtual

In previous versions

pip install virtualenv
virtualenv /caminho/para/o/ambiente/virtual

I hope I’ve helped.

  • 3

    Only an addendum for those who use Ubuntu, the module venv shall be installed separately with apt install python3-venv or for specific versions such as python3.7-venv, python3.8-venv, etc....

1

virtualenv serves to create and manage virtual environments on your machine, virtual environments are isolated environments for you to develop your project, it will create a local and lean copy of the python version you are using and will install only in this copy the versions of the libraries you use in your project.

It is advisable to use virtual environments to develop, because if someone else or even you are developing the same project on another machine, you will have access to the exact and compatible versions of python and the modules you are using in your project, avoiding incompatibilities.

You are not required to work with virtual environments, but it is recommended, you also find most projects out there using virtual environments.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.