Python UV Setup

Initialize Python projects with uv, a fast package manager and installer

Python UV Setup

uv is a fast Python package installer and resolver, written in Rust. It's designed to be a drop-in replacement for pip and virtualenv with significantly better performance.

Install UV
Choose your operating system to get the installation command

Install with Homebrew

brew install uv

Alternative: Install with curl

curl -LsSf https://astral.sh/uv/install.sh | sh
Project Setup
Generate commands to set up a new Python project with uv

Project Setup Commands

Run these commands to set up your project

# Create project directory
mkdir my_project
cd my_project

# Initialize virtual environment with uv
uv venv

# Initialize git repository
git init

# Create basic project structure
mkdir my_project
touch my_project/__init__.py

# Create requirements files
touch requirements.txt
touch requirements-dev.txt

# Activate virtual environment
source .venv/bin/activate  # On Unix/macOS
# OR
.venv\Scripts\activate  # On Windows

# Install basic dependencies
uv pip install -r requirements.txt

# Install development dependencies
uv pip install pytest pytest-cov
mkdir tests
touch tests/__init__.py
touch tests/test_my_project.py

# Install code quality tools
uv pip install ruff black
touch pyproject.toml

# Add basic pyproject.toml configuration
cat > pyproject.toml << EOL
[tool.black]
line-length = 88
target-version = ["py311"]

[tool.ruff]
line-length = 88
target-version = "py311"
select = ["E", "F", "I"]
EOL

# Create .gitignore
cat > .gitignore << EOL
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# Virtual environments
.venv/
venv/
ENV/

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

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
coverage.xml
*.cover
EOL

# Create README.md
echo "# my_project" > README.md
Common UV Commands
Reference for frequently used uv commands

Package Management Commands

Commands for managing dependencies with uv

# Install a package
uv pip install package_name

# Install a specific version
uv pip install package_name==1.0.0

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

# Install with extras
uv pip install package_name[extra]

# Install development dependencies
uv pip install -e .

# Uninstall a package
uv pip uninstall package_name

# List installed packages
uv pip list

# Generate requirements.txt from installed packages
uv pip freeze > requirements.txt

# Upgrade a package
uv pip install --upgrade package_name

# Install packages in development mode
uv pip install -e .
Why Use UV?

Speed

uv is significantly faster than pip, often 10-100x faster for package installation and resolution.

Compatibility

Works as a drop-in replacement for pip and virtualenv with the same command structure.

Reliability

Written in Rust with a focus on correctness and deterministic builds.

Modern Features

Supports modern Python packaging standards and provides improved dependency resolution.