Python Environment Managers
Why do you need an environment manager for Python?
- Version Control: Manage different Python and package versions across projects
- Ease of Deployment: Simplify the transition to production environments
- Avoiding Conflicts: Prevent package conflicts between different projects
Python Virtual Environment (venv)
The venv module supports creating lightweight "virtual environments".
Basic Usage
# Create new environment
python -m venv myenv
# Activate environment
# On Windows:
myenv\Scripts\activate
# On Unix/MacOS:
source myenv/bin/activate
# Deactivate environment
deactivate
# Remove the virtual environment (delete the folder)
rm -rf myenv
Features
- Built into Python standard library
- Lightweight and simple
- Supports only single Python version
Anaconda
Anaconda provides package, dependency, and environment management for any language.
Windows Setup
Add the following paths to system environment variables:
C:\ProgramData\anaconda3
C:\ProgramData\anaconda3\Library\bin
C:\ProgramData\anaconda3\Scripts
C:\ProgramData\anaconda3\Lib\site-packages
提示
Restart your PC after configuration.
Basic Commands
# Check Anaconda version
conda --version
# Update Anaconda
conda update conda
conda update anaconda
Disable Automatic Activation
To prevent Conda from being automatically activated as the default terminal environment when you open a terminal:
conda config --set auto_activate_base false
Environment Management
# Create new environment
conda create --name myenv python=3.11
# Activate environment
conda activate myenv
# List all environments
conda env list
# Deactivate environment
conda deactivate
# Remove an environment
conda remove --name myenv --all
Package Management
# Install packages
conda install package_name
# Update a package
conda update package_name
# Remove a package
conda remove package_name
# List all installed packages
conda list
# Search for a package
conda search package_name
Environment Export and Import
# Export an environment
conda env export > environment.yml
# Create an environment from a file
conda env create -f environment.yml
uv
uv is an extremely fast Python package and project manager, written in Rust.
Usage Steps
- Install uv
- Create a new project
- Create a new environment in the project folder
- Install the required packages
- Add the package dependencies
- Run program with uv
Install & Using uv
# Install uv
pip install uv
Project Management
# Create new project
uv init new_project
cd new_project
# Create new environment
uv venv
# Run a script
uv run hello.py
Python Management
# Install multiple Python versions
uv python install 3.10 3.11 3.12
# Set a Python version in the current directory
uv python pin 3.11
# List all python versions
uv python list
Install Packages
# Install packages (much faster than pip)
uv pip install numpy pandas
# Install from requirements.txt
uv pip install -r requirements.txt
Managing Package Dependencies
# Specify a version constraint
uv add 'package_name==2.31.0'
# Add a git dependency
uv add git+https://github.com/package_link
# Remove a package
uv remove package_name
# Upgrade a package
uv lock --upgrade-package package_name
Comparison
Feature | venv | conda | uv |
---|---|---|---|
Purpose | Basic virtual environments | Complete environment management | Fast package management |
Speed | ★★ | ★ | ★★★ |
Key Strength | Built into Python | Handles all dependencies | Ultra-fast installations |
Package Source | PyPI only | Conda + PyPI | PyPI only |
Multiple Python Versions | No | Yes | Yes |
Main Use Case | Single Python projects | Data Science & Scientific Computing | Modern Python development |
Recommended Use Cases
Choose venv when:
- Working on simple Python projects
- Standard library is sufficient
- Need lightweight environment
Choose Anaconda when:
- Working on data science projects
- Need scientific computing libraries
- Dealing with complex dependencies
- Using other languages like R, Julia
Choose uv when:
- Modern Python development
- Fast package installation is important
- Managing multiple Python versions
- Project-oriented development