Language: EN

que-son-los-entornos-virtuales-python

What are Virtual Environments in Python

A virtual environment is an isolated Python environment that allows us to have an independent copy of Python and its packages for each project.

This means that we can have different versions of Python and packages installed in each virtual environment, without interfering with other projects or the global Python system on our machine.

The goal is to avoid conflicts between installed libraries, or even due to changes in Python versions. For this reason, it is also very common when collaborating among several people or in Open Source projects.

What is a virtual environment?

A virtual environment is simply a folder with a specific structure that Python creates for us.

Virtual Environment
├── Include
├── Libs
│   └── site-packages
└── Scripts

In summary, this folder contains,

  • A specific Python interpreter (or a symbolic link to it)
  • Configuration files
  • A folder with the libraries we add to the environment
  • Scripts (which for example activate or deactivate the environment)

When we launch Python, and we have previously activated an environment in the same terminal session, Python knows that “it should not exit its environment”.

So, for practical purposes, it’s as if we have a “small independent installation of Python”. In other words, a virtual environment.

Creating a Virtual Environment

To create a new virtual environment, we can use the venv tool that comes included with Python 3.

First, we create a new directory for our project and move into it:

mkdir my_project
cd my_project

Then, we use venv to create the virtual environment. We specify the name of the folder that will contain the virtual environment (for example, venv):

On Windows systems:

python -m venv my_env

On Unix/Linux or MacOS systems:

python3 -m venv my_env

This will create a my_env folder in our project directory that will contain the virtual environment.

Activating the Virtual Environment

Once the virtual environment is created, we need to activate it to start using it. To do this, we need to call a small script called activate that is saved in the Scripts folder of the environment.

On Windows systems:

my_env\Scripts\activate

On Unix/Linux or MacOS systems:

source my_env/bin/activate

When you activate the virtual environment, you will see that your terminal prompt changes to indicate that you are in the virtual environment.

(my_env) C:\path_to_my_project\my_env >

Deactivating the Virtual Environment

When we have finished working on our project and want to exit the virtual environment, we simply execute:

deactivate

This will return us to the global Python environment of our system.

Generally, what we will do is close the terminal window where we activated the virtual environment, and that’s it.

Installing Packages in the Virtual Environment

Once the virtual environment is activated, we can use pip to install specific packages within this environment without affecting the global Python system.

pip install requests

This will install the requests package inside the virtual environment venv.

Using Virtual Environments with Visual Studio Code

If you are using Visual Studio Code as your development environment, you can also select a virtual environment for your project. In fact, it’s very easy.

On one hand, we can directly create a virtual environment. To do this, in the command palette (Ctrl + Shift + P) we type “Python: Create Environment”, and follow the instructions it gives us. vscode-python-enviroments-1

If we already have a virtual environment and want to activate it, in the command palette we type “Python: Select Interpreter”. A dropdown will appear with the different interpreters that VSCode has detected, including the virtual environment we have open.

vscode-python-enviroments-2

In the bottom bar of VSCode, we can verify which interpreter we have activated.

vscode-python-enviroments-3

Also, if we open a terminal console (Ctrl + Shift + ñ) we will see

When we want to stop using the virtual environment, we go back to “Python: Select Interpreter” and select another environment, or the global installation.