Python development setup

Home
Author: avolent
Updated on: November 2024
/programming/python development setup

Summary

Modern Python development setup using UV - an extremely fast Python package installer and resolver written in Rust. UV replaces pip, pip-tools, pipenv, poetry, pyenv, and virtualenv with a single tool that’s 10-100x faster.

Assumptions

Contents

  1. Why UV?
  2. Installation
  3. Basic Usage
  4. Project Setup
  5. Python Version Management
  6. Useful Links

Why UV?

UV is a next-generation Python package manager that combines the functionality of multiple tools:

Installation

macOS and Linux

curl -LsSf https://astral.sh/uv/install.sh | sh

Windows

powershell -c "irm https://astral.sh/uv/install.ps1 | iex"

Using Homebrew (macOS/Linux)

brew install uv

Verify Installation

uv --version

Basic Usage

Create a New Project

# Create a new project with virtual environment
uv init my-project
cd my-project

# Or initialize in existing directory
uv init

Install Dependencies

# Install packages (automatically creates venv if needed)
uv add requests pandas numpy

# Install dev dependencies
uv add --dev pytest black ruff

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

Run Commands in Virtual Environment

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

# Or run commands directly without activation
uv run python script.py
uv run pytest

Project Setup

Initialize a New Project

# Create new project
uv init my-api
cd my-api

# Add dependencies
uv add fastapi uvicorn

# Add development tools
uv add --dev pytest ruff black mypy

# Create a simple app
cat > main.py << 'EOF'
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}
EOF

# Run the app
uv run uvicorn main:app --reload

Lock Dependencies

# Generate lock file for reproducible installs
uv lock

# Install from lock file
uv sync

Python Version Management

UV can install and manage Python versions automatically:

# Install a specific Python version
uv python install 3.12

# List available Python versions
uv python list

# Use specific Python version for project
uv python pin 3.12

# This creates a .python-version file
# UV will automatically use this version

Using Specific Python Version

# Create venv with specific Python version
uv venv --python 3.12

# Or specify when running
uv run --python 3.11 python script.py

Common Workflows

Development Workflow

# 1. Clone project
git clone https://github.com/username/project
cd project

# 2. Install dependencies (UV auto-creates venv)
uv sync

# 3. Run tests
uv run pytest

# 4. Format code
uv run black .
uv run ruff check --fix .

# 5. Add new dependency
uv add new-package

Update Dependencies

# Update all packages
uv lock --upgrade

# Update specific package
uv lock --upgrade-package requests

# Sync to updated versions
uv sync

Useful Commands

# Show installed packages
uv pip list

# Show dependency tree
uv tree

# Remove package
uv remove package-name

# Export requirements
uv pip freeze > requirements.txt

# Clean cache
uv cache clean

Migration from Other Tools

From pip + venv

# Before:
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

# After:
uv venv
uv pip install -r requirements.txt
# or simply:
uv sync

From Poetry

# UV can read pyproject.toml
uv sync

From Pipenv

# Convert Pipfile to requirements
# Then use UV
uv pip install -r requirements.txt

References

CSS is from Latex.css | Wiki built by avolent.io | Repository located on Github

Return to top