Challenge - 5 Problems
Init File Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Purpose of __init__.py in Python packages
What is the main role of the
__init__.py file in a Python package?Attempts:
2 left
💡 Hint
Think about how Python recognizes folders as packages.
✗ Incorrect
The __init__.py file tells Python that the folder is a package. It can also run setup code when the package is imported.
❓ Predict Output
intermediate2:00remaining
Output when importing a package with __init__.py
Given this package structure:
and
What is the output when running
mypkg/ __init__.py module.py
and
__init__.py contains:print('Package initialized')What is the output when running
import mypkg?Python
print('Package initialized') # inside __init__.py
Attempts:
2 left
💡 Hint
Code in __init__.py runs when the package is imported.
✗ Incorrect
When you import a package, Python runs the code inside __init__.py. Here, it prints 'Package initialized'.
❓ Predict Output
advanced2:00remaining
Effect of empty __init__.py on package import
What happens if
__init__.py is an empty file in a package folder and you run import mypackage?Attempts:
2 left
💡 Hint
Empty __init__.py files are allowed and common.
✗ Incorrect
An empty __init__.py file still marks the folder as a package. Importing it works fine without output or error.
🧠 Conceptual
advanced2:00remaining
Role of __init__.py in namespace packages
In Python 3.12+, what is the difference between a package with and without an
__init__.py file?Attempts:
2 left
💡 Hint
Think about how Python handles packages that share the same name in different places.
✗ Incorrect
Namespace packages allow splitting a package across folders without __init__.py. Regular packages have __init__.py and are contained in one folder.
🔧 Debug
expert3:00remaining
Why does importing a package fail without __init__.py?
You have a folder named
tools with Python files inside but no __init__.py. You try import tools and get ModuleNotFoundError. Why?Attempts:
2 left
💡 Hint
Consider Python versions and package recognition rules.
✗ Incorrect
Before namespace packages, Python required __init__.py to mark a folder as a package. Without it, import fails with ModuleNotFoundError.