PCAP Modules and Packages Practice Question
You are developing a package 'analytics' that contains subpackages 'stats' and 'ml'. The __init__.py of 'analytics' imports a function 'normalize' from 'analytics.stats'. When a user runs `import analytics`, they get an ImportError. Which change ensures the package imports correctly?
⚠ Common exam trap
Python Institute often tests the distinction between absolute and relative imports in packages, and the trap here is that candidates mistakenly think absolute imports like `from analytics.stats import normalize` are always safe, not realizing they depend on the package being installed or the parent directory being in `sys.path`.
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
✓
Change the import in analytics/__init__.py to: from .stats import normalize
It uses an explicit relative import (`from .stats import normalize`), which is the proper way to import from a subpackage within a package. Absolute imports like `from analytics.stats import normalize` can fail if the package's parent directory is not in `sys.path`, which is common when running scripts directly. Relative imports resolve correctly based on the package structure, ensuring the import works regardless of how the package is invoked.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Change the import to: from stats import normalize
Why it's wrong here
An unqualified `from stats import normalize` is an absolute import that instructs Python to locate a top-level module named `stats` on `sys.path`. Inside `analytics/__init__.py`, this will not automatically refer to `analytics/stats`; unless a standalone top-level `stats` package accidentally exists, the import raises `ModuleNotFoundError`. Even if it happens to resolve, it bypasses the package context and can bind the wrong module. The fix is to make the import package-relative.
- ✗
Add sys.path.append('.') before the import in __init__.py
Why it's wrong here
`sys.path.append('.')` mutates Python's global module search path at import time, adding the current working directory as a source for top-level modules. This is a fragile workaround: it only succeeds when the process happens to be launched from the directory that contains `stats`, it pollutes `sys.path` for every subsequent import, and it is explicitly discouraged in packaging guidance. It does not teach Python that `stats` lives inside `analytics`; it only forces a location-dependent lookup. The standard solution is a relative import, not path manipulation.
- ✓
Change the import in analytics/__init__.py to: from .stats import normalize
Why this is correct
`from .stats import normalize` is a relative import: the leading dot tells CPython's import machinery to start from the current package, `analytics`, and resolve `.stats` as `analytics.stats` (PEP 328). This works regardless of the absolute `sys.path` layout, whether `analytics` is installed as a package or invoked from a script, and it avoids name collisions with any unrelated top-level `stats` module. Since the statement appears in `analytics/__init__.py`, `.stats` is unambiguous and correctly loads the sibling submodule, making `normalize` available as `analytics.normalize`.
- ✗
Move the import statement to the stats/__init__.py file
Why it's wrong here
Relocating the import to `stats/__init__.py` changes the semantics: rather than importing `normalize` into the `analytics` package namespace, it would bind it only as an attribute of the `stats` submodule (e.g., `analytics.stats.normalize`), so code expecting `analytics.normalize` would still fail. Moreover, if the moved statement is still an absolute `from stats import normalize`, the same top-level resolution problem persists; if it is switched to relative, the import is now only executed when the `stats` submodule is imported, which is not guaranteed by simply importing `analytics`. The original failure is an import-path problem in `analytics/__init__.py`, not a missing import in the submodule, so moving it avoids rather than fixes the defect.
Visual reference
Go deeper
Related to this question
About these practice questions
This PCAP question is part of Courseiva's 169-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.