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
Recall & Review
beginner
What is a Python package?
A Python package is a folder that contains a special file called __init__.py and other Python files or subfolders. It helps organize code into groups so it is easier to find and use.
Click to reveal answer
beginner
What is the purpose of the __init__.py file in a package?
The __init__.py file tells Python that the folder should be treated as a package. It can be empty or contain code that runs when the package is imported.
Click to reveal answer
beginner
How do you import a module from a package?
Use the dot notation: <code>import package_name.module_name</code> or <code>from package_name import module_name</code>. This lets you use the code inside that module.
Click to reveal answer
intermediate
What is a subpackage in Python?
A subpackage is a package inside another package. It is a folder with its own __init__.py file inside the main package folder.
Click to reveal answer
beginner
Why is organizing code into packages helpful?
It keeps code neat and easy to find, helps avoid name conflicts, and makes it easier to share and reuse code in different projects.
Click to reveal answer
What file must be present in a folder for Python to recognize it as a package?
Asetup.py
Bmain.py
C__init__.py
Dpackage.py
✗ Incorrect
The __init__.py file marks a folder as a Python package.
How do you import the module tools from the package utils?
Aimport tools.utils
Bimport utils-tools
Cfrom tools import utils
Dimport utils.tools
✗ Incorrect
Use import utils.tools to import the tools module inside the utils package.
What is a subpackage?
AA module inside a package
BA package inside another package
CA file named <code>subpackage.py</code>
DA package without <code>__init__.py</code>
✗ Incorrect
A subpackage is a package folder inside another package folder.
Which statement is true about __init__.py?
AIt can contain code that runs when the package is imported
BIt must always be empty
CIt is only needed for Python 3.12+
DIt is used to run scripts
✗ Incorrect
__init__.py can be empty or contain initialization code for the package.
Why use packages in Python?
ATo organize code and avoid name conflicts
BTo make code run faster
CTo write code without indentation
DTo create executable files
✗ Incorrect
Packages help organize code and prevent name conflicts by grouping related modules.
Explain what a Python package is and how it is structured.
Think about folders and special files that group code.
You got /3 concepts.
Describe how to import a module from a package and why this is useful.
Consider how you tell Python where to find the code you want.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the __init__.py file in a Python package?
easy
A. To mark a directory as a Python package
B. To execute the main program
C. To store global variables
D. To compile Python files
Solution
Step 1: Understand the role of __init__.py
The __init__.py file tells Python that the directory should be treated as a package.
Step 2: Differentiate from other options
It does not execute the main program, store global variables, or compile files; its main role is package identification.
Final Answer:
To mark a directory as a Python package -> Option A
Quick Check:
__init__.py marks packages [OK]
Hint: Remember: __init__.py makes folder a package [OK]
Common Mistakes:
Thinking __init__.py runs main code automatically
Confusing it with a script file
Assuming it compiles Python files
2. Which of the following is the correct way to import the module utils from a package named mypackage?
easy
A. import mypackage.utils
B. from utils import mypackage
C. import utils.mypackage
D. from mypackage import utils
Solution
Step 1: Recall Python import syntax for packages
To import a module from a package, use from package_name import module.
Step 2: Match syntax to options
from mypackage import utils matches this syntax: from mypackage import utils.
Final Answer:
from mypackage import utils -> Option D
Quick Check:
Import module with 'from package import module' [OK]
Hint: Use 'from package import module' to import modules [OK]
Common Mistakes:
Reversing package and module names
Using incorrect import order
Trying to import module as package
3. Given this package structure:
mypackage/
__init__.py
math_ops.py
string_ops.py
And this code:
from mypackage import math_ops
print(math_ops.add(2, 3))
If math_ops.py contains a function def add(a, b): return a + b, what is the output?
medium
A. None
B. TypeError
C. 5
D. NameError
Solution
Step 1: Understand the import and function call
The code imports math_ops from mypackage and calls math_ops.add(2, 3).
Step 2: Evaluate the function output
The function add returns the sum of 2 and 3, which is 5.
Final Answer:
5 -> Option C
Quick Check:
2 + 3 = 5 [OK]
Hint: Trace function call and return value carefully [OK]
Common Mistakes:
Forgetting to call the function with parentheses
Confusing module and function names
Assuming import fails without __init__.py content
4. You have a package folder named tools with modules calc.py and format.py. You try to run:
from tools import calc
print(calc.multiply(4, 5))
But get ModuleNotFoundError. What is the most likely cause?
medium
A. Function multiply does not exist in calc.py
B. Missing __init__.py file in the tools folder
C. Syntax error in import statement
D. Python version is too old
Solution
Step 1: Understand ModuleNotFoundError cause
This error often means Python does not recognize the folder as a package.
Step 2: Check package structure requirements
Without __init__.py, Python won't treat 'tools' as a package, causing import failure.
Final Answer:
Missing __init__.py file in the tools folder -> Option B
Hint: Always add __init__.py to package folders [OK]
Common Mistakes:
Assuming function absence causes ModuleNotFoundError
Ignoring package folder structure
Blaming Python version without checking files
5. You want to create a package datautils with submodules clean.py and transform.py. You want users to import clean_data function directly from datautils like this:
from datautils import clean_data
Which code should you add to datautils/__init__.py to enable this?
hard
A. from .clean import clean_data
B. import clean_data from clean
C. from clean import clean_data
D. import clean.clean_data
Solution
Step 1: Understand relative imports in packages
Inside __init__.py, use relative import with dot: from .clean import clean_data.
Step 2: Confirm correct syntax for exposing functions
This syntax makes clean_data available directly from datautils.
Final Answer:
from .clean import clean_data -> Option A
Quick Check:
Use 'from .module import func' in __init__.py [OK]
Hint: Use relative import with dot in __init__.py [OK]
Common Mistakes:
Using absolute import without dot inside __init__.py
Wrong import syntax like 'import clean_data from clean'