Python Virtual Environment (venv)
A virtual environment is an isolated Python runtime space with its own interpreter, installed packages, and configuration, completely isolated from the system global environment.
Python Virtual Environment is an independent Python runtime environment that allows you to create isolated Python environments for different projects on the same machine.
Different projects can run in parallel in their respective virtual environments without interfering with each other.
Each virtual environment has its own:
- Python interpreter
- Installed packages/libraries
- Environment variables
Virtual environments give each project an independent dependency space, completely eliminating version conflicts
Why Virtual Environments Are Needed
Section titled “Why Virtual Environments Are Needed”- Project Isolation: Different projects can use different versions of Python and third-party libraries
- Avoid Pollution: Installed packages only affect the current environment, not polluting the global Python
- Controllable Dependencies: Accurately record and reproduce the environment through
requirements.txt - Safe Testing: You can safely upgrade or try out new packages without affecting other projects
Scenario Example:
- Project A requires Django 3.2
- Project B requires Django 4.0
- If installed globally, the two versions will conflict
Virtual Environment Tools
Section titled “Virtual Environment Tools”| Tool Name | Type | Python Version Support | Installation Method | Features | Use Cases |
|---|---|---|---|---|---|
| venv (Recommended) | Built-in Module | ≥ 3.3 | No installation required, built-in | Lightweight, officially recommended, simple to use | General development, daily projects |
| virtualenv | Third-party Tool | 2.x and 3.x | pip install virtualenv |
Rich features, multi-version compatibility | Projects needing legacy version or advanced features |
| conda | Anaconda Built-in | 2.x and 3.x | Installed with Anaconda/Miniconda | Cross-language package management, data science ecosystem | Data science, machine learning projects |
If older version support is needed, you can use virtualenv (Python 2 compatible):
This chapter will use venv to create and manage virtual environments.
Creating a Virtual Environment
Section titled “Creating a Virtual Environment”Python 3.3+ has the venv module built-in, requiring no additional installation.
Usage Process
Section titled “Usage Process”Usage Process Overview
Section titled “Usage Process Overview”1 Create python -m venv 2 Activate activate 3 Install Dependencies pip install 4 Develop/Debug python / run 5 Deactivate deactivate
Check the Python version:
Create a virtual environment:
For example, create a virtual environment named .venv:
Example
Section titled “Example”Parameter Description:
-m venv: Use the venv module.venv: The name of the virtual environment (can be customized)
Directory Structure After Creation
Section titled “Directory Structure After Creation”Activating the Virtual Environment
Section titled “Activating the Virtual Environment”After activation, the python and pip commands in the current terminal will automatically point to the virtual environment, and all installation operations will be performed in the isolated space.
macOS / Linux
Section titled “macOS / Linux”Windows (CMD / PowerShell)
Section titled “Windows (CMD / PowerShell)”After successful activation, the command prompt usually displays the environment name:
Verify that activation is in effect:
Using the Virtual Environment
Section titled “Using the Virtual Environment”Installing Packages
Section titled “Installing Packages”In an activated environment, packages installed using pip will only affect the current environment:
For example:
Viewing Installed Packages
Section titled “Viewing Installed Packages”Exporting Dependencies
Section titled “Exporting Dependencies”Using requirements.txt to record and reproduce the project environment is a standard practice for team collaboration:
Example content of requirements.txt file:
Installing Dependencies from a File
Section titled “Installing Dependencies from a File”Tip: Add .venv/ to .gitignore and only commit requirements.txt. Virtual environments are large in size and path-bound, so they should not be included in version control.
Exiting the Virtual Environment
Section titled “Exiting the Virtual Environment”When you finish working, you can exit the virtual environment:
After exiting, the command prompt will return to normal, and Python and pip commands will use the system global version.
Deleting the Virtual Environment
Section titled “Deleting the Virtual Environment”To delete a virtual environment, simply delete the corresponding directory:
Deleting the Virtual Environment
Section titled “Deleting the Virtual Environment”A virtual environment is essentially a regular directory. Deleting the directory completely removes it:
macOS / Linux
Section titled “macOS / Linux”Windows
Section titled “Windows”Note: Before deleting, execute deactivate to exit the environment first; otherwise, the current Shell will retain invalid environment variables.
Real Project Example
Section titled “Real Project Example”Suppose you are developing a Django project:
Example
Section titled “Example”Advanced Usage
Section titled “Advanced Usage”Specifying Python Version
Section titled “Specifying Python Version”If you have multiple Python versions installed, you can specify which version to use to create the virtual environment:
Creating an Environment Without pip
Section titled “Creating an Environment Without pip”Creating a Virtual Environment That Inherits System Packages
Section titled “Creating a Virtual Environment That Inherits System Packages”Frequently Asked Questions
Section titled “Frequently Asked Questions”1. Why doesn’t my virtual environment have an activate script?
Section titled “1. Why doesn’t my virtual environment have an activate script?”Make sure you are using the correct path:
- Windows:
Scripts\activate - Unix/Linux:
bin/activate
2. How do I know if I’m currently in a virtual environment?
Section titled “2. How do I know if I’m currently in a virtual environment?”Check if the command prompt has an environment name prefix, or run:
3. Slow package installation
Section titled “3. Slow package installation”Use a domestic mirror source:
Check if the Python interpreter’s path is in the virtual environment directory.
4. Can the virtual environment be moved?
Section titled “4. Can the virtual environment be moved?”Not recommended. Scripts and configuration files within the virtual environment contain hardcoded absolute paths, which will become invalid after moving. If migration is needed, the easiest way is to recreate the environment and restore dependencies through requirements.txt.
5. How much space does a virtual environment take?
Section titled “5. How much space does a virtual environment take?”An empty environment is about 20–50 MB, increasing as more packages are installed. Data science projects (NumPy, Pandas, PyTorch) can reach several GB. Regularly cleaning up unused virtual environments is a good habit.
Best Practices
Section titled “Best Practices”- One environment per project: Avoid dependencies from different projects interfering with each other
- Unified naming as
.venv: Most IDEs (VS Code, PyCharm) can automatically recognize it - Update
requirements.txtpromptly: Sync and export after eachpip install - Ignore virtual environments in version control: Add
.venv/to.gitignore - Regularly clean up abandoned environments: Free up disk space
- Pin version numbers in production: Explicitly write
==versions inrequirements.txtto avoid accidental upgrades