Sub topic 12
Project layout and installing your package
This chapter assumes you can build a package with __init__.py files and know how sys.path is searched. It covers where to put a package in a project and how to make it importable from anywhere.
Why layout matters
When you run python script.py, Python puts the script's directory at the front of sys.path. Imports can then work only because of where you ran the command. Move to another directory and they fail with ModuleNotFoundError. Installing the package fixes this. The package goes onto the path in a standard way, and every tool finds it the same way.
The src layout
Put the importable package in a src directory, next to the project metadata and tests:
shapes-project/
pyproject.toml
README.md
src/
shapes/
__init__.py
circle.py
util/
__init__.py
mathx.py
tests/
test_circle.pyThe src directory is not on sys.path by default. Your tests therefore cannot import shapes by accident from the current directory. They import the installed copy, which is what your users will get. A flat layout, with shapes/ at the project root, is simpler. It can hide packaging mistakes, such as a file missing from the build, because the import works from the working directory anyway.
Describing the project
pyproject.toml holds the build configuration and the package metadata:
[build-system]
requires = ["setuptools>=61"]
build-backend = "setuptools.build_meta"
[project]
name = "shapes"
version = "0.1.0"
requires-python = ">=3.9"Setuptools finds packages under src/ automatically. The distribution name (shapes in [project]) is what you give to pip. The import name is the directory name under src. They often match, but they don't have to.
Installing in editable mode
Create a virtual environment, then install the project:
python -m venv .venv
source .venv/bin/activate
python -m pip install -e .The -e flag means editable. Pip does not copy your code into site-packages. It records a pointer to src/shapes, so edits to your files take effect on the next import with no reinstall. Check the result from any directory:
cd /tmp
python -c "import shapes; print(shapes.__file__)"The printed path should lead into your project's src directory.
Common problems
- Import works only from the project root. The package is probably not installed. Run
python -m pip install -e .and test again from another directory. - A new subpackage is missing from a regular install. Check that it contains an
__init__.py. Without one, tools that expect regular packages may skip it. - Wrong environment.
pip installmay target a different interpreter than the one running your code. Usepython -m pipso the installer and interpreter match.
Exercise
You move your tests to a new directory and import shapes raises ModuleNotFoundError, even though it worked from the project root. What is the likely cause, and what command fixes it?
Show answer
The package was importable only because the project root was the working directory and therefore on sys.path. It was never installed. Run python -m pip install -e . from the project root, inside the virtual environment you use to run the tests.