Question 257 of 169
PCAP Modules and Packages Practice Question
A developer creates a package 'mypackage' with the following structure:
mypackage/ __init__.py module1.py module2.py
The __init__.py contains:
from mypackage.module1 import func1 from mypackage.module2 import func2
__all__ = ['func1', 'func2']
In a separate script, the developer writes:
from mypackage import * print(func1())
This works as expected. However, when the developer runs the same script from a different directory (not the one containing mypackage), the import works but the script prints an error that func1 is not defined. What could be the problem?
⚠ Common exam trap
The PCAP exam often tests the distinction between regular packages (with `__init__.py`) and namespace packages (without `__init__.py` in Python 3.3+), trapping candidates who assume that a directory containing a package structure always executes its `__init__.py` regardless of file presence or validity.
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 mypackage directory lacks proper __init__.py (maybe it is not present or invalid), causing it to be treated as a namespace package, and the __init__.py is never executed.
If the `mypackage` directory is found but its `__init__.py` is missing, invalid, or not executed (e.g., due to being a namespace package in Python 3.3+), the `from mypackage import *` statement will not trigger the imports defined in `__init__.py`. Consequently, `func1` and `func2` are never bound in the package namespace, leading to a `NameError` when the script tries to call `func1()`. This scenario occurs when the package is located via `sys.path` but the `__init__.py` is not properly processed, often because the directory is treated as a namespace package (PEP 420) rather than a regular package.
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 current working directory is not in sys.path, so the package cannot be found.
Why it's wrong here
If the current working directory were missing from `sys.path`, the initial `import mypackage` would raise `ModuleNotFoundError` before any attribute access could be attempted. The fact that the import succeeds proves the package directory itself is locatable, so the symptom of the missing function must arise from something happening (or not happening) during package initialization. Therefore the working directory cannot be the cause.
- ✗
The __all__ variable hides func1 because it does not include it, but it does.
Why it's wrong here
`__all__` in `mypackage/__init__.py` governs only the behavior of `from mypackage import *`, not ordinary attribute access via `mypackage.func1`. Even if `func1` were omitted from `__all__`, it would not be hidden — the module-level name simply would not be re-exported by a star import. Moreover, the actual `__all__` shown includes `func1` and `func2`, so this option is factually wrong as well.
- ✓
The mypackage directory lacks proper __init__.py (maybe it is not present or invalid), causing it to be treated as a namespace package, and the __init__.py is never executed.
Why this is correct
For a directory to be a regular package, Python requires a valid `__init__.py`; when that file is missing, Python 3.3+ treats the directory as a namespace package. A namespace package executes no initialization code, so the `from mypackage.func1 import func1` lines that would normally populate the package namespace never run. The package is still importable, but it appears empty — exactly matching the failure to find `func1` while `import mypackage` succeeds.
- ✗
The imports in __init__.py are relative imports and fail when run from a different directory.
Why it's wrong here
The statements in the intended `__init__.py` are absolute imports — they begin with the fully qualified package name (`mypackage.func1`), not with a dot. Absolute imports resolve against `sys.path` and the package's absolute name, so they are unaffected by the current working directory. A relative import like `from .func1 import func1` is required for the failure mode described, but that is not what the code contains.
About these practice questions
Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →
Last reviewed: Jul 4, 2026
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.
Question Discussion
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
Sign in to join the discussion.