Coding Pirates

Lesson 7

Project structure

One file is fine for a small program. Real projects grow, so we split the code into several files and folders. This is how a modern Python project looks:

learn-python/
├── pyproject.toml        ← name, Python version, packages (pytest lives here)
├── .python-version
├── main.py               ← the program starts here
├── pirates/              ← your own code, split into files (a "package")
│   ├── __init__.py       ← empty file that says: this folder is a package
│   └── treasure.py
└── tests/
    └── test_treasure.py  ← tests for the code in pirates/
  • A folder with an __init__.py file is a package. Each .py file in it is a module.
  • You use code from a module with from pirates.treasure import share_treasure.
  • Tests live in tests/, and pytest finds every file that starts with test_.

Bigger projects and libraries put the packages in a src/ folder: src/learn_python/. That is what uv init does when you leave out --no-package. Same idea, one folder deeper.

1 / 6