Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Understanding the Role of __init__.py in Python Packages
📖 Scenario: You are organizing a small Python project with multiple modules. You want to make sure Python treats your folder as a package so you can import your modules easily.
🎯 Goal: Learn how to create a Python package by adding an __init__.py file and understand its role in package initialization and imports.
📋 What You'll Learn
Create a folder structure with modules
Add an __init__.py file to the package folder
Use __init__.py to import specific functions
Import and use functions from the package in a script
💡 Why This Matters
🌍 Real World
Python packages are used to organize code into reusable modules, making it easier to maintain and share.
💼 Career
Understanding <code>__init__.py</code> is essential for Python developers to create and manage packages professionally.
Progress0 / 4 steps
1
Create a package folder with two modules
Create a folder named mypackage and inside it create two Python files named module1.py and module2.py. In module1.py, write a function called greet that returns the string "Hello from module1". In module2.py, write a function called farewell that returns the string "Goodbye from module2".
Python
Hint
Each module is a separate file. Define one function per file as described.
2
Add an __init__.py file to the package
Inside the mypackage folder, create a file named __init__.py. In this file, import the greet function from module1 and the farewell function from module2.
Python
Hint
Use relative imports starting with a dot to import from sibling modules.
3
Use the package imports in a script
Create a new Python script outside the mypackage folder named test_package.py. In this script, import the greet and farewell functions directly from mypackage. Call both functions and store their results in variables named hello_message and bye_message.
Python
Hint
Import functions directly from the package name, not from individual modules.
4
Print the messages from the package functions
In test_package.py, add print statements to display the values of hello_message and bye_message.
Python
Hint
Use two print statements, one for each message variable.
Practice
(1/5)
1. What is the main role of a __init__.py file in a Python folder?
easy
A. To mark the folder as a Python package
B. To store global variables for the project
C. To execute the main program code
D. To compile Python files into bytecode
Solution
Step 1: Understand the purpose of __init__.py
The __init__.py file 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 A
Quick Check:
__init__.py marks 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
A. Create a file named __init__.py with no content
B. Create a file named init.py with no content
C. Create a file named __init__.py with a main() function
D. Create a file named __init__.txt with no content
Solution
Step 1: Identify the exact filename required
The file must be named exactly __init__.py to 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__.py with no content -> Option A
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]