PCAP Modules and Packages Practice Question
A programmer wants to create a package named 'analytics' with subpackages 'statistics' and 'ml'. Which directory structure correctly defines these packages?
⚠ Common exam trap
Python Institute often tests the misconception that only the top-level package needs an `__init__.py` file, or that subdirectories without `__init__.py` are still valid subpackages, leading candidates to pick option C or A.
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
✓
analytics/ (__init__.py), analytics/statistics/ (__init__.py), analytics/ml/ (__init__.py)
In Python, a directory is recognized as a package only if it contains an `__init__.py` file (even if empty). To create the 'analytics' package with 'statistics' and 'ml' subpackages, each directory must have its own `__init__.py`. Option D correctly places `__init__.py` in all three directories: `analytics/`, `analytics/statistics/`, and `analytics/ml/`.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
analytics/, analytics/statistics/ (__init__.py), analytics/ml/ (__init__.py)
Why it's wrong here
The top-level `analytics` directory lacks an `__init__.py` file, so Python will not recognize it as a package. Consequently, even though `statistics` and `ml` have `__init__.py`, they cannot be imported as subpackages of `analytics` because the parent package is not importable. A package must have `__init__.py` at every level, starting from the root, for the entire hierarchy to be traversable by the import system.
- ✗
analytics/ (__init__.py), analytics/statistics/ (__init__.py), analytics/ml/ (__init__.py), analytics/__init__.py (file)
Why it's wrong here
This structure is impossible and redundant because a directory cannot contain two different files with the identical name `__init__.py`. The list implies both a directory entry `analytics/ (__init__.py)` and a separate file `analytics/__init__.py` in the same location, which is a contradiction. Even if it were syntactically allowed, having multiple `__init__.py` files in one directory would be redundant and serve no purpose; the correct structure has exactly one `__init__.py` per package directory.
- ✗
analytics/ (__init__.py), analytics/statistics/, analytics/ml/
Why it's wrong here
Here the top-level `analytics` is correctly marked as a package, but the subdirectories `statistics` and `ml` are missing their own `__init__.py` files. Without those files, Python 3 treats them as ordinary directories, not as packages, so `import analytics.statistics` or `from analytics import ml` will fail with a ModuleNotFoundError. They are simply not included in the package's module namespace.
- ✓
analytics/ (__init__.py), analytics/statistics/ (__init__.py), analytics/ml/ (__init__.py)
Why this is correct
This is the canonical package layout: each directory—`analytics`, `statistics`, and `ml`—contains an `__init__.py` file, which marks it as a regular Python package. The `__init__.py` files are executed when the package is imported, and their presence allows the import system to traverse the hierarchy (e.g., `import analytics.ml`). This structure works in both Python 2 and Python 3 and is the required format for a standard (non-namespace) package.
Go deeper
Related to this question
About these practice questions
One of 421 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.