Loading...
Loading...
Initialize and configure new Python projects with uv, including creating projects, setting up pyproject.toml, managing dependency groups, and pinning Python versions. Use when starting new projects, configuring development environments, or standardizing project structure with uv. Covers `uv init`, `uv add`, `uv python pin`, and initial project scaffolding with proper dependency organization.
npx skill4agent add dawiddutoit/custom-claude uv-project-setup# Initialize project in current directory or new directory
uv init my-project
cd my-project
# Install dependencies in isolated environment
uv sync
# Start developing
uv run python main.pypyproject.toml.python-version.venv/main.py# Create new project directory and initialize
uv init my-project
cd my-project
# Or initialize in current directory
mkdir my-project && cd my-project
uv initpyproject.toml.python-versionREADME.mdmain.py.gitignore# Pin to specific Python version (creates .python-version)
uv python pin 3.12
# Pin to minor version range
uv python pin 3.12.3
# Verify pinning worked
cat .python-version.python-version# Add runtime dependencies
uv add requests httpx pandas
# Add exact versions
uv add "fastapi==0.104.0"
# Add version ranges (recommended)
uv add "django>=4.2,<5.0"
# Add with extras
uv add "pandas[excel,plot]"uv.lock# In pyproject.toml, define development groups under [dependency-groups]
[dependency-groups]
dev = [
"pytest>=8.0.0",
"pytest-cov>=4.1.0",
"black>=23.0.0",
]
lint = [
"ruff>=0.1.0",
"mypy>=1.7.0",
"pylint>=3.0.0",
]
docs = [
"mkdocs>=1.5.0",
"mkdocs-material>=9.0.0",
]# Install all groups
uv sync --all-groups
# Install only specific groups
uv sync --group dev --group lint
# Install without dev groups (production)
uv sync --no-devpyproject.toml[tool.uv][tool.uv]
# Use custom virtual environment location
# project-environment = ".venv"
# Define conflicting extras that can't be used together
conflicts = [
[{ extra = "cuda" }, { extra = "cpu" }],
]
# Support multiple platform-specific environments
environments = [
"sys_platform == 'darwin'",
"sys_platform == 'linux'",
"sys_platform == 'win32'",
][project]
name = "my-package"
version = "0.1.0"
dependencies = ["httpx>=0.27.0"]
[project.optional-dependencies]
# Extras for optional functionality
excel = ["openpyxl>=3.0.0"]
plot = ["matplotlib>=3.5.0"]
# Meta-extra for everything
all = ["openpyxl>=3.0.0", "matplotlib>=3.5.0"]
[project.scripts]
# CLI entry points
my-cli = "my_package.cli:main"
dev-server = "my_package.server:run"# Install with specific extras
pip install my-package[excel,plot]
# Install with all extras
pip install my-package[all]# View dependency tree
uv tree
# Run your project
uv run python main.py
# Create and activate environment manually (optional)
source .venv/bin/activate # Unix
.venv\Scripts\activate # Windows# Initialize project
uv init my-api
cd my-api
# Pin Python version
uv python pin 3.12
# Add web framework and utilities
uv add fastapi uvicorn sqlalchemy pydantic
# Add development dependencies
uv add --group dev pytest pytest-asyncio black ruff mypy
# Verify setup
uv tree
uv run pytest --collect-only# Create data science project
uv init data-analysis
cd data-analysis
# Pin to Python 3.12
uv python pin 3.12
# Add data science stack
uv add "pandas>=2.2.0" "numpy>=1.26.0" "scikit-learn>=1.3.0"
# Add Jupyter and visualization
uv add jupyter matplotlib seaborn plotly
# Add development tools
uv add --group dev pytest jupyter-contrib-nbextensions
# Organize into pyproject.toml groups[project]
name = "data-analysis"
version = "0.1.0"
description = "Data analysis pipeline"
requires-python = ">=3.12"
dependencies = [
"pandas>=2.2.0",
"numpy>=1.26.0",
"scikit-learn>=1.3.0",
"matplotlib>=3.5.0",
]
[dependency-groups]
jupyter = ["jupyter>=1.0.0"]
dev = ["pytest>=8.0.0", "black>=23.0.0"]# Initialize library project
uv init my-library
cd my-library
# Core dependencies only
uv add "requests>=2.31.0" "click>=8.1.0"
# Add testing framework
uv add --dev pytest pytest-cov
# Add linting/formatting
uv add --group lint ruff mypy black
# Add documentation
uv add --group artifacts mkdocs mkdocs-material
# Add optional features
uv add --group async aiohttp asyncio-contextmanager
uv add --group database sqlalchemy psycopg2-binary[project]
name = "my-library"
version = "0.1.0"
dependencies = ["requests>=2.31.0", "click>=8.1.0"]
[project.optional-dependencies]
async = ["aiohttp>=3.8.0"]
database = ["sqlalchemy>=2.0.0", "psycopg2-binary>=2.9.0"]
all = ["aiohttp>=3.8.0", "sqlalchemy>=2.0.0", "psycopg2-binary>=2.9.0"]
[dependency-groups]
dev = ["pytest>=8.0.0"]
lint = ["ruff>=0.1.0", "mypy>=1.7.0"]
docs = ["mkdocs>=1.5.0"]# In pyproject.toml - use git repository for package
[tool.uv.sources]
# Get package from git tag
httpx = { git = "https://github.com/encode/httpx", tag = "0.27.0" }
# Install local package in editable mode (for monorepos)
local-lib = { path = "../local-lib", editable = true }
# Use specific PyPI index for package
torch = { index = "pytorch" }
# Define custom index
[[tool.uv.index]]
name = "pytorch"
url = "https://download.pytorch.org/whl/cu118"
explicit = true # Only use for torch package aboveuv add torch
uv sync#!/usr/bin/env -S uv run
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "httpx>=0.27",
# "rich>=13.0",
# ]
# ///
import httpx
from rich import print
def main():
response = httpx.get("https://api.github.com")
print(response.json())
if __name__ == "__main__":
main()chmod +x script.py
./script.py
# Or run directly
uv run script.py[project]
name = "cross-platform-app"
version = "0.1.0"
dependencies = [
"click>=8.1.0",
"pathlib-plus>=1.0.0",
]
[tool.uv]
# Support multiple operating systems with different dependencies
environments = [
"sys_platform == 'darwin'",
"sys_platform == 'linux'",
"sys_platform == 'win32'",
]
[tool.uv.sources]
# Platform-specific wheels via environment markers
[tool.uv.sources.pywin32]
version = "306"
markers = "sys_platform == 'win32'"# Always pin Python version early in project
uv python pin 3.12
# Commit .python-version to repository
git add .python-version# Wrong: adds to main dependencies
uv add pytest
# Correct: adds to dev group
uv add --dev pytest
uv add --group lint ruff
# Then install only production deps
uv sync --no-dev# Always commit uv.lock to version control
git add uv.lock
git commit -m "Update dependencies"
# Use --frozen in CI to catch sync issues
uv sync --frozen# Ensure build-system is defined in pyproject.toml
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
# Or use setuptools
requires = ["setuptools>=45", "wheel"]
build-backend = "setuptools.build_meta"[tool.uv]
# Prevent conflicting extras from being used together
conflicts = [
[{ extra = "cuda" }, { extra = "cpu" }],
]
# In dependency-groups, don't mix conflicting versions
[dependency-groups]
# Keep compatible versions together
ml-cpu = ["torch-cpu>=2.0"]
ml-gpu = ["torch-gpu>=2.0"]