Skip to content
beginner Phase 7 · Python Packages & Projects

Virtual Environments & Dependency Management

Manage dependencies with venv, pip, poetry, and requirements.txt.

45m
0 problems
Topic Progress 0%

venv Module

Creating Virtual Environments

# Create virtual environment
python -m venv myenv

# Create with specific Python version
python3.11 -m venv myenv

# Create in specific location
python -m venv /path/to/myenv

# With system site packages (access global packages)
python -m venv --system-site-packages myenv

Activating

# Windows
myenv\Scripts\activate

# macOS/Linux
source myenv/bin/activate

# You'll see (myenv) in prompt
(myenv) $ 

Deactivating

deactivate

Managing Packages

# Install packages
pip install requests

# Save dependencies
pip freeze > requirements.txt

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

# Upgrade all packages
pip list --outdated
pip install --upgrade <package>

Cleaning Up

# Remove virtual environment
# Windows
rmdir /s /q myenv

# macOS/Linux
rm -rf myenv

# Or just delete the directory

Modern Tools

pipenv

# Install pipenv
pip install pipenv

# Create environment and install package
pipenv install requests

# Install dev dependencies
pipenv install pytest --dev

# Activate shell
pipenv shell

# Run command in environment
pipenv run python script.py

# Generate requirements.txt
pipenv requirements > requirements.txt

Poetry

# Install poetry
pip install poetry

# Create new project
poetry new my-project

# Initialize in existing project
poetry init

# Add dependency
poetry add requests

# Add dev dependency
poetry add pytest --dev

# Install all dependencies
poetry install

# Run commands
poetry run python script.py

# Build package
poetry build

# Publish
poetry publish

pyenv (Python Version Management)

# Install pyenv
# macOS/Linux
curl https://pyenv.run | bash

# Install Python version
pyenv install 3.11.0

# Set global version
pyenv global 3.11.0

# Set local version (per project)
pyenv local 3.11.0

# List installed versions
pyenv versions

Tool Comparison

| Tool | Purpose | Best For |
|------|---------|----------|
| venv | Virtual environments | Simple projects |
| pipenv | Virtual env + dependency management | Medium projects |
| poetry | Package management + building | Publishing packages |
| pyenv | Python version management | Multiple Python versions |
| conda | Scientific computing | Data science |

Best Practices

Requirements Files

# requirements.txt - Production dependencies
requests==2.28.0
flask>=2.0,<3.0
python-dotenv==1.0.0

# requirements-dev.txt - Development dependencies
-r requirements.txt
pytest>=7.0
black>=22.0
mypy>=0.900
pre-commit>=2.0

Dependency Pinning

# ❌ Bad: Unpinned (non-reproducible)
requests
flask

# ✅ Good: Pinned (reproducible)
requests==2.28.0
flask==2.3.2

# ✅ Good: Range (flexible but safe)
requests>=2.28,<3.0
flask>=2.0,<3.0

.gitignore

# Virtual environments
myenv/
venv/
.env

# Python files
__pycache__/
*.pyc
*.pyo
*.pyd
.Python

# Distribution
dist/
build/
*.egg-info/

# IDE
.vscode/
.idea/
*.swp
*.swo

Common Issues

# Issue: Package not found
# Solution: Check if in correct environment
which python
pip list

# Issue: Version conflicts
# Solution: Use virtual environment
python -m venv fresh_env
source fresh_env/bin/activate
pip install -r requirements.txt

# Issue: Permission denied
# Solution: Use --user flag or virtual env
pip install --user package_name

# Issue: Outdated pip
# Solution: Upgrade pip
python -m pip install --upgrade pip

Project Setup Checklist

# 1. Create virtual environment
python -m venv venv

# 2. Activate
source venv/bin/activate  # or venv\Scripts\activate

# 3. Upgrade pip
python -m pip install --upgrade pip

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

# 5. Install dev dependencies
pip install -r requirements-dev.txt

# 6. Verify
pip list
python -c 'import my_package'

Reproducibility

# Generate exact requirements
pip freeze > requirements.txt

# Or using pip-compile (from pip-tools)
pip install pip-tools
pip-compile requirements.in  # Input file
pip install -r requirements.txt  # Compiled file

# Recreate environment
python -m venv new_env
source new_env/bin/activate
pip install -r requirements.txt