A comprehensive guide to creating and using Python virtual environments. This tutorial covers everything from setup to managing dependencies.
- What is a Virtual Environment?
- Why Use Virtual Environments?
- Prerequisites
- Creating a Virtual Environment
- Activating the Virtual Environment
- Installing Packages
- Managing Dependencies
- Deactivating the Virtual Environment
- Common Commands
- Best Practices
- Example Projects
- Troubleshooting
A virtual environment is an isolated Python environment on your machine where you can install packages and dependencies without affecting your system-wide Python installation. Each virtual environment has its own Python interpreter and set of libraries.
- Isolation: Package versions in one project don't affect other projects
- Consistency: Ensures your project works the same on different machines
- Clean System: Keeps your system Python clean and uncluttered
- Easy Cleanup: Simply delete the folder to remove everything
Imagine you have two projects:
- Project A requires Django 3.0
- Project B requires Django 4.0
Without virtual environments, you can't have both versions installed simultaneously. Virtual environments solve this by creating isolated spaces for each project.
You'll need:
- Python 3.3+ installed on your system
- Terminal/Command Prompt access
- Basic command-line knowledge
python --versionOr on some systems:
python3 --versionpython -m venv venvpython3 -m venv venvExplanation:
python -m venv: Runs the venv modulevenv: The name of your virtual environment folder (you can use any name)
The command creates a folder structure like this:
venv/
├── Include/
├── Lib/
├── Scripts/ (Windows) or bin/ (macOS/Linux)
└── pyvenv.cfg
venv\Scripts\activatevenv\Scripts\Activate.ps1Note: If you get an execution policy error on PowerShell, run:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUsersource venv/bin/activateYour terminal prompt will change:
(venv) C:\Users\yourname\project>
The (venv) prefix indicates your virtual environment is active.
Once your virtual environment is activated, install packages using pip:
pip install package-nameInstall single package:
pip install requestsInstall specific version:
pip install Django==4.0Install multiple packages:
pip install requests Flask numpypip listThis shows all installed packages in your virtual environment.
Save all your project dependencies to a file:
pip freeze > requirements.txtDjango==4.0
requests==2.28.0
numpy==1.23.0
pandas==1.4.0
When setting up the project on another machine:
pip install -r requirements.txtThis ensures everyone uses the exact same versions.
Simply type:
deactivateYour terminal prompt will return to normal, and you'll use the system Python again.
C:\Users\yourname\project>
| Command | Description |
|---|---|
python -m venv venv |
Create a virtual environment |
venv\Scripts\activate (Windows) |
Activate on Windows |
source venv/bin/activate (Mac/Linux) |
Activate on Mac/Linux |
deactivate |
Deactivate virtual environment |
pip install package |
Install a package |
pip install -r requirements.txt |
Install from requirements file |
pip list |
List installed packages |
pip freeze > requirements.txt |
Export dependencies |
pip uninstall package |
Remove a package |
pip show package |
Show package info |
Every Python project should have its own virtual environment.
Never commit the venv/ folder to version control:
# .gitignore
venv/
__pycache__/
*.pyc
*.egg-info/
Always track dependencies:
pip freeze > requirements.txtAlways activate your virtual environment before installing packages or running code.
While you can name it anything, venv or env are common conventions.
Specify the Python version needed in your README:
Python 3.9.0 or higher
This repository includes example projects in the examples/ directory:
A basic script demonstrating package usage.
A Flask web application example.
Using pandas and numpy for data processing.
Solution:
- On macOS/Linux, try
python3instead ofpython - Ensure Python is in your system PATH
- Check installation:
python --version
Solution:
- Ensure you're in the correct directory
- Use the full command with correct path
- Try
python -m venv --helpfor more info
Solution:
- Verify virtual environment is activated (look for
(venv)in prompt) - Use
pip listto confirm installation - Try restarting your terminal
Solution:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUserSolution:
Simply delete the venv/ folder:
# Windows
rmdir /s venv
# macOS/Linux
rm -rf venvContributions are welcome! Feel free to:
- Report issues
- Suggest improvements
- Add more examples
- Fix typos
This tutorial is provided as-is for educational purposes.
Happy Coding! 🐍