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 installed packages 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 between multiple 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 (that 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 doesn’t have to leave its environment”.
So, for practical purposes, it’s like having a “small independent installation of Python” (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 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
):
python -m venv my_env
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 stored in the Scripts
folder of the environment.
my_env\Scripts\activate
source my_env/bin/activate
Upon activating 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 run:
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 within 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 directly create a virtual environment. To do this, in the command palette (Ctrl + Shift + P) we type Python: Create Enviroment, and follow the instructions it gives us.
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.
In the lower bar of VSCode, we can verify which interpreter we have activated.
Additionally, if we open a terminal console (Ctrl + Shift + ñ) we will see
When we want to stop using the virtual environment, we go back to using Python: Select Interpreter and select another environment or the global installation.