Directory Structure for Python Projects

To get started with Directory Structure I would like to quote the following lines from a well-kown python book called The Hitchhiker’s Guide to Python: Best Practices for Development by Kenneth Reitz and Tanya Schlusser.

By “structure” we mean the decisions you make concerning how your project best meets its objective. We need to consider how to best leverage Python’s features to create clean, effective code. In practical terms, “structure” means making clean code whose logic and dependencies are clear as well as how the files and folders are organized in the filesystem.

To be honest, the directory structure depends on the project you are working on. It can change in every project but the goal is to keep it as familiar as possible with the commonly used directory structures.


Creating a skeleton directory structure for every project can be boring and time-consuming. To make life a little bit easier use a framework like –  Cookiecutter


Installable Single Package

Most of the time when I start a new project, I create all the required files and folders (skeleton files, folders) at the beginning even before pushing them to a source code management system (SCM)

helloworld/
│
├── helloworld/
│   ├── __init__.py
│   ├── helloworld.py
│   └── helpers.py
│
├── tests/
│   ├── helloworld_tests.py
│   └── helpers_tests.py
│
├── .gitignore
├── LICENSE
├── README.md
├── requirements.txt
└── setup.py

Application with Internal Packages

In larger applications, there could be one or more internal packages that are either tied together with a main runner script or that provide specific functionality to a larger library. Below is an example of such a directory structure with internal packages

helloworld/
│
├── bin/
│
├── docs/
│   ├── hello.md
│   └── world.md
│
├── helloworld/
│   ├── __init__.py
│   ├── runner.py
│   ├── hello/
│   │   ├── __init__.py
│   │   ├── hello.py
│   │   └── helpers.py
│   │
│   └── world/
│       ├── __init__.py
│       ├── helpers.py
│       └── world.py
│
├── data/
│   ├── input.csv
│   └── output.xlsx
│
├── tests/
│   ├── hello
│   │   ├── helpers_tests.py
│   │   └── hello_tests.py
│   │
│   └── world/
│       ├── helpers_tests.py
│       └── world_tests.py
│
├── .gitignore
├── LICENSE
└── README.md

Web Application Layout

Flask documentation gives a suggested layout for their tutorial project (a blogging web application called Flaskr), and this is how the main project directory layout looks like

flaskr/
│
├── flaskr/
│   ├── ___init__.py
│   ├── db.py
│   ├── schema.sql
│   ├── auth.py
│   ├── blog.py
│   ├── templates/
│   │   ├── base.html
│   │   ├── auth/
│   │   │   ├── login.html
│   │   │   └── register.html
│   │   │
│   │   └── blog/
│   │       ├── create.html
│   │       ├── index.html
│   │       └── update.html
│   │
│   └── static/
│       └── style.css
│
├── tests/
│   ├── conftest.py
│   ├── data.sql
│   ├── test_factory.py
│   ├── test_db.py
│   ├── test_auth.py
│   └── test_blog.py
│
├── venv/
│
├── .gitignore
├── setup.py
└── MANIFEST.in

Sources

Enjoy!