What is virtualenv for in Python?

What is virtualenv for in Python?

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

Author: Marco Souza, 2020-11-17

2 answers

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 libraries required for your system would be installed in the operating system environment.

Scenario

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 they hire you to set up 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 pygame, however, the version of Python chosen is 3.8.1

A friend asked you to do a web scraping and you decide to test Python 3.9. In addition, you will use the libraries requests and bs4 (Beautifulsale]}

Questions

If you do not use virtualenv for each project, you would have to use Python installed on your system and you would have to put all libraries in it. This would cause a library management problem.

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

Advantage

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 need the update.

Other

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

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

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

In time

In Python 3.3 and above vc creates 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 helped.

 8
Author: Paulo Marques, 2020-11-18 17:42:11

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 version of python you are using and will install only in that copy the versions of the libraries you use in your project.

It is quite advisable to use virtual environments to develop, because in case another person 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 the recommended one, you will also find most projects out there using virtual environments.

 1
Author: Joao Roberto Mendes, 2020-11-17 21:19:15