Python Project Structure
A scalable and maintainable folder structure for Python applications
File Structure
- src
- __init__.py
- main.py
- core
- __init__.py
- config.py
- database.py
- exceptions.py
- logging.py
- models
- __init__.py
- user.py
- post.py
- base.py
- services
- __init__.py
- user_service.py
- auth_service.py
- email_service.py
- repositories
- __init__.py
- user_repository.py
- base_repository.py
- api
- __init__.py
- routes.py
- middleware.py
- utils
- __init__.py
- helpers.py
- validators.py
- decorators.py
- tests
- __init__.py
- conftest.py
- unit
- test_services.py
- test_models.py
- integration
- test_api.py
- test_database.py
- docs
- api.md
- installation.md
- scripts
- migrate.py
- seed_data.py
- requirements.txt
- requirements-dev.txt
- setup.py
- pyproject.toml
- .env.example
- .gitignore
- README.md
Directory Structure Explanation
core/exceptions.py
Custom exception classes for handling application-specific errors.
core/logging.py
Logging configuration and setup for application monitoring.
src/repositories/
Data access layer implementing the repository pattern for database operations.
repositories/user_repository.py
Repository class for user data access and database operations.
src/api/
API layer containing routes and HTTP request handling.
api/routes.py
API route definitions and endpoint handlers.
api/middleware.py
Middleware functions for request processing and authentication.
utils/decorators.py
Custom decorators for cross-cutting concerns like authentication and logging.
tests/conftest.py
Pytest configuration and shared test fixtures.
tests/unit/
Unit tests for individual components and functions.
tests/integration/
Integration tests for testing component interactions.
docs/
Project documentation including API docs and installation guides.
scripts/
Utility scripts for database migrations, data seeding, and maintenance.
pyproject.toml
Modern Python project configuration file for build tools and dependencies.
requirements-dev.txt
Development dependencies separate from production requirements.
Blog
Browse all Python tutorials, guides, and development articles.