This guide covers everything you need to know about working with UV in the DocuNative project.
- Setting Up a New Branch
- Installing Additional Packages
- Adding Dependencies to the Project
- Updating Existing Dependencies
- Common UV Commands
- Troubleshooting UV
- Best Practices
- Example: Complete Workflow
When starting work on a new feature or bug fix:
# 1. Create and switch to your branch
git checkout -b feature/your-feature-name
# 2. Sync dependencies (creates .venv if needed)
uv sync
# 3. Activate the environment
source .venv/bin/activate # macOS/Linux
.venv\Scripts\activate # WindowsWhat uv sync does:
- Reads
pyproject.tomlanduv.lock - Installs exact versions specified in the lock file
- Ensures everyone has identical dependencies
For temporary testing or experimentation:
# Install a package temporarily (not added to pyproject.toml)
uv pip install package-name
# Example: Install ipython for debugging
uv pip install ipythonUse cases:
- Quick testing
- Debugging sessions
- Exploratory data analysis
- One-off scripts
If UV has compatibility issues with a specific package:
# First, install pip into the UV environment
uv pip install pip
# Then use pip normally
pip install package-name
# Example:
pip install matplotlibNote: This is rarely needed. UV handles 99% of packages perfectly.
If you need a package permanently for production code:
Add your package to the dependencies array:
dependencies = [
# ... existing deps ...
"new-package>=1.0,<2.0", # Add with version constraints
]Version constraint guidelines:
>=1.0,<2.0- Allow minor updates, block major breaking changes==1.5.3- Pin exact version (only if absolutely necessary)>=1.0- No upper bound (risky, not recommended)
uv lockThis resolves all dependencies and regenerates uv.lock.
uv syncgit add pyproject.toml uv.lock
git commit -m "Add new-package dependency for feature X"Important: Always commit both files together!
# You need pandas for data processing
vim pyproject.toml # Add "pandas>=2.0,<3.0" to dependencies
uv lock # Resolve dependencies
uv sync # Install
# Test the import
python -c "import pandas; print(pandas.__version__)"
# Commit
git add pyproject.toml uv.lock
git commit -m "Add pandas for data processing pipeline"Upgrade all packages to their latest compatible versions:
# Update lock file with latest versions
uv lock --upgrade
# Install the updated versions
uv syncUse case: Monthly maintenance updates
Upgrade only one package:
# Upgrade only torch to latest compatible version
uv lock --upgrade-package torch
# Install the update
uv syncUse case: Security patch for specific package, or testing new feature in one library
# See what uv lock --upgrade would change (dry run)
uv lock --upgrade --dry-run| Command | Purpose | When to Use |
|---|---|---|
uv sync |
Install dependencies from uv.lock |
After pulling changes, switching branches |
uv lock |
Regenerate uv.lock from pyproject.toml |
After editing dependencies |
uv lock --upgrade |
Update all packages to latest versions | Monthly updates |
uv lock --upgrade-package pkg |
Update specific package | Security patches, feature updates |
uv pip install pkg |
Install package temporarily | Testing, debugging |
uv pip list |
List installed packages | Checking versions |
uv pip uninstall pkg |
Remove package | Cleanup |
uv run python script.py |
Run script without activating venv | Quick scripts, CI/CD |
uv venv |
Create virtual environment | Manual setup (rarely needed) |
uv venv --python 3.11 |
Create venv with specific Python | Testing different versions |
Symptom: UV can't find a package on PyPI
Solution:
# Clear UV cache
rm -rf ~/.cache/uv
# Retry
uv syncSymptom: UV shows dependency conflict error
Example:
Because package-a>=2.0 requires package-b<1.0
and package-c>=1.0 requires package-b>=1.5,
we can conclude that package-a>=2.0 and package-c>=1.0 are incompatible.
Solution: Adjust version constraints in pyproject.toml to find compatible ranges:
# Before (conflicting)
dependencies = [
"package-a>=2.0",
"package-c>=1.0",
]
# After (compatible)
dependencies = [
"package-a>=1.5,<2.0", # Use older version
"package-c>=1.0",
]Symptom: Lock file doesn't match pyproject.toml
Solution:
# Regenerate lock file
uv lock
# Then sync
uv syncSymptom: Package requires compilation but no pre-built wheel exists
Solution:
# Option 1: Install build tools
# macOS
brew install cmake
# Linux
sudo apt install build-essential
# Then retry
uv sync
# Option 2: Use pip fallback
uv pip install pip
pip install problematic-packageSymptom: UV complains about Python version
Solution:
# Check required version in pyproject.toml
# requires-python = ">=3.11,<3.12"
# Ensure Python 3.11 is available
python3.11 --version
# Recreate venv with correct version
rm -rf .venv
uv venv --python 3.11
uv sync-
Always run
uv syncafter pulling changesgit pull origin main uv sync # Update dependencies -
Commit both
pyproject.tomlanduv.locktogethergit add pyproject.toml uv.lock git commit -m "Update dependencies" -
Use version constraints for stability
dependencies = [ "torch>=2.0,<3.0", # Good: Allows patches, blocks breaking changes "gradio>=6.0,<7.0", # Good: Semantic versioning ]
-
Test changes with clean environment
rm -rf .venv make install make test
-
Don't manually edit
uv.lock- It's auto-generated
- Changes will be overwritten by
uv lock - Use
uv lockto regenerate it
-
Don't use
pip install -r requirements.txt- We don't use
requirements.txtanymore - Use
uv syncinstead
- We don't use
-
Don't commit
.venv/directory- It's in
.gitignore - Everyone creates their own with
uv sync - Contains machine-specific paths
- It's in
-
Don't use
pip freeze > requirements.txt- Use
pyproject.tomlfor dependencies - Use
uv.lockfor exact versions requirements.txtis deprecated in this project
- Use
Here's a complete workflow for adding a new feature that requires a new dependency:
# 1. Create feature branch
git checkout -b feature/add-pdf-compression
# 2. Install existing dependencies
uv sync
# 3. Test new package temporarily
uv pip install PyPDF2
python -c "import PyPDF2; print('Works!')"
# 4. Write your code
vim pipeline/compress.py
# (implement your feature)
# 5. Add package permanently to pyproject.toml
vim pyproject.toml
# Add: "PyPDF2>=3.0,<4.0" to dependencies
# 6. Update lock file
uv lock
# 7. Reinstall to verify everything works
uv sync
# 8. Test your feature
python pipeline/compress.py
# (verify it works)
# 9. Test with clean environment
rm -rf .venv
make install
python pipeline/compress.py
# (verify it still works)
# 10. Commit changes
git add pyproject.toml uv.lock pipeline/compress.py
git commit -m "Add PDF compression feature with PyPDF2"
# 11. Push and create PR
git push origin feature/add-pdf-compression
# (Create PR on GitHub)# Daily Operations
uv sync # Install/update deps after pulling
uv run python script.py # Run script without activating venv
# Adding Dependencies
vim pyproject.toml # Add package to dependencies
uv lock # Resolve and update lock file
uv sync # Install new packages
# Testing Packages
uv pip install package-name # Install temporarily
python -c "import package" # Test it
# Updating
uv lock --upgrade # Update all packages
uv lock --upgrade-package torch # Update specific package
uv sync # Install updates
# Troubleshooting
rm -rf ~/.cache/uv # Clear cache
uv lock # Regenerate lock file
uv sync # Reinstall- UV Documentation: https://docs.astral.sh/uv/
- Project Issue Tracker: GitHub Issues
- Quick Questions: Ask in project Discord/Slack
Last Updated: March 16, 2026