Flask Project Structure: Best Practices and Examples
Informational article in the Building Real-World Flask Applications topical map — Design & Architecture content group. 12 copy-paste AI prompts for ChatGPT, Claude & Gemini covering SEO outline, body writing, meta tags, internal links, and Twitter/X & LinkedIn posts.
Flask project structure: best practices and examples prescribes a package-based layout that separates application code, configuration, static assets, and tests, and recommends using an application factory with at least three configuration classes (development, testing, production) to align with the Twelve-Factor App principles. This layout typically places the Flask app package under a src or app directory, puts Blueprints in a blueprints or modules subpackage, keeps database models and migration scripts (Alembic) in a models and migrations folder, and stores tests under tests/ so continuous integration tools can run pytest without importing production settings, and it should include requirements.txt and pyproject.toml for reproducible installs.
The mechanism works by decoupling initialization from registration so the same codebase can be instantiated with different configurations for local, CI, and production environments; the application factory pattern plus Flask Blueprints enable this separation. Tools like SQLAlchemy for ORM, Alembic for migrations, pytest for testing, Docker for containerized builds, and python-dotenv for local environment overrides are commonly used. Explicit package organization into app/, app/api/, app/models/, app/static/, and tests/ implements a predictable Flask blueprint structure and a clear Flask folder layout that reduces import-time side effects and simplifies running multiple app instances in different environments.
The most important nuance is that a single app.py script hides operational concerns until they become urgent: database migrations end up mixed with route code, tests inherit production configuration, and secrets leak into version control. For a production-ready Flask app those risks require moving models to a dedicated package, keeping Alembic migrations in migrations/, and running pytest against a testing configuration that uses an in-memory SQLite or isolated test database. Choosing between micro, modular, and hexagonal templates involves tradeoffs: micro templates minimize boilerplate but make integration testing harder, modular blueprints ease runtime wiring, and hexagonal architecture enforces ports-and-adapters separation which benefits complex domain logic and long-term maintainability; API versioning and backward-compatibility tests are often overlooked.
Practical next steps include adopting an application factory, moving route groups into Blueprints, centralizing configuration into Config classes with environment-variable overrides, adding Alembic migrations and a tests/ suite run by pytest, and packaging the app for containerized CI with a Dockerfile. Security controls should isolate secrets via environment variables, enforce HTTPS at the proxy, and run dependency scans during CI. Logging and metrics using the logging library and Prometheus client should be wired into entry points and accompanied by health and readiness endpoints for orchestrators. Immutable deployments and health checks reduce drift. The page contains a structured, step-by-step framework.
- Work through prompts in order — each builds on the last.
- Click any prompt card to expand it, then click Copy Prompt.
- Paste into Claude, ChatGPT, or any AI chat. No editing needed.
- For prompts marked "paste prior output", paste the AI response from the previous step first.
flask project structure example
Flask project structure: best practices and examples
authoritative, practical, evidence-based
Design & Architecture
Intermediate to senior Python developers and backend engineers building production-grade Flask applications who need a clear, opinionated, immediately usable project layout and migration path to production
Combines architecture theory with ready-to-use example layouts, security and testing checklist, CI/deployment snippets, and three real-world starter templates (micro, modular, and hexagonal) tied to the pillar on production-ready Flask architectures
- Flask application structure
- Flask blueprint structure
- production-ready Flask app
- Flask folder layout
- Flask architecture patterns
- structuring Flask projects
- Keeping everything in a single app.py file and not explaining migration and testing placement, which makes the article impractical for production use.
- Giving abstract principles without copy-paste file trees or runnable snippets (readers want immediate templates).
- Ignoring how configuration and environment variables differ between local and production, failing to show Config classes or dotenv usage.
- Skipping tests and CI guidance — recommending structure but not where tests, fixtures, and factories belong.
- Recommending blueprints without concrete examples of package __init__.py, registration code, and routing import patterns leading to circular imports.
- Not addressing deployment concerns (Docker, Gunicorn, process managers) so the structure can't be mapped to production workflows.
- Using vague statements about 'scalability' without showing background tasks placement (Celery/RQ) and inter-service boundaries.
- Provide three ready-to-clone starter templates (micro, modular/blueprints, hexagonal) in a GitHub gist — call this out in the intro and link from the CTA to increase dwell and backlinks.
- Show concrete file-tree diffs (before and after refactor) to illustrate migration paths from single-file apps; include git commands to create branches for each layout.
- Include a minimal GitHub Actions workflow and Dockerfile snippet in the CI/CD section and show exactly where to put them in the repo (/.github/workflows/, /docker/), which improves perceived utility and CTR.
- When describing Blueprints, include the exact import style that avoids circular imports (use local imports inside factory functions) and a short comment explaining why.
- Surface library version numbers (Flask X.Y.Z, Flask-Migrate, Celery) and 'last-tested' date to signal freshness; update these periodically — mention 'Tested with Flask 3.0+' if applicable.
- Recommend naming conventions for packages (api/, web/, core/, services/) and enforce one rule per section — readers often get overwhelmed by too many options.
- Provide one real-world anti-pattern and a migration recipe (e.g., migrating views from app.py to blueprints in 5 git commits) that a team can follow during refactoring.