The __init__.py file tells Python that a folder is a package. It helps organize code into groups.
When you want to group related Python files into one package.
When you want to import code from one folder easily.
When you want to run setup code automatically when a package is imported.
When you want to control what is available when someone imports your package.
Syntax
Python
# __init__.py can be empty or contain code# Example: initialize a variable
message = 'Hello from package'# Example: import submodulesfrom . import module1
from . import module2
The __init__.py file must be inside the folder you want to treat as a package.
It can be empty or have Python code that runs when the package is imported.
Examples
This makes Python treat the folder as a package without extra setup.
Python
# Empty __init__.py file# Just create an empty file named __init__.py inside the folder
This variable can be accessed when the package is imported.
Python
# __init__.py with a variable
message = 'Welcome to my package!'
This lets you import submodules directly from the package.
When import mypackage runs, the code inside __init__.py executes, printing 'Package imported'.
Step 2: Check subsequent import and function call
Then from mypackage import module imports the module, and print(module.greet()) prints 'Hello!'.
Final Answer:
Package imported
Hello! -> Option B
Quick Check:
Init runs first, then greet() output = A [OK]
Hint: Code in __init__.py runs on package import [OK]
Common Mistakes:
Assuming __init__.py code does not run
Mixing order of printed lines
Expecting ImportError without reason
4. You have a folder named utils with a file helper.py inside. You try to import helper using import utils.helper but get ModuleNotFoundError. What is the most likely cause?
medium
A. The helper.py file has syntax errors
B. The helper.py file is empty
C. You need to run Python with administrator rights
D. The utils folder is missing __init__.py
Solution
Step 1: Understand package import requirements
Python requires an __init__.py file in a folder to treat it as a package for imports like utils.helper.
Step 2: Identify cause of ModuleNotFoundError
If __init__.py is missing, Python does not recognize utils as a package, causing the error.
Final Answer:
The utils folder is missing __init__.py -> Option D
Quick Check:
Missing __init__.py causes import error = D [OK]
Hint: Always add __init__.py to folders for imports [OK]
Common Mistakes:
Blaming syntax errors without checking
Thinking admin rights affect imports
Assuming empty files cause import failure
5. You want to create a package shapes with subpackage polygons. You want importing shapes to automatically import polygons as well. How should you modify shapes/__init__.py to achieve this?
hard
A. Add import shapes.polygons inside shapes/__init__.py
B. Add import polygons inside shapes/__init__.py
C. Add from . import polygons inside shapes/__init__.py
D. Leave shapes/__init__.py empty; Python imports subpackages automatically
Solution
Step 1: Understand relative imports in packages
To import a subpackage inside a package's __init__.py, use relative import syntax like from . import polygons.
Step 2: Avoid absolute import inside the package
Using import polygons or import shapes.polygons may cause errors or circular imports; relative import is preferred.
Step 3: Confirm that empty __init__.py does not import subpackages
Python does not import subpackages automatically; explicit import is needed.
Final Answer:
Add from . import polygons inside shapes/__init__.py -> Option C
Quick Check:
Use relative import from . import polygons = A [OK]
Hint: Use relative import from . import subpackage in __init__.py [OK]
Common Mistakes:
Using absolute import inside __init__.py
Expecting automatic subpackage import
Forgetting to add __init__.py in subpackage
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.