venv

Version ManagementPythonVirtual EnvironmentStandard LibraryLightweight

Language Version Management Tool

venv

Overview

venv is a virtual environment creation tool that comes standard with Python 3.3+. It enables creating isolated package environments for each Python project, preventing dependency conflicts. As part of the standard library, it requires no additional installation and provides lightweight, simple virtual environment management.

Details

Key Features

  • Built-in Standard: Included with Python 3.3+ requiring no additional installation
  • Lightweight: Simple design focused on essential functionality
  • Project Isolation: Independent package environments per project
  • Clean Environment: Safe development without contaminating system Python
  • Consistency: Reproducible dependency management with pip freeze/install
  • Cross-platform: Identical operations across Windows, macOS, and Linux

How It Works

venv creates an isolated Python environment in a specified directory, placing dedicated Python interpreter and pip. When activated, it modifies the PATH environment variable to prioritize that environment's Python.

Relationship with Other Tools

  • virtualenv: Third-party predecessor tool to venv
  • conda: More feature-rich but heavyweight environment management
  • pipenv: Project management tool integrating pip + venv
  • poetry: Modern tool specialized in dependency management

Pros and Cons

Pros

  • Built-in Standard: No additional installation or setup required
  • Lightweight & Fast: Minimal functionality for rapid environment creation
  • Simple: Low learning curve and easy to understand
  • Safety: Minimal impact on system environment
  • Reproducibility: Complete environment reproduction via requirements.txt
  • Official Support: Long-term support from Python core development team

Cons

  • Basic Features Only: Limited advanced functionality compared to other tools
  • Fixed Python Version: Depends on Python version at creation time
  • No Project Management: Lacks dependency resolution or lock functionality
  • No Auto-activation: No automatic switching when changing directories
  • Language Limited: Only supports Python, not other languages

Reference Pages

Usage Examples

Basic Virtual Environment Creation and Usage

# Create virtual environment (in .venv directory)
python -m venv .venv

# Activate virtual environment (Linux/macOS)
source .venv/bin/activate

# Activate virtual environment (Windows)
.venv\Scripts\activate.bat

# Verify virtual environment is activated
which python  # or where python (Windows)
python --version

Project-specific Environment Management

# Create project directory
mkdir my-python-project
cd my-python-project

# Create project-specific virtual environment
python -m venv venv

# Activate virtual environment
source venv/bin/activate  # Linux/macOS
# or
venv\Scripts\activate     # Windows

# Install required packages
pip install requests flask pandas

# Save dependencies to file
pip freeze > requirements.txt

# Deactivate virtual environment
deactivate

Dependency Management and Reproduction

# Reproduce existing project environment
git clone https://github.com/example/python-project.git
cd python-project

# Create new virtual environment
python -m venv .venv
source .venv/bin/activate

# Install dependencies from requirements.txt
pip install -r requirements.txt

# Install development dependencies if available
pip install -r requirements-dev.txt

# Install with specific version constraints
pip install "django>=4.0,<5.0"
pip install pytest==7.4.3

Advanced Configuration Options

# Create environment with access to system packages
python -m venv --system-site-packages myenv

# Create without pip, don't run ensurepip
python -m venv --without-pip myenv

# Use symbolic links (Linux/macOS)
python -m venv --symlinks myenv

# Set custom prompt name
python -m venv --prompt "MyProject" myenv

# Clear existing virtual environment directory
python -m venv --clear existing-venv

Usage with PowerShell (Windows)

# Set execution policy (first time only)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

# Create virtual environment
python -m venv venv

# Activate virtual environment
venv\Scripts\Activate.ps1

# Verify environment
Get-Command python
python --version

# Deactivate virtual environment
deactivate

Usage in CI/CD Environment

# GitHub Actions example
name: Python Tests
on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: [3.9, 3.10, 3.11, 3.12]
    
    steps:
    - uses: actions/checkout@v4
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}
    
    - name: Create virtual environment
      run: python -m venv venv
    
    - name: Activate virtual environment
      run: source venv/bin/activate
    
    - name: Install dependencies
      run: |
        source venv/bin/activate
        pip install --upgrade pip
        pip install -r requirements.txt
        pip install -r requirements-test.txt
    
    - name: Run tests
      run: |
        source venv/bin/activate
        pytest tests/

Environment Cleanup and Removal

# Deactivate virtual environment
deactivate

# Remove virtual environment directory
rm -rf venv  # Linux/macOS
# or
rmdir /s venv  # Windows

# Add virtual environment to .gitignore
echo "venv/" >> .gitignore
echo ".venv/" >> .gitignore
echo "*.pyc" >> .gitignore
echo "__pycache__/" >> .gitignore