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__.pyfile is a package. Each.pyfile 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 withtest_.
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.
Lesson 7
if __name__ == "__main__"
You will see this at the bottom of almost every main.py:
def main() -> None:
...
if __name__ == "__main__":
main()- When you run a file (
uv run main.py), Python sets__name__to"__main__", somain()is called. - When another file imports it,
__name__is the file's name instead, so nothing runs by accident. - That is what makes it possible to test your functions without starting the whole program.
Lesson 7 · Try it
Two files working together
This example has two files. Click the tab pirates/treasure.py to read the
locked file, then press Run. Try to:
- Share 500 coins between 9 pirates instead.
- Remove the last two lines. What happens when you run it? Why?
Press ▶ Run to see what happens.
Lesson 7 · Exercise
Use a module
The locked file pirates/ship.py has a function crew_size. Import
it, use it in main() to print The crew has 5 pirates, and make
sure main() only runs when the file is started directly.
Press ▶ Run to see what happens.
💡 Need a hint?
Line 1: from pirates.ship import crew_size. In main: size = crew_size("Black Pearl") and print an f-string. At the bottom: if __name__ == "__main__": main()
Lesson 7
Do it in your project
In your learn-python project, make the folder pirates with an
empty __init__.py and this file:
def share_treasure(coins: int, pirates: int) -> tuple[int, int]:
"""Return how many coins each pirate gets, and how many are left over."""
each = coins // pirates
left_over = coins % pirates
return each, left_overAdd a test for it:
from pirates.treasure import share_treasure
def test_share_treasure():
assert share_treasure(100, 7) == (14, 2)
assert share_treasure(10, 5) == (2, 0)uv run pytest
ruff check .
ty checkAll green? Commit it with git. You now have a real, tested Python project. 🏴☠️
The end of the introduction
What's next?
You know variables, constants, types, operators, decisions, loops, functions and how a project is put together. That is the foundation of every program ever written.
Now you choose what to build, and in which language:
🐍 Python
Games with pygame, bots, data, small web apps.
🟨 JavaScript / TypeScript
Websites and web apps.
🐹 Go
Fast command-line tools and servers.
Talk to your instructor about your idea. 🏴☠️