PCAP Modules and Packages Practice Question
A Python project uses namespace packages spread across multiple directories. The package structure is: project/ and lib/ both contain subdirectories 'mypkg/'. Each has an __init__.py file. When importing 'mypkg', which directory's contents are used?
⚠ Common exam trap
Many candidates confuse regular packages (with __init__.py) with namespace packages (without __init__.py), assuming Python merges both regardless, when in fact the presence of __init__.py prevents merging and triggers first-found-wins behavior.
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
✓
Only the directory that appears first in sys.path is used.
When both directories contain an __init__.py file, they are regular packages, not namespace packages. Python's import system uses the first match in sys.path; it does not merge regular packages. Therefore, only the directory that appears first in sys.path is used, making option B correct.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Both directories are merged into a single namespace package.
Why it's wrong here
Both directories contain an __init__.py file, which makes them regular packages, not namespace packages. Python merges directory portions into a namespace package only when none of the directories have an __init__.py, per PEP 420. Since these are regular packages, the import system treats the first matching directory as the package and never combines the two. Therefore, the merging described is impossible in this scenario.
- ✓
Only the directory that appears first in sys.path is used.
Why this is correct
When Python imports a package, the path-based finder scans sys.path from index 0 upward and uses the first directory that contains a matching __init__.py file. That directory is bound to the package name, and the search stops immediately, so later directories are never examined. This deterministic first-match resolution ensures predictable behavior even when multiple copies of the same package exist on the path.
- ✗
An error is raised due to ambiguous import.
Why it's wrong here
The existence of two directories with the same package name at different sys.path entries is not considered ambiguous and does not raise any error. The import system is designed to resolve such situations by selecting the first match, so no ImportError or any other exception occurs. Ambiguity would only arise from unrelated issues like syntax errors or conflicting names within the same directory, not from duplicate package directories. Thus, the import proceeds silently using the first found package.
- ✗
The last directory in sys.path overrides the previous.
Why it's wrong here
sys.path is traversed in ascending order, starting at index 0, so the earliest matching directory takes precedence over any later entries. A later directory cannot override an earlier match; it is only reached if no earlier directory contains the requested package. If you wanted the later directory to win, you would have to reorder sys.path or remove the earlier conflicting entry. This is a common source of subtle bugs when outdated packages linger in site-packages.
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.