Module 11 / Project 01 — Package Structure¶
Home: README · Module: Package Publishing
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| — | This project | Walkthrough | — | Flashcards | — | — |
Focus¶
- The
srclayout for Python packages pyproject.tomlconfiguration__init__.pyand what it does- Entry points (console scripts)
Why this project exists¶
Before you can share your code as a pip-installable package, you need to structure it correctly. This project builds a real package from scratch and explains every file.
Run¶
Expected output¶
Project structure¶
01-package-structure/
├── pyproject.toml # Package metadata and build config
├── README.md # This file (also rendered on PyPI)
├── LICENSE # MIT license
├── src/
│ └── mymath/
│ ├── __init__.py # Makes mymath a package, exports version
│ ├── calculator.py # Core math functions
│ └── statistics.py # Mean, median, mode functions
└── tests/
├── test_calculator.py
└── test_statistics.py
Alter it¶
- Add a new module
src/mymath/geometry.pywitharea_circle()andarea_rectangle(). Update__init__.pyto export it. - Add a console script entry point in
pyproject.tomlso you can runmymath-calcfrom the command line. - Change the version to 0.2.0 and add a
__version__attribute.
Break it¶
- Remove
__init__.py. Can you still import mymath? - Put files directly in
mymath/instead ofsrc/mymath/. What changes? - Delete
pyproject.toml. Can you still run the code? Can you build a package?
Fix it¶
- Restore
__init__.pyand verify imports work again. - If using flat layout, update
pyproject.tomlto find packages correctly. - Recreate
pyproject.tomlwith the minimum required fields.
Explain it¶
- Why does the
srclayout exist? What problem does it solve? - What is the difference between a module and a package?
- What does
__init__.pydo when youimport mymath? - What is an entry point and why would you use one?
Mastery check¶
You can move on when you can: - create a package with the src layout from memory, - explain what pyproject.toml fields are required, - add a new module and export it from init.py, - write tests for your package.
Related Concepts¶
- Errors and Debugging
- How Imports Work
- How Loops Work
- Types and Conversions
- Quiz: Errors and Debugging