- A
The __init__.py file in the lib directory is empty and should contain imports.
Why wrong: An empty __init__.py works to mark a directory as a package.
- B
The lib directory is not in sys.path when the script is run from cron.
Why wrong: The script's directory is added to sys.path, so lib as a subdirectory is accessible.
- C
Relative imports are not allowed in a script that is executed directly because its __name__ is not set to a package name.
When a script is run directly, it is treated as __main__, not as part of a package, so relative imports fail.
- D
The cron job uses a different Python interpreter that does not have the required standard library.
Why wrong: This would affect standard libraries, not custom modules.
Quick Answer
The correct answer is that relative imports fail in directly executed scripts because the script’s `__name__` is set to `'__main__'` instead of a package name. When you run `python /opt/myapp/script.py`, Python treats the file as the top-level entry point, and relative imports like `from . import config` require the importing module to be part of a package with a proper `__name__` reflecting the package hierarchy—without that, the interpreter cannot resolve the dot. This is a classic Certified Associate Python Programmer PCAP exam trap: candidates often assume the script works because it runs from the correct directory, but the real issue is that relative imports are only valid inside a package loaded with `-m` or imported from another module. The exam tests your understanding of Python’s module system and the `__name__` variable. Memory tip: “Main means no dot—when `__name__` is `'__main__'`, relative imports are in vain.”
PCAP Modules and Packages Practice Question
This PCAP practice question tests your understanding of modules and packages. The scenario asks you to isolate a root cause — eliminate options that address a different problem before choosing. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.
A Python script placed in /opt/myapp/script.py fails with ImportError when run from a cron job with the command: python /opt/myapp/script.py. The script works when run manually from the /opt/myapp/ directory. The script contains the line: from . import config. The config module is located in /opt/myapp/lib/config.py with an __init__.py in /opt/myapp/lib/. What is the most likely cause of the failure?
Clue words in this question
Noticing these words before you look at the options changes how you read each choice.
Clue:
"most likely"Why it matters: Probability qualifier — the question wants the most probable cause or outcome, not a guaranteed one. Eliminate low-probability options.
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
Relative imports are not allowed in a script that is executed directly because its __name__ is not set to a package name.
Option C is correct because when a Python script is executed directly (e.g., `python /opt/myapp/script.py`), its `__name__` is set to `'__main__'`, not to a package name. Relative imports (like `from . import config`) require the importing module to be part of a package with a proper `__name__` reflecting the package hierarchy. Since the script is run as the top-level entry point, the relative import fails with an `ImportError`. This explains why the script works when run manually from `/opt/myapp/` (if the working directory is set appropriately, but the relative import still fails unless the script is run as a module with `-m`), but fails from cron where the working directory is typically the user's home directory.
Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.
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 the lib directory is empty and should contain imports.
Why it's wrong here
An empty __init__.py works to mark a directory as a package.
- ✗
The lib directory is not in sys.path when the script is run from cron.
Why it's wrong here
The script's directory is added to sys.path, so lib as a subdirectory is accessible.
- ✓
Relative imports are not allowed in a script that is executed directly because its __name__ is not set to a package name.
Why this is correct
When a script is run directly, it is treated as __main__, not as part of a package, so relative imports fail.
Clue confirmation
The clue word "most likely" in the question point toward this answer.
Related concept
Read the scenario before looking for a memorised answer.
- ✗
The cron job uses a different Python interpreter that does not have the required standard library.
Why it's wrong here
This would affect standard libraries, not custom modules.
Common exam traps
Common exam trap: answer the scenario, not the keyword
Python Institute often tests the distinction between running a script directly (`python script.py`) versus running it as a module (`python -m package.script`), and the trap here is that candidates mistakenly blame `sys.path` or `__init__.py` contents instead of recognizing that relative imports are fundamentally incompatible with direct script execution.
Detailed technical explanation
How to think about this question
Under the hood, Python's import system uses the `__name__` attribute to resolve relative imports. When a script is run directly, `__name__` is `'__main__'`, which has no package context, so relative imports are disallowed (PEP 328). To run a script with relative imports, it must be executed as a module using `python -m myapp.script` from the parent directory, which sets `__name__` to `'myapp.script'` and allows the package hierarchy to be resolved. In real-world deployments, this is a common pitfall when scripts are invoked from cron or systemd without using the `-m` flag.
KKey Concepts to Remember
- Read the scenario before looking for a memorised answer.
- Find the constraint that changes the correct option.
- Eliminate answers that are true in general but not in this case.
TExam Day Tips
- Watch for words such as best, first, most likely and least administrative effort.
- Review why wrong options are wrong, not only why the correct option is correct.
Key takeaway
Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.
Real-world example
How this comes up in practice
A cloud solutions architect for a retail company is evaluating services for a new workload. The correct answer here reflects best practice for the specific scenario described — not a general cloud recommendation. Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option. Cloud exam questions reward reading the constraint carefully: the same technology can be right or wrong depending on the use case.
What to study next
Got this wrong? Here's your next step.
Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.
- →
Modules and Packages — study guide chapter
Learn the concepts, then practise the questions
- →
Modules and Packages practice questions
Targeted practice on this topic area only
- →
All PCAP questions
511 questions across all exam domains
- →
Certified Associate Python Programmer PCAP study guide
Full concept coverage aligned to exam objectives
- →
PCAP practice test guide
How to use practice tests most effectively before exam day
Related practice questions
Related PCAP practice-question pages
Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.
Modules and Packages practice questions
Practise PCAP questions linked to Modules and Packages.
Strings practice questions
Practise PCAP questions linked to Strings.
Object-Oriented Programming practice questions
Practise PCAP questions linked to Object-Oriented Programming.
Exceptions and File I/O practice questions
Practise PCAP questions linked to Exceptions and File I/O.
PCAP fundamentals practice questions
Practise PCAP questions linked to PCAP fundamentals.
PCAP scenario practice questions
Practise PCAP questions linked to PCAP scenario.
PCAP troubleshooting practice questions
Practise PCAP questions linked to PCAP troubleshooting.
Practice this exam
Start a free PCAP practice session
Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.
FAQ
Questions learners often ask
What does this PCAP question test?
Modules and Packages — This question tests Modules and Packages — Read the scenario before looking for a memorised answer..
What is the correct answer to this question?
The correct answer is: Relative imports are not allowed in a script that is executed directly because its __name__ is not set to a package name. — Option C is correct because when a Python script is executed directly (e.g., `python /opt/myapp/script.py`), its `__name__` is set to `'__main__'`, not to a package name. Relative imports (like `from . import config`) require the importing module to be part of a package with a proper `__name__` reflecting the package hierarchy. Since the script is run as the top-level entry point, the relative import fails with an `ImportError`. This explains why the script works when run manually from `/opt/myapp/` (if the working directory is set appropriately, but the relative import still fails unless the script is run as a module with `-m`), but fails from cron where the working directory is typically the user's home directory.
What should I do if I get this PCAP question wrong?
Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.
Are there clue words in this question I should notice?
Yes — watch for: "most likely". Probability qualifier — the question wants the most probable cause or outcome, not a guaranteed one. Eliminate low-probability options.
What is the key concept behind this question?
Read the scenario before looking for a memorised answer.
About these practice questions
Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →
Same concept, more angles
2 more ways this is tested on PCAP
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. Refer to the exhibit. Given the project structure, which of the following import statements in main.py would cause an ImportError?
medium- A.from utils import strings
- ✓ B.from ..utils import helpers
- C.from utils import helpers
- D.from utils.strings import format
Why B: Option B uses a relative import with '..' which is only valid inside a package (i.e., when the module is loaded as part of a package and has a __package__ attribute set). In a flat project structure where main.py is a top-level script, '..' attempts to go above the top-level package, which is not allowed and raises an ImportError. Python's import system requires that relative imports be used only within a package hierarchy.
Variation 2. Refer to the exhibit. Which import statement in app.py will successfully import and use the greet function from helpers.py?
easy- A.import utils.helpers; then greet()
- B.from utils.helpers import greet
- ✓ C.from .utils.helpers import greet
- D.from my_package.utils.helpers import greet
Why C: Option C is correct because in a package structure, a relative import (using a leading dot) is required to import from a sibling module within the same package. The dot (.) refers to the current package, so `from .utils.helpers import greet` correctly imports the `greet` function from `helpers.py` located in the `utils` subpackage relative to `app.py`.
Last reviewed: Jun 30, 2026
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.
Question Discussion
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
Sign in to join the discussion.