A clean and structured template for building Python applications following the Hexagonal Architecture (also known as Ports and Adapters) pattern and Domain-Driven Design (DDD) principles.
The project is organized into the following key directories:
Contains the core of your framework.
Contains the core of your application and the domain logic.
Contains the bounded contexts of your application.
Contains the modules of your application.
Contains the tests of your application.
This template provides a starting point for building applications using Hexagonal Architecture, which offers:
- Clear separation of concerns
- Domain-centric design
- Framework independence
- Highly testable code
- Maintainable and scalable structure
The project follows these key principles:
-
Domain Layer (
domain/)- Contains the business logic
- Pure Python with no external dependencies
- Defines interfaces (ports) for external operations
-
Application Layer (
application/)- Orchestrates the flow of data and implements use cases
- Coordinates between the domain and infrastructure layers
-
Infrastructure Layer (
infrastructure/)- Implements adapters for external services
- Contains framework-specific code
- Implements the interfaces defined in the domain layer
- Clone this repository:
git clone https://github.com/your-username/python-hexagonal-architecture- Install uv (if needed):
curl -LsSf https://astral.sh/uv/install.sh | sh- Create a virtual environment and install dependencies:
uv sync --all-groupsTo install only production dependencies:
uv sync --no-dev- Create your bounded contexts under
src/ - Define your domain model in the
domain/layer - Implement use cases in the
application/layer - Add adapters in the
infrastructure/layer - Write tests following the same structure in
tests/
Run tests using:
uv run pytestDependencies are managed with uv and declared in pyproject.toml:
- Base:
[project.dependencies] - Production extras:
[project.optional-dependencies.prod] - Optional adapter extras: e.g.
[project.optional-dependencies.rabbitmq] - Development:
[dependency-groups.dev](e.g. pytest, ruff) - Testing:
[dependency-groups.test](e.g. pytest, pytest-cov)
Install RabbitMQ adapter dependencies with:
uv sync --extra rabbitmqAdd dependencies with:
uv add <package> # production
uv add --dev <package> # dev group
uv add --group test <package> # test groupThis project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.