Master Python Virtual Environments: A Complete Guide to venv, virtualenv & conda
This article explains why Python virtual environments are essential, compares the three main tools (venv, virtualenv, conda), and provides step‑by‑step commands for creating, activating, managing dependencies, exporting requirements, and cleaning up environments, plus advanced tips and FAQs.
Python developers often face dependency conflicts when different projects require different library versions. This guide introduces virtual environments as an isolated workspace for each project, preventing version clashes, global pollution, permission issues, and ensuring reproducible setups.
Why use a virtual environment?
Imagine your computer as a shared kitchen where all projects use the same utensils and ingredients (the interpreter and third‑party libraries). A virtual environment gives each project its own kitchen with a dedicated Python interpreter, pip tool, library set, and environment variables.
Python interpreter
pip package manager
Third‑party library dependencies
Environment variable settings
It solves the following problems:
Dependency conflicts : different projects need different library versions.
Environment pollution : avoids installing excessive global packages.
Permission issues : no sudo required for package installation.
Project reproducibility : easily recreate the exact runtime environment.
Three mainstream tools comparison
venv (Python standard library, supports Python 3.3+): lightweight, simple, no extra installation; ideal for everyday Python 3 development.
virtualenv (third‑party, supports Python 2.7+): richer features, supports more Python versions; suited for projects that still need Python 2.
conda (Anaconda distribution, supports Python 2.7/3.3+): manages both Python and C libraries; best for data‑science and machine‑learning projects.
Recommendation: For most Python 3 projects, venv is the official, simplest, and most convenient choice.
Hands‑on tutorial: Using venv
1. Create a virtual environment
# Create a virtual environment named my_project_venv
python -m venv my_project_venvThis creates a my_project_venv directory containing an isolated Python environment.
2. Activate the virtual environment
Windows: my_project_venv\Scripts\activate.bat macOS/Linux: source my_project_venv/bin/activate After activation, the command prompt shows (my_project_venv), indicating the environment is active.
3. Operate inside the virtual environment
# Check Python version
python --version
# Install a specific package version
pip install django==2.2.0
# List installed packages
pip list
# Install development dependencies
pip install pytest flake84. Export the dependency list
# Export all dependencies to requirements.txt
pip freeze > requirements.txtThe generated requirements.txt might contain:
Django==2.2.0
pytest==6.2.4
flake8==3.9.25. Recreate the environment from requirements.txt
# Install all dependencies in a new environment
pip install -r requirements.txt6. Deactivate the virtual environment
deactivateTypical project workflow
Create environment: python -m venv venv Activate: source venv/bin/activate Install dependencies: pip install -r requirements.txt Develop and test within the isolated environment.
Export dependencies: pip freeze > requirements.txt Commit requirements.txt to version control; use it to reproduce the environment during deployment.
FAQ
Q: Where should the virtual‑environment folder be placed? Usually in the project root, named venv or .venv, and added to .gitignore to avoid committing it.
Q: How to delete a virtual environment? Simply remove the folder: rm -rf venv (Linux/macOS) or rd /s venv (Windows).
Q: Can multiple projects share the same virtual environment? Not recommended. Each project should have its own isolated environment to avoid conflicts.
Advanced tip: Using virtualenvwrapper
# Install virtualenvwrapper
pip install virtualenvwrapper
# Create a new environment
mkvirtualenv my_env
# List all environments
workon
# Switch to an environment
workon my_env
# Delete an environment
rmvirtualenv my_envSummary
✅ Resolves dependency conflicts.
✅ Keeps the global environment clean.
✅ Guarantees project reproducibility.
✅ Facilitates team collaboration and deployment.
Start creating a separate virtual environment for every Python project to become a professional Python developer.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
