Why Virtual Environments Are Needed?¶
Imagine you’re developing two Python projects simultaneously: one requires Django 2.2, and the other needs Django 4.0. If you install them in the same Python environment, Django 4.0 will overwrite Django 2.2, rendering the older project unrunnable. This is where virtual environments come in—they allow you to create isolated Python environments for each project, preventing dependency conflicts.
Additionally, virtual environments prevent system-wide Python pollution, keep projects clean, and simplify team collaboration (just share the dependency file to quickly replicate the development environment).
What is venv?¶
Starting from Python 3.3, Python includes a built-in module called venv specifically for creating virtual environments. It requires no extra installation and can be used directly with Python’s built-in tools, making it beginner-friendly.
Installing venv?¶
If your Python version is 3.3 or higher (most modern Python 3.9+ versions are compatible), venv is pre-installed and doesn’t need additional downloading. Verify your Python version with:
python --version # or python3 --version (for Mac/Linux)
If your Python version is too old, consider upgrading (upgrading won’t affect other system environments).
Creating a Virtual Environment¶
-
Open your terminal:
- Windows: Command Prompt or PowerShell
- Mac/Linux: Terminal -
Navigate to your project directory using
cd:
cd Documents/my_web_project # Replace with your project path
- Create the virtual environment (named
venv; the name can be customized, butvenvis recommended):
python -m venv venv
After execution, a venv folder will appear in your project directory, containing the isolated Python interpreter and dependency tools.
Activating the Virtual Environment¶
Activation is required to use the environment’s Python and dependencies. Commands vary by system:
- Windows (Command Prompt):
venv\Scripts\activate.bat
- Windows (PowerShell):
.\venv\Scripts\Activate.ps1
Note: If you get a “file cannot be loaded” error, run this first:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Type Y to confirm, then re-run the activation command.
- Mac/Linux:
source venv/bin/activate # Or simplify to: . venv/bin/activate
Once activated, your terminal prompt will show (venv), indicating the environment is active.
Verifying the Virtual Environment¶
Check if Python and pip belong to the virtual environment:
python --version # Should show the virtual environment's Python version (e.g., 3.9.7)
pip --version # Should show pip path within venv (e.g., venv/bin/pip)
Installing Dependencies¶
With the environment active, use pip to install packages (e.g., Flask):
pip install flask
Installed packages only exist in the virtual environment and won’t affect the system-wide Python.
Exporting Dependencies to a File¶
Save installed packages to requirements.txt for sharing/deployment:
pip freeze > requirements.txt
This file records all installed packages and their versions (e.g., flask==2.3.3).
Installing Dependencies (New Environment/Team Project)¶
For existing projects, install dependencies from requirements.txt after activating the environment:
pip install -r requirements.txt
Deactivating the Virtual Environment¶
When done, exit the environment with:
deactivate
The (venv) prefix in the terminal prompt will disappear, returning to the system-wide environment.
Deleting the Virtual Environment¶
To remove the virtual environment, simply delete the venv folder:
- Mac/Linux: rm -rf venv
- Windows: Manually delete the venv folder
Summary¶
The venv virtual environment is a essential tool for Python development, enabling isolation of project dependencies and avoiding version conflicts. Mastering venv (creation, activation, dependency management) ensures a secure and efficient development process. Now you can install frameworks like Django or Flask in your virtual environment and start building web projects!