Introduction
When building Python projects, managing dependencies correctly is very important. Without proper control, your system can become messy with conflicting package versions, leading to your project breaking. That is why Python developers use virtual environments — isolated spaces where each project can have its own packages, versions, and dependencies. In this article, you will learn in simple words how to create virtual environments, install packages, manage dependencies, and keep your Python projects clean and stable.
What Is a Virtual Environment in Python?
A virtual environment is a separate workspace for each Python project. It lets you install specific libraries without affecting other projects or your system Python.
Why You Need It
Avoid version conflicts between projects
Keep each project independent
Prevent system-level Python from breaking
Make deployment easier
Example
Project A needs Django 3
Project B needs Django 5
A virtual environment allows both to exist without conflict.
How to Create a Virtual Environment Using venv
Python comes with a built-in module called venv for creating environments.
Step 1: Create the Virtual Environment
python3 -m venv myenv
This creates a folder named myenv that contains your isolated Python environment.
Step 2: Activate the Environment
On Windows:
myenv\Scripts\activate
On macOS / Linux:
source myenv/bin/activate
Step 3: Deactivate
deactivate
How You Know It’s Activated
Your terminal will show:
(myenv) C:\Users\User>
Installing Dependencies Inside the Virtual Environment
Once the environment is activated, you can install packages using pip.
Example
pip install numpy pandas flask
These packages will be installed only inside the virtual environment.
Check Installed Packages
pip list
Managing Dependencies with requirements.txt
A requirements.txt file keeps track of all packages needed for a project.
Create requirements.txt Automatically
pip freeze > requirements.txt
This file stores exact versions of installed packages.
Install from requirements.txt
pip install -r requirements.txt
This ensures every user gets the same package versions.
Why This Is Important
Creating Multiple Virtual Environments for Different Projects
Each project should have its own environment.
Example
python -m venv projectA_env
python -m venv projectB_env
Switch environments by activating and deactivating them when needed.
Using pipx for Installing Global CLI Tools
Some tools like black, flake8, or pytest can be installed globally but isolated.
Example
pip install pipx
pipx install black
This keeps global tools separate from project environments.
Using Virtual Environment Wrapper Tools
Tools like virtualenvwrapper or conda make managing environments easier.
virtualenvwrapper example
mkvirtualenv myproject
workon myproject
Conda environment example
conda create -n myproject python=3.10
conda activate myproject
Why Use These Tools?
Easy activation and switching
Better for data science environments
More control over Python versions
Best Practices for Managing Python Dependencies
Always create a virtual environment for every project
Use requirements.txt or pip-tools for version control
Avoid installing packages globally
Document environment setup in README
Use .env files for environment variables
Keep Python versions consistent across projects
Regularly upgrade outdated dependencies
Using pip-tools for Advanced Dependency Management
pip-tools helps manage pinned and unpinned dependency versions.
Install pip-tools
pip install pip-tools
Use requirements.in
Create a file:
numpy
pandas
flask
Then compile:
pip-compile
This generates a full requirements.txt with locked versions.
Conclusion
Creating virtual environments and managing dependencies correctly is essential for every Python developer. Using tools like venv, pip, pip-tools, and conda, you can prevent version conflicts, maintain clean project structures, and ensure consistent behavior across machines. With these simple techniques, your Python projects will become easier to manage, maintain, and deploy — whether you're working alone or with a team.