PCAP Modules and Packages Practice Question
A Python package 'mypackage' contains the following hierarchy:
mypackage/ __init__.py subpackage1/ __init__.py module_a.py subpackage2/ __init__.py module_b.py
From a script outside the package, a programmer writes:
import mypackage.subpackage1.module_a
Which statement is true about the import?
⚠ Common exam trap
Python Institute often tests the misconception that dotted imports skip `__init__.py` execution or that only the final module is loaded, when in fact Python executes every `__init__.py` along the dotted path to ensure proper package initialization.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Both mypackage/__init__.py and mypackage/subpackage1/__init__.py are executed.
When Python encounters an import statement with a dotted path like `import mypackage.subpackage1.module_a`, it executes the `__init__.py` files for each package in the path in order: first `mypackage/__init__.py`, then `mypackage/subpackage1/__init__.py`. This is because Python must initialize each package before it can access its subpackages or modules. Option D correctly states that both `__init__.py` files are executed.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Only mypackage/__init__.py is executed.
Why it's wrong here
The import system initializes every package in the dotted path, so when importing `mypackage.subpackage1`, Python first loads `mypackage` and executes its `__init__.py`, then loads `subpackage1` inside that package and executes its own `__init__.py`. Skipping the subpackage's initializer would break package initialization because subpackages are separate namespaces requiring their own setup. Therefore, the statement is incorrect: both files run, not just the top-level one.
- ✗
No __init__.py files are executed because the import uses a dotted path.
Why it's wrong here
Dotted paths are the standard way to import submodules and subpackages, and they trigger the same import machinery as simple imports. Each component after the first is looked up as an attribute of the previous package, but the actual import of that component as a package involves executing its `__init__.py`. A dotted path does not bypass initialization; rather, it requires that each parent package be fully initialized first.
- ✗
After the import, 'mypackage' is not available as a name in the namespace.
Why it's wrong here
In Python, the statement `import mypackage.subpackage1` binds the top-level name `mypackage` in the current namespace, regardless of the depth of the dotted path. The subpackage `subpackage1` is not bound directly; it is accessible as `mypackage.subpackage1`, but the top-level package name is always available. Thus, the claim that `mypackage` is unavailable is false.
- ✓
Both mypackage/__init__.py and mypackage/subpackage1/__init__.py are executed.
Why this is correct
When importing `mypackage.subpackage1`, Python executes the `__init__.py` of each package along the dotted path to initialize them as proper packages. This happens because the import system processes each component sequentially: first `mypackage` is imported, which runs its `__init__.py`, then its submodule `subpackage1` is imported, which runs its own `__init__.py`. This two-step initialization is fundamental to Python's package system, ensuring parent packages are fully loaded before their subpackages.
Go deeper
Related to this question
About these practice questions
One of 169 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.