src layout vs flat layout (original) (raw)

View this page

Edit this page

Toggle table of contents sidebar

The “flat layout” refers to organising a project’s files in a folder or repository, such that the various configuration files andimport packages are all in the top-level directory.

. ├── README.md ├── noxfile.py ├── pyproject.toml ├── setup.py ├── awesome_package/ │ ├── init.py │ └── module.py └── tools/ ├── generate_awesomeness.py └── decrease_world_suck.py

The “src layout” deviates from the flat layout by moving the code that is intended to be importable (i.e. import awesome_package, also known asimport packages) into a subdirectory. This subdirectory is typically named src/, hence “src layout”.

. ├── README.md ├── noxfile.py ├── pyproject.toml ├── setup.py ├── src/ │ └── awesome_package/ │ ├── init.py │ └── module.py └── tools/ ├── generate_awesomeness.py └── decrease_world_suck.py

Here’s a breakdown of the important behaviour differences between the src layout and the flat layout:

Running a command-line interface from source with src-layout

Due to the firstly mentioned specialty of the src layout, a command-line interface can not be run directly from the source tree, but requires installation of the package inDevelopment Modefor testing purposes. Since this can be unpractical in some situations, a workaround could be to prepend the package folder to Python’ssys.path when called via its __main__.py file:

import os import sys

if not package: # Make CLI runnable from source tree with # python src/package package_source_path = os.path.dirname(os.path.dirname(file)) sys.path.insert(0, package_source_path)