Here's the best practice for setting up a Python project with uv on a fresh Ubuntu instance:
First, install uv using the official installer:
curl -LsSf https://astral.sh/uv/install.sh | shThen restart your shell or run:
source $HOME/.cargo/envFor a new project, use uv's project scaffolding:
uv init my-project
cd my-projectThis creates a proper Python project structure with:
pyproject.tomlfor project configurationsrc/directory with your packageREADME.mdand other standard files
If you need a specific Python version, uv can install and manage it:
# Install a specific Python version
uv python install 3.11
# Use it in your project
uv python pin 3.11Add dependencies using uv instead of pip:
# Add runtime dependencies
uv add requests pandas
# Add development dependencies
uv add --dev pytest black ruff mypyuv automatically creates and manages a virtual environment. To run commands:
# Run Python scripts
uv run python script.py
# Run installed tools
uv run pytest
uv run black .
# Start a shell in the environment
uv shellIf you have an existing project with requirements.txt:
# Convert requirements.txt to pyproject.toml
uv init --package
uv add -r requirements.txt- Fast: uv is significantly faster than pip
- Reliable: Handles dependency resolution better
- Modern: Uses pyproject.toml standard
- Integrated: Manages Python versions, virtual environments, and dependencies
- Lock files: Automatically creates
uv.lockfor reproducible builds
- Use
uv syncto install dependencies from lock file - Use
uv treeto visualize dependency tree - Use
uv pipcommands if you need pip-compatible interface - Configure tools like ruff, black, mypy in pyproject.toml
This setup gives you a modern, fast, and reliable Python development environment that follows current best practices.