|
| 1 | +# Dependency Management with UV |
| 2 | + |
| 3 | +## Objective |
| 4 | +This guide provides best practices for managing Python dependencies using UV, a modern and fast Python package manager, ensuring reproducible builds and efficient dependency resolution. |
| 5 | + |
| 6 | +## Prerequisites |
| 7 | +- Python 3.11+ installed |
| 8 | +- UV installed |
| 9 | +- Basic understanding of Python packaging |
| 10 | + |
| 11 | +## Step-by-Step Instructions |
| 12 | + |
| 13 | +### 1. Initialize Project with UV |
| 14 | + |
| 15 | +```bash |
| 16 | +# Initialize a new project |
| 17 | +uv --init --package your-package-name |
| 18 | + |
| 19 | +# This creates: |
| 20 | +# - pyproject.toml |
| 21 | +# - README.md |
| 22 | +# - src/your_package_name/ |
| 23 | +# - tests/ |
| 24 | +``` |
| 25 | + |
| 26 | +### 2. Configure pyproject.toml |
| 27 | + |
| 28 | +```toml |
| 29 | +[project] |
| 30 | +name = "your-package-name" |
| 31 | +version = "0.1.0" |
| 32 | +description = "Your package description" |
| 33 | +authors = [ |
| 34 | + { name = "Your Name", email = "[email protected]"} |
| 35 | +] |
| 36 | +dependencies = [ |
| 37 | + "fastapi>=0.68.0", |
| 38 | + "uvicorn>=0.15.0", |
| 39 | + "pydantic>=1.8.0", |
| 40 | +] |
| 41 | +requires-python = ">=3.11" |
| 42 | + |
| 43 | +[build-system] |
| 44 | +requires = ["hatchling"] |
| 45 | +build-backend = "hatchling.build" |
| 46 | + |
| 47 | +[tool.uv] |
| 48 | +# UV specific configurations |
| 49 | +resolver = "uv" |
| 50 | +``` |
| 51 | + |
| 52 | +### 3. Add Development Dependencies |
| 53 | + |
| 54 | +```bash |
| 55 | +# Add development dependencies |
| 56 | +uv add --dev pytest black isort mypy |
| 57 | +``` |
| 58 | + |
| 59 | +### 4. Create Virtual Environment |
| 60 | + |
| 61 | +```bash |
| 62 | +# Create and activate virtual environment |
| 63 | +uv venv |
| 64 | +source .venv/bin/activate # On Windows: .venv\Scripts\activate |
| 65 | +``` |
| 66 | + |
| 67 | +### 5. Install Dependencies |
| 68 | + |
| 69 | +```bash |
| 70 | +# Install all dependencies |
| 71 | +uv pip install --system |
| 72 | + |
| 73 | +# Install in development mode |
| 74 | +uv pip install -e . |
| 75 | +``` |
| 76 | + |
| 77 | +### 6. Update Dependencies |
| 78 | + |
| 79 | +```bash |
| 80 | +# Update all dependencies |
| 81 | +uv pip install --upgrade --system |
| 82 | + |
| 83 | +# Update specific package |
| 84 | +uv pip install --upgrade fastapi |
| 85 | +``` |
| 86 | + |
| 87 | +### 7. Generate Requirements Files |
| 88 | + |
| 89 | +```bash |
| 90 | +# Generate requirements.txt |
| 91 | +uv pip freeze > requirements.txt |
| 92 | + |
| 93 | +# Generate dev-requirements.txt |
| 94 | +uv pip freeze --dev > dev-requirements.txt |
| 95 | +``` |
| 96 | + |
| 97 | +## Best Practices |
| 98 | + |
| 99 | +### 1. Dependency Management |
| 100 | +- Use exact versions for production dependencies |
| 101 | +- Group dependencies by purpose (core, dev, test) |
| 102 | +- Document dependency purposes |
| 103 | +- Regularly update dependencies |
| 104 | +- Use dependency groups for different environments |
| 105 | + |
| 106 | +### 2. Version Control |
| 107 | +- Include pyproject.toml in version control |
| 108 | +- Exclude virtual environment directory |
| 109 | +- Include requirements.txt for compatibility |
| 110 | +- Document version constraints |
| 111 | +- Use semantic versioning |
| 112 | + |
| 113 | +### 3. Security |
| 114 | +- Regularly audit dependencies |
| 115 | +- Use trusted package sources |
| 116 | +- Monitor for vulnerabilities |
| 117 | +- Keep dependencies updated |
| 118 | +- Document security considerations |
| 119 | + |
| 120 | +### 4. Performance |
| 121 | +- Use UV for faster installations |
| 122 | +- Minimize dependency count |
| 123 | +- Optimize dependency resolution |
| 124 | +- Use appropriate version constraints |
| 125 | +- Leverage caching |
| 126 | + |
| 127 | +## Validation |
| 128 | + |
| 129 | +### 1. Check Dependencies |
| 130 | +```bash |
| 131 | +# List installed packages |
| 132 | +uv pip list |
| 133 | + |
| 134 | +# Check for outdated packages |
| 135 | +uv pip list --outdated |
| 136 | +``` |
| 137 | + |
| 138 | +### 2. Test Installation |
| 139 | +```bash |
| 140 | +# Test clean installation |
| 141 | +uv pip install --system --no-cache-dir |
| 142 | + |
| 143 | +# Verify package imports |
| 144 | +python -c "import your_package_name" |
| 145 | +``` |
| 146 | + |
| 147 | +### 3. Security Audit |
| 148 | +```bash |
| 149 | +# Install safety |
| 150 | +uv pip install safety |
| 151 | + |
| 152 | +# Run security check |
| 153 | +safety check |
| 154 | +``` |
| 155 | + |
| 156 | +## Common Issues and Solutions |
| 157 | + |
| 158 | +### Issue 1: Dependency Conflicts |
| 159 | +- **Solution**: Use UV's conflict resolution |
| 160 | +- **Prevention**: Document version constraints |
| 161 | + |
| 162 | +### Issue 2: Slow Installation |
| 163 | +- **Solution**: Use UV's caching |
| 164 | +- **Prevention**: Minimize dependency count |
| 165 | + |
| 166 | +### Issue 3: Version Incompatibility |
| 167 | +- **Solution**: Use appropriate version constraints |
| 168 | +- **Prevention**: Test with different Python versions |
| 169 | + |
| 170 | +## Integration with Docker |
| 171 | + |
| 172 | +### 1. Dockerfile Integration |
| 173 | +```dockerfile |
| 174 | +# Install UV |
| 175 | +RUN curl -LsSf https://astral.sh/uv/install.sh | sh |
| 176 | + |
| 177 | +# Copy dependency files |
| 178 | +COPY pyproject.toml ./ |
| 179 | + |
| 180 | +# Install dependencies |
| 181 | +RUN uv pip install --system |
| 182 | +``` |
| 183 | + |
| 184 | +### 2. Multi-stage Build |
| 185 | +```dockerfile |
| 186 | +# Build stage |
| 187 | +FROM python:3.11-slim as builder |
| 188 | + |
| 189 | +# Install UV and dependencies |
| 190 | +RUN curl -LsSf https://astral.sh/uv/install.sh | sh |
| 191 | +COPY pyproject.toml ./ |
| 192 | +RUN uv pip install --system |
| 193 | + |
| 194 | +# Production stage |
| 195 | +FROM python:3.11-slim |
| 196 | +COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages |
| 197 | +``` |
| 198 | + |
| 199 | +## Next Steps |
| 200 | +- Configure Dockerfile (see Dockerfile-Best-Practices.md) |
| 201 | +- Set up CI/CD pipeline (see GitHub-Actions-Build-and-Deploy.md) |
| 202 | +- Implement testing strategy |
0 commit comments