This guide explains how virtual environments work in your Python learning setup.
Virtual environments are isolated Python installations that keep dependencies separate between projects. Each topic in your learning environment has its own .venv/ directory.
learn-python/
├── topics/
│ ├── fizzbuzz/
│ │ ├── .venv/ # Python 3.12 + pytest
│ │ ├── pyproject.toml
│ │ └── main.py
│ ├── data-science/
│ │ ├── .venv/ # Python 3.12 + pandas + numpy + matplotlib
│ │ ├── pyproject.toml
│ │ └── analysis.py
│ └── web-scraping/
│ ├── .venv/ # Python 3.12 + requests + beautifulsoup4
│ ├── pyproject.toml
│ └── scraper.py
- Each topic has completely separate packages
- No version conflicts between topics
- Clean, predictable environments
pyproject.tomlanduv.lockensure same versions everywhere- Easy to share exact environment with others
uv runautomatically uses the correct environment- No manual activation needed for most tasks
cd topics/any-topic/
uv run python script.py # ✅ Uses topic's .venv automatically
uv run pytest # ✅ Uses topic's .venv automatically
uv run pip list # ✅ Shows topic's packagescd topics/any-topic/
# Windows PowerShell
.venv\Scripts\Activate.ps1
# Linux/Mac
source .venv/bin/activate
# Now you're "inside" the virtual environment
python script.py # Uses topic's Python
pip list # Shows topic's packages
which python # Shows path to topic's Python
deactivate # Deactivate (return to system)# Create/update virtual environment
uv sync
# Add packages to current topic only
uv add requests beautifulsoup4
# Add development dependencies
uv add --dev pytest black mypy
# Remove packages
uv remove requests
# List installed packages
uv pip list
# Show dependency tree
uv tree
# Check environment info
uv python infocd topics/
uv init --app machine-learning --vcs none
cd machine-learning/
# Add ML-specific packages
uv add scikit-learn pandas numpy matplotlib
uv add --dev pytest jupyter
# Environment is ready!
uv run python train_model.pycd topics/data-science/
uv add pandas numpy matplotlib seaborn jupyter
uv sync
# Start Jupyter in this environment
uv run jupyter notebook
# Run analysis scripts
uv run python analyze_data.pycd topics/web-dev/
uv add flask requests jinja2
uv add --dev pytest black
# Run Flask app
uv run python app.py
# Run tests
uv run pytestcd topics/fizzbuzz/
uv pip list # Shows: pytest
cd ../data-science/
uv pip list # Shows: pandas, numpy, matplotlib, etc.
cd ../algorithms/
uv pip list # Shows: different packages# Recreate the virtual environment
rm -rf .venv/
uv sync# Check what's installed
uv pip list
# Update dependencies
uv sync
# Check dependency tree for conflicts
uv tree# Check Python version in current environment
uv run python --version
# Check available Python versions
uv python list
# Use specific Python version (creates new .python-version)
uv python pin 3.11
uv sync# Everything needed is in these files:
# - pyproject.toml (dependencies)
# - uv.lock (exact versions)
# - .python-version (Python version)
# Someone else can recreate exactly:
git clone your-repo
cd topics/some-topic/
uv sync # Recreates identical .venv/- Use
uv runfor most commands - Add packages with
uv add(notpip install) - Commit
pyproject.tomlanduv.lockto git - Use topic-specific dependencies
- Install packages globally with pip
- Manually mess with
.venv/contents - Copy
.venv/directories between machines - Mix pip and uv in same environment
Quick health check for any topic:
cd topics/your-topic/
# 1. Check if environment exists
ls .venv/ # Should show Python installation
# 2. Check Python version
uv run python --version # Should show expected version
# 3. Check installed packages
uv pip list # Should show your dependencies
# 4. Check for issues
uv sync # Should complete without errors
# 5. Test basic functionality
uv run python -c "import sys; print(sys.executable)"This setup ensures each topic has exactly the packages it needs, nothing more, nothing less!