PCAP Modules and Packages Practice Question
A package 'tools' has the structure: tools/__init__.py, tools/calc.py, tools/io.py. In __init__.py, the developer writes: from . import calc. A user tries: import tools; print(tools.add(1,2)). This fails with AttributeError. Why?
⚠ Common exam trap
Python Institute often tests the misconception that importing a submodule automatically makes its contents available at the package level, when in fact only the submodule name is added to the package namespace.
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
✓
The add function is not imported into the tools package namespace.
The `__init__.py` file only imports the `calc` module itself into the `tools` package namespace, not the `add` function. When a user calls `tools.add(1,2)`, Python looks for `add` directly in the `tools` namespace, but it is not there — it is only accessible as `tools.calc.add`. The `from . import calc` statement binds the name `calc` to the module, not its contents.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
The add function is not imported into the tools package namespace.
Why this is correct
The AttributeError occurs because `__init__.py` only imports the `calc` module itself (e.g., via `from . import calc`), so the package namespace of `tools` contains the name `calc`, but not `add`. In Python, the attributes of a package are exactly the names bound in its `__init__.py`; importing a submodule does not automatically copy that submodule's functions into the package's top level. Thus `tools.add` is an undefined attribute, while `tools.calc.add` works.
- ✗
The function add is not defined in calc.py.
Why it's wrong here
The scenario explicitly assumes that `add` is defined inside `calc.py`; if it were truly absent, the error would occur when accessing `tools.calc.add` (or during `from tools.calc import add`) with a message like `AttributeError: module 'tools.calc' has no attribute 'add'`. The actual error about `tools.add` points to a missing name at the package level, not a missing function definition in the submodule. The function exists; it is simply not bound in the `tools` package namespace.
- ✗
Relative imports are not allowed in __init__.py files.
Why it's wrong here
Relative imports are perfectly legal inside `__init__.py` files and are, in fact, the standard way for a package to import its own submodules in a layout-safe manner. A relative import such as `from . import calc` only raises an error in a top-level script that is not part of a package, or when the `__package__` context is missing. Since `tools` is a package and its `__init__.py` is executing with a known parent package, relative imports are valid and cannot be the source of the `AttributeError`.
- ✗
The user should use tools.calc.add instead.
Why it's wrong here
Recommending `tools.calc.add` is a workaround that sidesteps the error, but it does not explain why the original access `tools.add` fails. The question asks for the root cause, which is that `add` was never re-exported or aliased into the `tools` package namespace by `__init__.py`. To make `tools.add` work, one would need to add an explicit binding such as `from .calc import add` or `add = calc.add`; simply using a longer dotted path is not an explanation of the causal mechanism behind the error.
Go deeper
Related to this question
About these practice questions
This PCAP question is part of Courseiva's 421-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. 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.