Lesson · Virtual Environments, Dependencies & Packaging
Project Structure
65 min readProject Structure
Project structure determines import behavior, packaging behavior, and test ergonomics. A clean layout makes the application boundary, package boundary, tests, and configuration obvious.
Learning goals
- Describe what a project layout decides: which directory is importable, what tests can reach before installation, and where tools look for configuration.
- Compare a flat layout with the
srclayout and say which import mistakes each one lets through. - Explain why a
srclayout forces tests to exercise the package the way an installer would. - Lay out a repository for an application and for a library, and name the differences between the two shapes.
- Keep tests, package code and configuration in their own places so tooling finds them without path patching.
Mental model
A layout is a set of boundaries: which directory is importable, which directory merely observes the package, and which files configure everything from outside. Each boundary exists to make one class of mistake visible, and the src layout is the strictest version of the first one.
Mental model
read it top to bottom
repository root README, pyproject.toml, ignore rules ↓ src/ boundary only the package lives below it package directory (acme_tools/) what the rest of the world imports ↓ tests/ boundary exercises the package from outside tools read configuration build, test and editor setup from the root
Core idea
The layout of a repository decides what a Python process can import before any installation step, which is why it is really a question about boundaries: where the importable package starts, where tests stand relative to it, and where configuration applies from. The src layout buys one specific guarantee, that a test run cannot quietly import the working tree it sits next to, and the price is telling the tools where the package lives. Application and library repositories differ mainly in which boundary carries the weight.
Package layout
In Project Structure, Package layout names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
src layout
A src layout keeps importable package code under a dedicated source directory. This helps tests catch packaging mistakes instead of accidentally importing the repository working tree.
Application vs library
This comparison separates Application from library in the context of Project Structure. Focus on ownership, mutability, cost, failure behavior, and API intent rather than treating the two choices as interchangeable.
Tests directory
In Project Structure, Tests directory names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
Configuration
A project's configuration is the set of files that declare how its code is built, tested and inspected, headed by pyproject.toml; tool sections in that file let a single root serve packaging, testing and linting. Keeping configuration at the repository root is what lets every tool agree on where the package and the tests live.
Avoiding import-path hacks
In Project Structure, Avoiding import-path hacks names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
Walkthrough
- The tree puts the package under
src/acme_tools/, so the importable name isacme_toolsand its files (__init__.py,core.py) are the only code the outside world can import. tests/test_core.pysits outsidesrc/, which is the point of the shape: a test must reach the package through installation or an explicit editable install, not by happening to sit beside it.pyproject.tomlat the root is where build and test tools are configured; with asrclayout, tooling has to be told (or has to auto-discover) that the package lives undersrc/, as the example's final comment notes.- Predict before running: what
import acme_toolsresolves to when you startpythonfrom the repository root with nothing installed, and what changes if the package sits at the root instead.
Experiments to try
- Recreate the tree in a fresh folder, run
python -c "import acme_tools"from the root, and record theModuleNotFoundError; then install withpython -m pip install -e .and run it again. - Move the package to the repository root (a flat layout), repeat the import from the root, and explain when that convenience hides a packaging bug.
- Add a test that imports
acme_toolsand run it from the root before and after the editable install; note the difference. - Rename the package directory so it no longer matches the project's
name, and see which command reports the mismatch first.
Common mistakes
- Importing the same module through two different paths (as
acme_tools.corein one place and ascorein another): the second form works only while the working directory happens to be the package's parent. - Patching
sys.pathin tests or scripts (sys.path.insert(0, ...)): the run then depends on the working directory, which masks exactly the packaging errors the layout exists to expose. - Spreading configuration across the repository (one file at the root, another inside the package): tools then apply different roots and disagree about the same project.
- Letting generated directories (
build/,dist/,*.egg-info/,__pycache__/) into version control: a fresh clone behaves differently from a working tree that has run the tools.
Practice: Easy
- Recreate the worked example's tree (pyproject.toml,
src/acme_tools/with__init__.pyandcore.py,tests/test_core.py) and describe which directory each file belongs to and why. - Run
python -c "import acme_tools"from the repository root before any installation; record theModuleNotFoundErrorand explain it from the layout. - Move
acme_toolsfromsrc/to the repository root, repeat the command, and say which mistake thesrclayout was catching.
Practice: Medium
- Convert a flat project to the
srclayout: move the package, adjust the tool configuration, and show that bothimport acme_toolsand the test run succeed after installing the project. - Write one test that uses only the public import path
acme_toolsand one that reaches intoacme_tools.core; explain which of the two belongs in a suite. - Configure one repository for two consumers (an application entry point and a packaged wheel) and list which layout change each consumer forces.
Practice: Hard
- Design the repository shape for a small library that also ships a CLI: name the package directory, the tests directory, the entry point's location and the configuration file; justify each boundary from a class of mistake it prevents.
- Build a failure lab: a project whose imports work only from one working directory, a test that needs a
sys.pathhack, and an install whose metadata name does not match the directory; reproduce each and explain the mechanism. - Restructure a small project into the
srclayout and write down every place that had to change (imports, configuration, commands) as a migration checklist.
Lesson checklist
- I can draw the flat and
srclayouts and mark the importable directory in each. - I can explain why a
srclayout exposes packaging mistakes that a flat layout hides. - I can name the three boundaries (package, tests, configuration) and what each one decides.
- I can lay out an application and a library repository and say what differs between them.
Remember
A project layout is a set of boundaries: where the importable package starts, where tests stand, and where configuration applies. The `src` layout enforces the strictest version of the first boundary, so imports and tests fail until the project is properly packaged, which is exactly the feedback a growing codebase wants before it ships.
Interview angles
What is the main purpose of **Package layout**?+
In Project Structure, Package layout names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
What should you pay attention to when using **src layout**?+
A src layout keeps importable package code under a dedicated source directory. This helps tests catch packaging mistakes instead of accidentally importing the repository working tree.
How does **Application vs library** fit into this lesson?+
This comparison separates Application from library in the context of Project Structure. Focus on ownership, mutability, cost, failure behavior, and API intent rather than treating the two choices as interchangeable.
Why does the `src` layout make imports fail before installation, and why is that treated as a feature?+
Because the package is not on sys.path from the repository root, a test cannot reach it by accident; failing until the project is properly installed or configured means the test exercises the same import path a user will get.