18. Python Package & Environment Management#
1. uv — Overview#
- Fast modern Python package and project manager
- Written in Rust → 10-100x faster than pip
- Replaces: pip + venv + pyenv combined
- Created by Astral (makers of Ruff linter)
2. uv — Correct Command Sequence — Exam Answer:#
# ✅ Correct sequence (exam answer May_FN Q359)
uv init # Step 1: initialize project
uv python install 3.11 # Step 2: install specific Python version
uv add pandas numpy # Step 3: add dependencies
Wrong Sequences:#
❌ uv init python 3.11 → uv add pandas numpy
❌ uv create --version 3.11 → uv install pandas numpy
❌ uv new --python=3.11 → uv sync pandas numpy
3. uv — Complete Command Reference:#
# Project
uv init # create new project
uv init my-project # create in new folder
# Python versions
uv python install 3.11 # install Python 3.11
uv python list # list available versions
uv python pin 3.11 # pin version for project
# Dependencies
uv add pandas # add package
uv add pandas==2.1.0 # specific version
uv add pandas numpy scipy # multiple packages
uv remove pandas # remove package
uv sync # install from lockfile
# Running
uv run script.py # run in project environment
uv run jupyter notebook # run Jupyter
# Virtual environment
uv venv # create venv
uv venv --python 3.11 # with specific Python
# pip compatibility
uv pip install pandas
uv pip install -r requirements.txt
4. pip — Traditional Package Manager:#
pip install pandas # install
pip install pandas==2.1.0 # specific version
pip install -r requirements.txt # from requirements file
pip uninstall pandas # remove
pip list # list installed packages
pip freeze > requirements.txt # save current environment
pip show pandas # package details
pip install --upgrade pandas # upgrade package
pip install --no-cache-dir pandas # skip cache
5. venv — Built-in Virtual Environment:#
# Create
python -m venv .venv
# Activate
source .venv/bin/activate # Mac/Linux
.venv\Scripts\activate # Windows
.venv\Scripts\Activate.ps1 # Windows PowerShell
# Deactivate
deactivate
# Delete
rm -rf .venv # Mac/Linux
rmdir /s .venv # Windows
360a. conda — Data Science Package Manager:#
# Create environment
conda create -n myenv python=3.11
# Activate
conda activate myenv
# Deactivate
conda deactivate
# Install packages
conda install pandas numpy
# List environments
conda env list
# Export environment
conda env export > environment.yml
# Create from file
conda env create -f environment.yml
# Remove environment
conda env remove -n myenv
conda vs pip:#
| Feature | pip | conda |
|---|
| Package source | PyPI | Anaconda + PyPI |
| Environment mgmt | Needs venv | Built-in |
| Non-Python packages | ❌ | ✅ (C libraries etc.) |
| Data science focused | ❌ | ✅ |
| Speed | Medium | Slower |
6. requirements.txt — Version Pinning:#
# ✅ Exact version (fully reproducible)
pandas==2.1.0
numpy==1.24.0
scikit-learn==1.3.0
matplotlib==3.7.0
# ✅ Allow patches (minor updates)
pandas>=2.0.0,<3.0.0
# ❌ Unpinned (unpredictable)
pandas
numpy
Generate requirements.txt:#
pip freeze > requirements.txt # current environment
pip install -r requirements.txt # install from file
7. python-dotenv — Load .env Files:#
pip install python-dotenv
from dotenv import load_dotenv
import os
load_dotenv() # loads .env file
api_key = os.getenv('API_KEY') # read variable
db_url = os.getenv('DATABASE_URL')
# .env file (NEVER commit to git!)
API_KEY=your_key_here
DATABASE_URL=postgresql://user:pass@localhost/db
DEBUG=false
SECRET_KEY=supersecretkey
# .gitignore — always exclude
.env
.env.*
*.env
| Task | Traditional | uv | conda |
|---|
| Create venv | python -m venv | uv venv | conda create |
| Install package | pip install | uv add | conda install |
| Install Python | pyenv install | uv python install | Built-in |
| From requirements | pip install -r | uv sync | conda env create |
| Speed | Medium | ✅ Fastest | Slowest |
| Data science | ❌ | ❌ | ✅ |
Why Virtual Environments?#
Without venv:
Project A needs pandas==1.5.0
Project B needs pandas==2.1.0
→ Conflict! Can only have one globally
With venv:
Project A → its own environment → pandas==1.5.0
Project B → its own environment → pandas==2.1.0
→ No conflict ✅
Quick Reference#
uv sequence (exam answer):
uv init → uv python install 3.11 → uv add pandas ✅
uv commands:
uv add → add dependency
uv remove → remove dependency
uv sync → install from lockfile
uv run → run in project environment
pip commands:
pip install package==version
pip freeze > requirements.txt
pip install -r requirements.txt
venv:
python -m venv .venv
source .venv/bin/activate (Mac/Linux)
.venv\Scripts\activate (Windows)
conda:
conda create -n env python=3.11
conda activate env
conda install package
.env file:
✅ Store secrets here
✅ Load with python-dotenv
✅ Always add to .gitignore
❌ Never commit to git
requirements.txt:
pandas==2.1.0 ✅ pinned
pandas ❌ unpinned