The __init__.py file tells Python that a folder is a package. It helps organize code into groups.
__init__ file role in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
# __init__.py can be empty or contain code # Example: initialize a variable message = 'Hello from package' # Example: import submodules from . 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
Python
# Empty __init__.py file # Just create an empty file named __init__.py inside the folder
Python
# __init__.py with a variable message = 'Welcome to my package!'
Python
# __init__.py importing submodules from . import tools from . import helpers
Sample Program
This example shows how __init__.py imports a function from a submodule so you can call it directly from the package.
Python
# Folder structure: # mypackage/ # __init__.py # greet.py # Content of greet.py: # def say_hello(): # print('Hello from greet module!') # Content of __init__.py: from .greet import say_hello # main.py (outside mypackage folder): import mypackage mypackage.say_hello()
Important Notes
Without __init__.py, Python 3.3+ treats folders as implicit namespaces, but explicit __init__.py is still recommended for clarity.
You can put setup code in __init__.py that runs when the package is imported.
Summary
__init__.py marks a folder as a Python package.
It can be empty or contain code to run on import.
It helps organize and control package imports.
Practice
1. What is the main role of a
__init__.py file in a Python folder?easy
Solution
Step 1: Understand the purpose of
The__init__.py__init__.pyfile tells Python that the folder should be treated as a package.Step 2: Differentiate from other file roles
It does not store global variables, run main code, or compile files; its role is to mark the folder as a package.Final Answer:
To mark the folder as a Python package -> Option AQuick Check:
__init__.pymarks package = C [OK]
Hint: Remember:
__init__.py means 'this is a package' [OK]Common Mistakes:
- Thinking it runs main program code
- Confusing it with a config file
- Assuming it compiles Python files
2. Which of the following is a correct way to create an empty
__init__.py file in a package folder?easy
Solution
Step 1: Identify the exact filename required
The file must be named exactly__init__.pyto mark the folder as a package.Step 2: Confirm that it can be empty
The file can be empty; no code is required inside for it to work.Final Answer:
Create a file named__init__.pywith no content -> Option AQuick Check:
Empty__init__.pyfile = B [OK]
Hint: Filename must be exactly
__init__.py [OK]Common Mistakes:
- Using wrong filename like init.py or __init__.txt
- Adding unnecessary code inside when empty is fine
- Confusing with main.py or other files
3. Given this folder structure and files:
What will be the output when running
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())What will be the output when running
main.py?medium
Solution
Step 1: Understand import behavior
Whenimport mypackageruns, the code inside__init__.pyexecutes, printing 'Package imported'.Step 2: Check subsequent import and function call
Thenfrom mypackage import moduleimports the module, andprint(module.greet())prints 'Hello!'.Final Answer:
Package imported Hello! -> Option BQuick Check:
Init runs first, then greet() output = A [OK]
Hint: Code in
__init__.py runs on package import [OK]Common Mistakes:
- Assuming
__init__.pycode 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
Solution
Step 1: Understand package import requirements
Python requires an__init__.pyfile in a folder to treat it as a package for imports likeutils.helper.Step 2: Identify cause of ModuleNotFoundError
If__init__.pyis missing, Python does not recognizeutilsas a package, causing the error.Final Answer:
Theutilsfolder is missing__init__.py-> Option DQuick Check:
Missing__init__.pycauses 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
Solution
Step 1: Understand relative imports in packages
To import a subpackage inside a package's__init__.py, use relative import syntax likefrom . import polygons.Step 2: Avoid absolute import inside the package
Usingimport polygonsorimport shapes.polygonsmay cause errors or circular imports; relative import is preferred.Step 3: Confirm that empty
Python does not import subpackages automatically; explicit import is needed.__init__.pydoes not import subpackagesFinal Answer:
Addfrom . import polygonsinsideshapes/__init__.py-> Option CQuick Check:
Use relative importfrom . 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__.pyin subpackage
