PCAP Modules and Packages Practice Question
A package 'mypkg' has the following structure: mypkg/ __init__.py submod1.py submod2.py The __init__.py file contains: from . import submod1, submod2. A user runs 'import mypkg' and then 'mypkg.submod1.func()'. However, the user got an AttributeError. What is the most likely cause?
⚠ Common exam trap
Python Institute often tests whether candidates understand that an AttributeError on a module attribute (like a function) is distinct from an ImportError or ModuleNotFoundError, leading them to incorrectly suspect the import mechanism rather than the missing definition in the module.
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
✓
The submod1 module does not define a function named 'func'.
The AttributeError indicates that the attribute 'func' was not found on the module object 'submod1'. Since the import statement in __init__.py correctly makes submod1 accessible as mypkg.submod1, the only remaining reason for the error is that submod1.py does not define a function named 'func'. The import mechanism itself is working as intended.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
submod1 is actually a package, not a module, so it cannot be called with 'func()'.
Why it's wrong here
The claim confuses a package with an object's callability and is unsupported by the traceback. In Python, a package is also a module (with an __init__.py), and even if submod1 were a package, the error would name a missing attribute 'func' on that package, not complain about calling the package itself. The AttributeError shows the module object was found and imported; it simply lacks a top-level function named func. There is no evidence in the structure to classify submod1 as anything other than a regular module.
- ✗
The __init__.py should use absolute imports like 'import mypkg.submod1' to make submod1 accessible.
Why it's wrong here
Suggesting absolute imports as a fix misdiagnoses the failure. Relative imports such as 'from . import submod1' in __init__.py are perfectly valid and already execute, which is why mypkg.submod1 is accessible after import mypkg. An absolute import like 'import mypkg.submod1' would also bind the name, but it would not create a 'func' attribute inside submod1; the error occurs during attribute lookup, not during package import. Thus the choice of import style is irrelevant to the missing function.
- ✓
The submod1 module does not define a function named 'func'.
Why this is correct
This is the correct diagnosis because the Python error 'AttributeError: module 'mypkg.submod1' has no attribute 'func'' means the module was successfully loaded, but the requested attribute 'func' is simply not present at the module's top level. This can happen if the function was never defined, is misspelled, or was removed. Attribute lookup on a module object checks its namespace, and a missing key raises AttributeError — not ImportError. The fix is to define func in submod1 (or import it there) before calling it.
- ✗
The user must also run 'from mypkg import submod1' before accessing submod1.
Why it's wrong here
This is unnecessary because the package's __init__.py already contains an import that binds submod1 as an attribute of mypkg. When you execute 'import mypkg', Python runs __init__.py, and a statement like 'from . import submod1' adds 'submod1' to the package namespace, making 'mypkg.submod1' immediately available. Running 'from mypkg import submod1' is redundant and, more importantly, it would not address the AttributeError — that command only brings the already-imported module into the local namespace, not its missing 'func' attribute.
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.