Challenge - 5 Problems
Package Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of importing a submodule
Consider this package structure:
In
What is the output of this code?
mypackage/
__init__.py
module_a.py
subpackage/
__init__.py
module_b.pyIn
module_b.py, there is a function def greet(): return 'Hello from B'.What is the output of this code?
from mypackage.subpackage import module_b print(module_b.greet())
Python
from mypackage.subpackage import module_b print(module_b.greet())
Attempts:
2 left
💡 Hint
Think about how Python imports functions from submodules inside packages.
✗ Incorrect
Importing the submodule
module_b from mypackage.subpackage works if the package structure is correct and greet() is defined there. Calling module_b.greet() returns the string as expected.❓ Predict Output
intermediate2:00remaining
Effect of __init__.py on package import
Given this package structure:
and
and
What is the output of this code?
mypackage/ __init__.py utils.py
and
__init__.py contains:from .utils import helper
and
utils.py contains:def helper():
return 'Helping'What is the output of this code?
import mypackage print(mypackage.helper())
Python
import mypackage print(mypackage.helper())
Attempts:
2 left
💡 Hint
Check what
__init__.py exposes when importing the package.✗ Incorrect
The
__init__.py imports helper from utils and makes it available as mypackage.helper. So calling mypackage.helper() works and returns 'Helping'.🔧 Debug
advanced2:00remaining
Why does this import fail?
You have this package structure:
In
You run this code from outside the package folder:
But it raises an error. What is the error?
mypackage/ __init__.py tools.py
In
tools.py you have:def tool():
return 'Tool here'You run this code from outside the package folder:
from mypackage import tool print(tool())
But it raises an error. What is the error?
Python
from mypackage import tool print(tool())
Attempts:
2 left
💡 Hint
Check what names are exposed by
mypackage when importing.✗ Incorrect
The package
mypackage does not expose tool directly because __init__.py does not import tool. So from mypackage import tool raises ImportError.📝 Syntax
advanced2:00remaining
Correct relative import syntax inside a package
Inside
Which import statement is correct?
mypackage/subpackage/module_c.py, you want to import func from mypackage/module_a.py.Which import statement is correct?
Attempts:
2 left
💡 Hint
Think about how many levels up you need to go to reach
module_a.✗ Incorrect
Since
module_c.py is inside subpackage, to import from module_a.py in the parent package, you use two dots .. to go up one level: from ..module_a import func.🚀 Application
expert2:00remaining
Number of modules loaded after import
Given this package structure:
Where:
-
-
What is the total number of distinct modules loaded into memory after running:
mypackage/
__init__.py
alpha.py
beta.py
gamma/
__init__.py
delta.pyWhere:
-
__init__.py in mypackage imports alpha and gamma-
__init__.py in gamma imports deltaWhat is the total number of distinct modules loaded into memory after running:
import mypackage
Python
import mypackageAttempts:
2 left
💡 Hint
Count the modules explicitly imported during the package import.
✗ Incorrect
Importing
mypackage runs its __init__.py, which imports alpha and gamma. Importing gamma runs its __init__.py, which imports delta. So modules loaded are: mypackage, alpha, gamma, and delta. Total 4 modules.