Sub topic 1
Laying out a library: why the src layout
Prerequisites
This chapter assumes you can write and import Python modules and packages, use a virtual environment with pip, and run basic shell commands. You do not need to know anything about building or publishing yet.
The two common layouts
A Python library can keep its importable package in one of two places:
- Flat layout: the package folder sits next to
pyproject.tomlat the project root. - src layout: the package folder sits inside a
src/directory.
The tutorial's example library, greeter, uses the src layout:
greeter/
├── pyproject.toml
├── README.md
├── LICENSE
├── src/
│ └── greeter/
│ ├── __init__.py
│ ├── cli.py
│ └── py.typed
└── tests/
└── test_greet.pyThe outer greeter/ folder is the project. The inner src/greeter/ folder is the import package, the thing users get with import greeter. Keep the two ideas apart. The project name is what people type after pip install. The import package name is what they type after import. They are often the same, but they do not have to be.
Why src prevents a common mistake
When you run python or pytest from the project root, that directory is usually on sys.path. In a flat layout, import greeter then finds the folder in your working tree. Your tests pass, but they have not tested the package that users will install.
This hides real problems:
- A file that is missing from the built package still imports fine locally.
- A data file you forgot to include is found because it sits next to your code.
- Metadata errors go unnoticed because nothing was actually installed.
With the src layout, import greeter from the project root fails until the package is installed. You must install it first, so your tests exercise the installed copy, the same thing a user receives.
flowchart TD
A[Run tests from project root] --> B{Layout}
B -->|Flat| C[Imports the folder in the working directory]
B -->|src| D[Imports the installed package]
C --> E[Packaging mistakes can stay hidden]
D --> F[Packaging mistakes show up as test failures]To make the diagram block valid for this platform, the same flow is shown here in the required form:
Working with the layout day to day
Create a virtual environment and install the project in editable mode, so code changes take effect without reinstalling:
python -m venv .venv
source .venv/bin/activate
python -m pip install -e ".[test]"
python -m pytestThe [test] part installs the optional test extra, which a later chapter defines. For now, any command that installs the project will do.
The src layout also keeps the project root tidy. Tests, docs, and scripts stay outside the package, so they are never shipped by accident.
Common errors
ModuleNotFoundError: No module named 'greeter': the project is not installed in the active environment. Activate the right environment and run the editable install.- Package folder placed at the wrong depth: the folder must be
src/greeter/, notsrc/greeter/greeter/. - Missing
__init__.py: build tools can then skip the package, depending on configuration. Include one in every package folder. - Tests inside the package: placing
tests/undersrc/greeter/ships them to users. Keep them at the project root.
Exercise
You run pytest from the project root in a src layout project and see ModuleNotFoundError: No module named 'greeter'. What does this tell you, and what do you do?
Show answer
The package is not installed in the active environment, which is the behavior the src layout intends. Activate your virtual environment and run python -m pip install -e ., then run the tests again.
Exercise
Name one problem a flat layout can hide that a src layout exposes.
Show answer
A file or data file missing from the built package. In a flat layout the import still works from the working directory, so tests pass. In a src layout the test only sees what was installed, so the omission causes a failure.