PCAP Modules and Packages Practice Question
Exhibit
Traceback (most recent call last):
File "main.py", line 1, in <module>
from mypackage import mymodule
ImportError: cannot import name 'mymodule' from 'mypackage' (unknown location)Refer to the exhibit. Which of the following is the most likely cause of this error?
⚠ Common exam trap
Python Institute often tests the distinction between a package (directory with __init__.py) and a module (single .py file), trapping candidates who assume any directory can be imported as a package without the required __init__.py marker.
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
✓
mypackage is a module file, not a package directory.
The error indicates that Python cannot import 'mypackage' as a package. If 'mypackage' is a single module file (e.g., mypackage.py) rather than a directory containing an __init__.py file, Python treats it as a module, not a package. This prevents the expected package-style import of submodules like 'mymodule', causing the ImportError.
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 __init__.py file in mypackage is empty.
Why it's wrong here
An empty __init__.py is not a problem: it is the standard way to mark a directory as a Python package, and the file may legitimately contain nothing. The existence of __init__.py makes the directory importable, after which `from mypackage import mymodule` resolves mymodule by looking for a submodule or an attribute defined in the package. An empty __init__.py does not suppress submodule discovery, so it cannot be the reason the import fails.
- ✗
There is a circular import between mypackage and mymodule.
Why it's wrong here
A circular import between a package and its own submodule would produce a different error, typically a partial initialization message like 'cannot import name X from partially initialized module' or simply AttributeError at import time. Here the error names mymodule from mypackage, but if mypackage were a real package, mymodule would be a separate module object, and circular imports would not cause mypackage to lose that submodule. The 'cannot import name' error with an unknown location points instead to mypackage being a plain module, not a circular dependency.
- ✗
mymodule.py does not exist in mypackage directory.
Why it's wrong here
If mymodule.py were absent from a genuine package, Python would raise `ModuleNotFoundError: No module named 'mypackage.mymodule'`, not `ImportError: cannot import name`. In a package, a missing submodule is a module-not-found condition because the import system searches the package's filesystem for the module file. The current error message explicitly says 'cannot import name from mypackage', which names mymodule as a missing attribute rather than a missing file, so the absent-file explanation does not fit.
- ✓
mypackage is a module file, not a package directory.
Why this is correct
When mypackage is a single-file module, it contains no namespace for submodules, so `from mypackage import mymodule` treats mymodule as an attribute that must exist in that file. Since no such attribute is defined, the import machinery raises `ImportError: cannot import name 'mymodule' from 'mypackage'` with the file location of the module. This is the most likely cause because the traceback location is the mypackage module itself, not a package directory, and it aligns with how Python distinguishes modules from packages.
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.