que-son-los-entornos-virtuales-python

What are Virtual Environments in Python

  • 4 min

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 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 Python version changes. For this reason, it is also very common when collaborating between 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 have previously activated an environment in the same terminal session, Python knows that it “must not leave its environment”.

So, for practical purposes, it’s as if we had a “small independent Python installation” (that is, 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 mi_proyecto cd mi_proyecto

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):

python -m venv my_env
Copied!

python3 -m venv mi_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 have to call a small Script called activate which is stored in the environment’s Scripts folder.

my_env\Scripts\activate
Copied!

source mi_env/bin/activate

When you activate the virtual environment, you’ll see your terminal prompt change to indicate you are in the virtual environment.

(my_env) C:\path_to_my_project\my_env >
Copied!

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 venv virtual environment.

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 create a virtual environment directly. To do this, in the command palette (Ctrl + Shift + P) we type Python: Create Enviroment, 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 any open virtual environment.

vscode-python-enviroments-2

In the bottom bar of VSCode we can verify which interpreter is active.

vscode-python-enviroments-3

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

When we want to stop using the virtual environment, we use Python: Select Interpreter again and select another environment, or the global installation.