When learning Python, many beginners focus solely on the language and its libraries while completely ignoring virtual environments. Consequently, managing Python projects can change into a multitude: dependencies installed for various projects could have conflicting versions, resulting in compatibility issues.
Even once I studied Python, no one emphasized the importance of virtual environments, which I now find very strange. They’re a particularly useful gizmo for isolating different projects from one another.
In this text, I’ll explain how virtual environments work, provide several examples, and share useful commands for managing them.
Problem
Imagine you may have two Python projects in your laptop, each situated in a special directory. You realize that it’s essential install the newest version of library A for the primary project. Later, you turn to the second project and try and install library B.
Here’s the issue: .
Because you haven’t used any tool for Dependency Management, all dependencies are installed globally in your computer. Because of the incompatible versions of library A, you encounter an error when attempting to install library B.
Solution
To forestall such issues, virtual environments are used. The concept is to allocate a separate cupboard space for every Python project. Each storage will contain all of the externally downloaded dependencies for a selected project in an isolated manner.
More specifically, if we download the identical library A for 2 projects inside their very own virtual environments, library A shall be downloaded twice — once for every environment. Furthermore, the versions of the library can differ between the environments because each environment is totally isolated and doesn’t interact with the others.
Now that the motivation behind using virtual environments is obvious, let’s explore find out how to create them in Python.
Virtual environments in Python
It is suggested to create a virtual environment in the basis directory of a project. An environment is created using the next command within the terminal:
python -m venv
By convention,  is frequently named venv, so the command becomes:
python -m venv venv
Consequently, this command creates a directory called , which accommodates the virtual environment itself. It’s even possible to go inside that directory, but usually, it is just not very useful, because the directory primarily accommodates system scripts that will not be intended for use directly.
To activate the virtual environment, use the next command:
source venv/bin/activate
Once the environment is activated, we are able to install dependencies for the project. So long as the is activated, any installed dependency will only belong to that environment.
To deactivate the virtual environment, type:
deactivate
Once the environment is deactivated, the terminal returns to its normal state. For instance, you’ll be able to switch to a different project and activate its environment there.
Dependency management
Installing libraries
Before installing any dependencies, it is suggested to activate a virtual environment to be sure that installed libraries belong to a single project. This helps avoid global version conflicts.
Essentially the most incessantly used command for dependency management is pip. In comparison with other alternatives,  is intuitive and straightforward to make use of.
To put in a library, type:
pip install
So, as an example, if we desired to download the newest version of pandas, we must always have typed:
pip install pandas
In some scenarios, we’d need to put in a selected version of a library.  provides a straightforward syntax to try this:
pip install pandas==2.1.4 # install pandas of version 2.1.4
pip install pandas>=2.1.4 # install pandas of version 2.1.4 or higher
pip install pandas<2.1.4 # install pandas of version less than 2.1.4
pip install pandas>=2.1.2,<2.2.4 # installs the newest version available between 2.1.2 and a pair of.2.4
Viewing dependency details
In case you are fascinated with a selected dependency that you may have installed, a straightforward approach to get more details about it's to make use of the pip show command:
pip show pandas
For instance, the command in the instance will output the next information:

Deleting dependency
To remove a dependency from a virtual environment, use the next command:
pip uninstall pandas
After executing this command, all files related to the desired library shall be deleted, thus freeing up disk space. Nonetheless, in the event you run a Python program that imports this library again, you'll encounter an ImportError.
File with requirements
A typical practice when managing dependencies is to create a  file that accommodates an inventory of all downloaded dependencies within the project together with their versions. Here is an example of what it would appear to be:
fastapi==0.115.5
pydantic==2.10.1
PyYAML==6.0.2
requests==2.32.3
scikit-learn==1.5.2
scipy==1.14.1
seaborn==0.13.2
streamlit==1.40.2
torch==2.5.1
torchvision==0.20.1
tornado==6.4.2
tqdm==4.67.1
urllib3==2.2.3
uvicorn==0.32.1
yolo==0.3.2
Ideally, each time you utilize the pip install command, you need to add a corresponding line to the  file to maintain track of all of the libraries utilized in the project.
Nonetheless, in the event you forget to try this, there continues to be another: the pip freeze command outputs all the installed dependencies within the project. Nevertheless, pip freeze will be quite verbose, often including many other library names which can be dependencies of the libraries you might be using within the project.
pip freeze > requirements.txt
At any time when you clone a Python project, it is predicted that a requirements.txt file is already present within the Git repository. To put in all of the dependencies listed on this file, you utilize the pip install command together with the -r flag followed by the necessities filename.
pip install -r requirements.txt
.gitignore
When working with version control systems, virtual environments should never be pushed to Git! As a substitute, they have to be mentioned in a .gitignore file.
Conclusion
In this text, we've checked out the very vital concept of virtual environments. By isolating downloaded dependencies for various projects, they permit for easier management of multiple Python Projects.
