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.
Jump into concepts and practice - no test required
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
__init__.py file in a Python folder?__init__.py__init__.py file tells Python that the folder should be treated as a package.__init__.py marks package = C [OK]__init__.py means 'this is a package' [OK]__init__.py file in a package folder?__init__.py to mark the folder as a package.__init__.py with no content -> Option A__init__.py file = B [OK]__init__.py [OK]mypackage/
__init__.py
module.py
# __init__.py content:
print('Package imported')
# module.py content:
def greet():
return 'Hello!'
# main.py content:
import mypackage
from mypackage import module
print(module.greet())main.py?import mypackage runs, the code inside __init__.py executes, printing 'Package imported'.from mypackage import module imports the module, and print(module.greet()) prints 'Hello!'.__init__.py runs on package import [OK]__init__.py code does not runutils with a file helper.py inside. You try to import helper using import utils.helper but get ModuleNotFoundError. What is the most likely cause?__init__.py file in a folder to treat it as a package for imports like utils.helper.__init__.py is missing, Python does not recognize utils as a package, causing the error.utils folder is missing __init__.py -> Option D__init__.py causes import error = D [OK]__init__.py to folders for imports [OK]shapes with subpackage polygons. You want importing shapes to automatically import polygons as well. How should you modify shapes/__init__.py to achieve this?__init__.py, use relative import syntax like from . import polygons.import polygons or import shapes.polygons may cause errors or circular imports; relative import is preferred.__init__.py does not import subpackagesfrom . import polygons inside shapes/__init__.py -> Option Cfrom . import polygons = A [OK]from . import subpackage in __init__.py [OK]__init__.py__init__.py in subpackage