What happens when `__init__.py` runs?

QuestionsQuestions8 SkillsProDec, 20 2025
0104

When you run a command that imports a package (like from geometry.shapes import ...), Python executes the code inside that package's __init__.py file once and only once during that session.

Here is exactly what happens when it runs:

1. The Execution Trigger

The moment any part of the package is touched for the first time, Python runs the __init__.py file.

  • If you run import geometry, it runs.
  • If you run from geometry.shapes import circle_area, it also runs before it even looks at shapes.py.

2. Namespace Creation

Everything defined in __init__.py (variables, functions, classes) becomes an attribute of the package object.

Example:
If you change your geometry/__init__.py to look like this:

print("Initializing the Geometry package!")
VERSION = "1.0.0"

Then when you run your script, you will see "Initializing the Geometry package!" printed in the terminal, and you could access geometry.VERSION in your main script.

3. Sub-module Management

If your package has many modules, you can use __init__.py to pre-load them. This is often used to "expose" functions so the user doesn't have to know the exact file structure.

For example, if you put this in geometry/__init__.py:

from .shapes import circle_area

The user can then call geometry.circle_area() directly, without needing to type geometry.shapes.circle_area().

Summary

Think of __init__.py as a "Welcome Mat" or a "Setup Script". It prepares the environment for the rest of your package to work correctly, ensuring that any necessary settings or shortcuts are ready before you start using the actual logic in files like shapes.py.

0 Comments

no data
Be the first to share your comment!