A Step-by-Step Guide to Creating Python Virtual Environments
A comprehensive guide outlining the essential steps for setting up isolated virtual environments in Python, covering installation, activation, and dependency management.
25 July 2025 · 3 min read
Why Virtual Environments Matter
If you have ever worked on more than one Python project at the same time, you have probably run into dependency conflicts. Project A needs version 1.2 of a library, but Project B needs version 2.0, and they cannot coexist peacefully in the same Python installation.
Virtual environments solve this problem by creating isolated spaces where each project can have its own dependencies, independent of every other project on your machine.
What is a Virtual Environment?
A virtual environment is a self-contained directory that contains a Python installation for a particular version of Python, plus a number of additional packages. When you activate a virtual environment, your shell uses the Python interpreter and packages from that directory instead of the system-wide installation.
Think of it as giving each project its own private Python installation.
Creating a Virtual Environment
Python 3 comes with venv built in, so you do not need to install anything extra. Open your terminal and navigate to your project directory:
python3 -m venv .venv
This creates a .venv directory in your project folder containing the virtual environment. I prefer .venv as a naming convention because the leading dot keeps it hidden in Unix-based file explorers, and most .gitignore templates already exclude it.
Activating the Environment
The activation command depends on your operating system and shell:
macOS and Linux (bash/zsh):
source .venv/bin/activate
Windows (PowerShell):
.venv\Scripts\Activate.ps1
Windows (Command Prompt):
.venv\Scripts\activate.bat
Once activated, you will see the environment name in your terminal prompt, usually as (.venv) at the beginning of the line.
Installing Packages
With the environment active, any packages you install with pip will go into the virtual environment rather than the system Python:
pip install requests flask pandas
To save your dependencies for reproducibility:
pip freeze > requirements.txt
And to install from a requirements file on another machine or after a fresh clone:
pip install -r requirements.txt
Deactivating
When you are done working on the project, simply run:
deactivate
Your terminal will return to using the system Python installation.
Best Practices
- Always use virtual environments for every Python project, no matter how small
- Add
.venv/to your.gitignoreto keep the environment out of version control - Keep
requirements.txtupdated and commit it to your repository - Use descriptive names if you maintain multiple environments, though
.venvworks well for single-environment projects - Consider
pip-toolsfor more sophisticated dependency management with pinned versions
Alternatives
While venv is the standard library solution, there are alternatives worth knowing about:
virtualenv: Third-party tool that predatesvenvand offers additional featuresconda: Popular in data science, manages both Python packages and system-level dependenciespoetry: Modern dependency management that handles virtual environments automaticallypipenv: Combinespipandvirtualenvinto a single workflow
Each has its place, but for most projects, the built-in venv module is all you need.
Conclusion
Virtual environments are a fundamental part of Python development. They take seconds to set up and save hours of debugging dependency conflicts. If you are not using them already, start today. Your future self will thank you.
Related paths
Continue into the parts of the site most closely connected to this article.