Learn why virtual environments are essential for Python development and how to create, activate, and manage them effectively.
Virtual environments are essential for Python development. They create isolated spaces for each project's dependencies, preventing conflicts and keeping your system clean. This guide covers everything from basic usage to advanced workflow tips.
Imagine you have two Django projects: one needs Django 4.2, the other needs Django 5.1. Without virtual environments, you can only have one version installed globally. Virtual environments solve this by giving each project its own isolated set of packages.
# Create a virtual environment
python -m venv myproject_env
# Or simply call it 'venv' (most common convention)
python -m venv venv
# Activate it
# On Linux/macOS:
source venv/bin/activate
# On Windows (CMD):
venv\Scripts\activate.bat
# On Windows (PowerShell):
venv\Scripts\Activate.ps1
# Your prompt changes to show the active environment:
(venv) $ python --version
# Install virtualenv
pip install virtualenv
# Create environment
virtualenv venv
# Create with specific Python version
virtualenv --python=python3.12 venv
# Install packages (only in this environment)
pip install django
pip install django==5.1 # Specific version
pip install "django>=5.0" # Minimum version
# See what's installed
pip list
pip freeze
# Save dependencies to a file
pip freeze > requirements.txt
# Install from requirements file
pip install -r requirements.txt
# Upgrade a package
pip install --upgrade django
# Uninstall
pip uninstall django
This file lists all your project's dependencies with exact versions:
# requirements.txt
Django==5.1
djangorestframework==3.15.2
psycopg2-binary==2.9.9
gunicorn==23.0.0
redis==5.0.8
Pillow==10.4.0
requirements/base.txt (shared), requirements/dev.txt (development tools), requirements/prod.txt (production only).
# Deactivate the current environment
deactivate
# Delete the environment (just remove the folder)
rm -rf venv/
Never commit your virtual environment to Git. Add it to .gitignore:
# .gitignore
venv/
env/
.venv/
*.pyc
__pycache__/
.env
requirements.txt, NOT the venv/ folder. Other developers recreate the environment using pip install -r requirements.txt.
# 1. Create project
mkdir my_django_project && cd my_django_project
# 2. Create & activate virtual environment
python -m venv venv
source venv/bin/activate
# 3. Install packages
pip install django djangorestframework
# 4. Save requirements
pip freeze > requirements.txt
# 5. Initialize git
git init
echo "venv/" >> .gitignore
# 6. Start Django project
django-admin startproject config .
# 7. When done working
deactivate
| Problem | Solution |
|---|---|
python: command not found | Try python3 instead of python |
pip installs globally | Make sure you activated the venv first |
PowerShell won't activate | Run: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser |
Permission denied on Linux | Use python3 -m venv venv, not sudo |
Module not found after install | Check you're in the right venv: which python |
Virtual environments are non-negotiable for Python development:
requirements.txt to track dependencies