Concept Flow - __init__ file role
Create folder
Add __init__.py file
Python treats folder as package
Import modules from package
Use package contents in code
The __init__.py file tells Python to treat a folder as a package, allowing imports from it.
my_package/ __init__.py module.py # In main.py from my_package import module module.hello()
| Step | Action | Effect | Result |
|---|---|---|---|
| 1 | Create folder 'my_package' | Folder created | Empty folder |
| 2 | Add empty __init__.py file | Folder becomes a package | my_package is a package |
| 3 | Add module.py with function hello() | Module ready inside package | module.py available |
| 4 | In main.py, import module from my_package | Python loads package and module | module imported |
| 5 | Call module.hello() | Function runs | Prints greeting message |
| 6 | End of program | Execution stops | Program ends |
| Variable | Start | After Step 2 | After Step 4 | After Step 5 | Final |
|---|---|---|---|---|---|
| my_package | folder | package | package with module | package with module | package with module |
| module | none | none | imported module | module function called | module loaded |
__init__.py file role in Python: - Marks a folder as a package - Can be empty or contain setup code - Enables importing modules from the folder - Without it, imports from folder fail - Essential for package structure