Poetry is a modern Python dependency management and packaging tool that provides:
- Dependency Resolution: Automatically resolves and locks dependencies
- Virtual Environments: Creates and manages isolated Python environments
- Reproducible Builds: Lock file ensures consistent installations across all environments
- Easy Dependency Management: Simple commands to add, update, and remove packages
macOS / Linux:
curl -sSL https://install.python-poetry.org | python3 -Windows (PowerShell):
(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py -Alternative (via pip):
pip install poetrypoetry --versionPoetry supports tab completion for bash, zsh, and fish:
# Bash
poetry completions bash >> ~/.bash_completion
# Zsh
poetry completions zsh > ~/.zfunc/_poetry
# Fish
poetry completions fish > ~/.config/fish/completions/poetry.fishThis project is configured with Poetry using the following files:
The main configuration file that defines:
- Project metadata (name, version, description, authors)
- Python version requirement (
^3.11) - All project dependencies
- Dependency groups (dev, wvnet, uat)
- Build system configuration
Auto-generated lock file that:
- Locks exact versions of all dependencies (including transitive ones)
- Ensures reproducible installations
- Should be committed to version control
Project-specific Poetry settings:
[virtualenvs]
create = true
in-project = trueThis configuration creates the virtual environment inside the project directory (.venv/), making it easier to manage and discover.
git clone https://github.com/3492PARTs/PARTs_WebAPI.git
cd PARTs_WebAPIInstall only production dependencies:
poetry install --no-devInstall with development dependencies (recommended for contributors):
poetry install --with devInstall all optional dependency groups:
poetry install --with dev,wvnet,uatOption 1: Use poetry run (recommended)
poetry run python src/manage.py runserver
poetry run pytestOption 2: Activate the shell
poetry shell
# Now you're in the virtual environment
python src/manage.py runserver
pytestOption 3: Direct shell activation
# Unix/macOS
source .venv/bin/activate
# Windows
.venv\Scripts\activateThis project defines several dependency groups for different use cases:
These are the core dependencies required to run the application:
[tool.poetry.dependencies]
python = "^3.11"
django = "5.2.8"
djangorestframework = "3.16.1"
djangorestframework-simplejwt = "5.5.1"
django-cors-headers = "4.8.0"
django-filter = "25.1"
django-simple-history = "3.10.1"
django-webpush = "0.3.6"
cloudinary = "1.44.1"
gunicorn = "23.0.0"
python-dotenv = "1.1.1"
requests = "2.32.5"
pillow = "11.3.0"
# ... and other core dependenciesInstallation:
poetry installTesting and development tools:
[tool.poetry.group.dev]
optional = true
[tool.poetry.group.dev.dependencies]
pipdeptree = "^2.23.4" # Visualize dependency tree
pytest = "^8.0.0" # Test framework
pytest-django = "^4.8.0" # Django plugin for pytest
pytest-cov = "^6.0.0" # Coverage reporting
pytest-mock = "^3.14.0" # Enhanced mocking
freezegun = "^1.5.0" # Time mocking for tests
factory-boy = "^3.3.0" # Test data factories
requests-mock = "^1.12.0" # HTTP request mocking
faker = "^33.0.0" # Fake data generationInstallation:
poetry install --with devUse for:
- Running tests (
pytest) - Code coverage reports
- Development and debugging
- Test data generation
Dependencies for WVNet deployment environment:
[tool.poetry.group.wvnet]
optional = true
[tool.poetry.group.wvnet.dependencies]
gunicorn = "23.0.0" # WSGI HTTP server
uwsgi = "2.0.30" # Application server
mysqlclient = "2.2.7" # MySQL database adapterInstallation:
poetry install --with wvnetUse for:
- WVNet production deployments
- MySQL database connections
- uWSGI application server setup
Dependencies for User Acceptance Testing (staging) environment:
[tool.poetry.group.uat]
optional = true
[tool.poetry.group.uat.dependencies]
gunicorn = "23.0.0" # WSGI HTTP server
uwsgi = "2.0.30" # Application server
psycopg2-binary = "*" # PostgreSQL database adapterInstallation:
poetry install --with uatUse for:
- UAT/staging deployments
- PostgreSQL database connections
- Pre-production testing
Add a new dependency:
# Add to main dependencies
poetry add django-extensions
# Add to dev group
poetry add --group dev black
# Add with version constraint
poetry add "django>=4.0,<5.0"Remove a dependency:
poetry remove package-nameUpdate dependencies:
# Update all dependencies
poetry update
# Update specific package
poetry update django
# Update packages in a group
poetry update --with devShow installed packages:
poetry show
# Show as a tree
poetry show --tree
# Show only dev dependencies
poetry show --only devCheck for outdated packages:
poetry show --outdatedRun a command in the virtual environment:
poetry run python src/manage.py migrate
poetry run pytest
poetry run python src/manage.py runserverActivate shell:
poetry shellExit shell:
exitShow environment info:
poetry env infoList environments:
poetry env listRemove environment:
poetry env remove python3.11Use a specific Python version:
poetry env use python3.11
poetry env use python3.12Update lock file without installing:
poetry lock --no-updateUpdate lock file and upgrade dependencies:
poetry lockInstall from lock file exactly:
poetry install --syncIf you need a requirements.txt file (for deployment platforms that don't support Poetry):
Export all dependencies:
poetry export -f requirements.txt --output requirements.txt --without-hashesExport with dev dependencies:
poetry export -f requirements.txt --output requirements.txt --with dev --without-hashesExport specific groups:
poetry export -f requirements.txt --output requirements.txt --with dev,wvnet --without-hashes# 1. Install with dev dependencies
poetry install --with dev
# 2. Set up environment
cp .env.example .env
# Edit .env with your settings
# 3. Run migrations
poetry run python src/manage.py migrate
# 4. Run tests
poetry run pytest
# 5. Start development server
poetry run python src/manage.py runserver# All tests with coverage
poetry run pytest
# Without coverage (faster)
poetry run pytest --no-cov
# Specific test file
poetry run pytest tests/user/test_user_comprehensive.py
# Verbose output
poetry run pytest -v# Install with WVNet dependencies
poetry install --without dev --with wvnet
# Start with gunicorn
cd src
poetry run gunicorn parts_webapi.wsgi:application --bind 0.0.0.0:8000# Install with UAT dependencies
poetry install --without dev --with uat
# Run migrations
poetry run python src/manage.py migrate --settings=parts_webapi.settings.production
# Start with gunicorn
cd src
poetry run gunicorn parts_webapi.wsgi:application --bind 0.0.0.0:8000# Recreate the virtual environment
poetry env remove python3.11
poetry install --with dev# Clear cache and reinstall
poetry cache clear pypi --all
poetry install --with dev# Regenerate lock file
poetry lock --no-update
poetry install --with dev# Add Poetry to PATH (after installation)
export PATH="$HOME/.local/bin:$PATH"
# Or reinstall
curl -sSL https://install.python-poetry.org | python3 -# Use newer installer (faster)
poetry config installer.modern-installation trueList current config:
poetry config --listSet PyPI credentials:
poetry config pypi-token.pypi <your-token>Configure virtualenv location:
# Create in project directory (already configured for this project)
poetry config virtualenvs.in-project true
# Create in global cache
poetry config virtualenvs.in-project falseThe project uses poetry.toml for project-specific settings:
[virtualenvs]
create = true
in-project = trueThis ensures consistent virtual environment location across all developers.
- Always use Poetry for dependency management
- Commit poetry.lock to ensure reproducible builds
- Use dependency groups appropriately (dev, wvnet, uat)
- Run tests before committing:
poetry run pytest - Keep dependencies updated regularly
- Use specific version constraints when adding critical dependencies
# 1. Add the dependency
poetry add package-name
# 2. Test that it works
poetry run pytest
# 3. Commit both pyproject.toml and poetry.lock
git add pyproject.toml poetry.lock
git commit -m "Add package-name dependency"# 1. Update dependencies
poetry update
# 2. Run tests to ensure nothing broke
poetry run pytest
# 3. Commit the updated lock file
git add poetry.lock
git commit -m "Update dependencies"| Feature | Poetry | pip + virtualenv |
|---|---|---|
| Dependency Resolution | ✅ Automatic | ❌ Manual |
| Lock File | ✅ Yes (poetry.lock) | |
| Virtual Environment | ✅ Automatic | ❌ Manual creation |
| Dependency Groups | ✅ Built-in | ❌ Multiple requirement files |
| Add/Remove Packages | ✅ poetry add/remove |
pip install + manual requirements update |
| Version Constraints | ✅ Flexible syntax | |
| Build System | ✅ Integrated | ❌ Separate tools |
- Official Documentation: https://python-poetry.org/docs/
- PyPI Package Index: https://pypi.org/
- Dependency Groups: https://python-poetry.org/docs/managing-dependencies/#dependency-groups
- Version Constraints: https://python-poetry.org/docs/dependency-specification/
- Commands Reference: https://python-poetry.org/docs/cli/
# Setup
poetry install # Install dependencies
poetry install --with dev # Install with dev group
poetry install --with dev,wvnet,uat # Install all groups
# Add/Remove
poetry add package-name # Add dependency
poetry add --group dev package-name # Add to dev group
poetry remove package-name # Remove dependency
# Update
poetry update # Update all
poetry update package-name # Update specific package
# Run
poetry run <command> # Run command
poetry shell # Activate shell
# Info
poetry show # List packages
poetry show --tree # Show as tree
poetry show --outdated # Check for updates
poetry env info # Environment info
# Export
poetry export -f requirements.txt --output requirements.txt --without-hashesFor questions about Poetry usage in this project:
- Check this documentation
- See CONTRIBUTING.md for contribution guidelines
- Review the official Poetry documentation
- Open an issue on GitHub for project-specific questions