PCAP Modules and Packages Practice Question
Exhibit
Refer to the exhibit. ``` # package/__init__.py from . import submodule # package/submodule.py from . import __init__ # main.py import package ``` Error: ``` ImportError: cannot import name '__init__' from partially initialized module 'package' (most likely due to a circular import) ```
What is the most likely cause of this error?
⚠ Common exam trap
The PCAP exam often tests the misconception that import errors are caused by missing `__all__` or incorrect execution directory, when the actual root cause is a circular dependency between `__init__.py` and a submodule that creates a partially initialized module state.
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
✓
Circular import between __init__.py and submodule.py.
A circular import occurs when `__init__.py` and `submodule.py` attempt to import each other (directly or indirectly) before either module is fully initialized. In Python, when a module is being imported, it is added to `sys.modules` as a partially initialized module; if another module tries to import it during that time, it may receive an incomplete or empty module object, leading to an `ImportError` or `AttributeError`. This is a common cause of import failures in packages where initialization logic in `__init__.py` depends on submodules that themselves import from the package root.
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 package's __init__.py is missing a __all__ definition.
Why it's wrong here
Missing __all__ never raises an ImportError for a direct import. The __all__ list only controls the behavior of 'from package import *', filtering which public attributes are pulled. When a submodule or package module is imported explicitly, Python loads it normally regardless of __all__. The traceback described is specifically a circular-import symptom, not a missing-star-export problem.
- ✗
The main.py is executed from a directory outside the package.
Why it's wrong here
If main.py were executed from a directory outside the package, Python would insert the script's own directory at sys.path[0] and would not find the package at all, producing a ModuleNotFoundError. The error message 'cannot import name "xxx" from partially initialized module' appears only after the package is found and is being initialized. Therefore this location issue cannot produce the reported ImportError; a circular dependency is the actual cause.
- ✗
The submodule.py attempts to import a module named '__init__' which is reserved.
Why it's wrong here
__init__ is not a reserved word or a forbidden import target in Python; it is simply the conventional file name for a package's initialization code. You can technically write 'from . import __init__' and it will resolve to that module. The error is not about the name's legality but about the timing: __init__ is only partially initialized when submodule.py runs, so the import fails.
- ✗
The submodule.py uses an invalid relative import syntax.
Why it's wrong here
The relative import syntax 'from . import __init__' is perfectly valid in Python 3; the leading dot refers to the current package. It is not a syntax error, so the interpreter would not raise this ImportError on those grounds. The issue is that the import request is executed during the package's own initialization, causing a circular import where the requested module is present in sys.modules but lacks the needed attributes.
- ✓
Circular import between __init__.py and submodule.py.
Why this is correct
This is exactly what produces the traceback. When __init__.py runs, it imports submodule.py; submodule.py then executes 'from . import __init__', which returns the partially initialized package module already sitting in sys.modules. Because the package's attribute (e.g., a class or function) has not yet been defined, Python raises ImportError: cannot import name that from a partially initialized module. The cycle is between the package initializer and one of its own submodules.
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.