Question 360 of 511
Modules and PackagesmediumMultiple ChoiceObjective-mapped

Quick Answer

The answer is to move main.py to the parent directory of myapp and use from myapp.modules import utils, or run the script with python -m myapp.main from the parent directory. This is correct because when you run a Python script directly, Python adds the script’s own directory—myapp in this case—to sys.path, not its parent. Since modules is a subdirectory of myapp, Python cannot find it as a top-level module, triggering the ImportError. On the Certified Associate Python Programmer PCAP exam, this question tests your understanding of Python’s module search path and the difference between running a script directly versus using the -m flag, a common trap for candidates who assume the parent directory is automatically included. Remember the memory tip: “Direct run uses script’s folder; -m flag uses current folder.”

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.

You are a Python developer working on a project with the following structure:

myapp/ __init__.py main.py modules/ __init__.py utils.py helpers.py

The file main.py contains:

from modules import utils
from modules import helpers

utils.some_function() helpers.another_function()

When you run main.py, you get an ImportError: No module named 'modules'. However, the modules directory exists and both __init__.py files are present. The directory myapp is not installed as a package; you are running main.py directly from the myapp directory. What is the most likely cause and how should you fix it?

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.

Question 1mediummultiple choice
Study the full Python automation breakdown →

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

Move main.py to the parent directory of myapp and use 'from myapp.modules import utils' or run with 'python -m myapp.main' from the parent.

Option A is correct because when running main.py directly, Python adds the directory containing main.py (myapp) to sys.path, not its parent. Since 'modules' is a subdirectory of myapp, Python cannot find it as a top-level module. Moving main.py to the parent directory or using the -m flag (which sets the working directory as the script's location) allows 'from myapp.modules import utils' to resolve correctly, as myapp becomes a package.

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.

  • Move main.py to the parent directory of myapp and use 'from myapp.modules import utils' or run with 'python -m myapp.main' from the parent.

    Why this is correct

    This ensures myapp is treated as a package and imports are absolute.

    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.

  • Add the parent directory of myapp to sys.path at the beginning of main.py.

    Why it's wrong here

    Works but is not standard; better to structure properly.

  • Add an __all__ variable in modules/__init__.py to explicitly export the submodules.

    Why it's wrong here

    __all__ does not affect importability of submodules; it controls 'from module import *'.

  • Rename modules/__init__.py to something else.

    Why it's wrong here

    Removing __init__.py would make modules a namespace package, which might not help.

Common exam traps

Common exam trap: answer the scenario, not the keyword

Python Institute often tests the distinction between running a script directly (which adds the script's directory to sys.path) versus using the -m flag (which adds the current working directory), and candidates mistakenly think that having __init__.py files alone is sufficient for any import style.

Detailed technical explanation

How to think about this question

When a script is run directly, Python adds the script's directory to sys.path[0], not the parent. The -m flag executes a module as a script while setting sys.path[0] to the current working directory, allowing package-relative imports. This is why 'python -m myapp.main' from the parent directory works: Python finds myapp as a package and resolves 'modules' as a subpackage. In real-world projects, this pattern is essential for running scripts inside a package without modifying sys.path.

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.

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.

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: Move main.py to the parent directory of myapp and use 'from myapp.modules import utils' or run with 'python -m myapp.main' from the parent. — Option A is correct because when running main.py directly, Python adds the directory containing main.py (myapp) to sys.path, not its parent. Since 'modules' is a subdirectory of myapp, Python cannot find it as a top-level module. Moving main.py to the parent directory or using the -m flag (which sets the working directory as the script's location) allows 'from myapp.modules import utils' to resolve correctly, as myapp becomes a package.

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 →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 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. A Python project has the following directory structure: project/ __init__.py main.py subpackage/ __init__.py module.py Inside 'module.py', there is a function 'func' that needs to be imported in 'main.py'. The team wants to import 'func' using a relative import from 'main.py'. However, when they run 'python main.py' from the project root, they get an ImportError. What is the most likely reason?

hard
  • A.The 'project' directory does not contain an __init__.py file.
  • B.The function 'func' is not defined as a public function.
  • C.Relative imports cannot be used in scripts executed directly as __main__.
  • D.The module name 'module.py' contains a hyphen.

Why C: Option C is correct because when a Python script is executed directly (e.g., `python main.py`), its `__name__` is set to `"__main__"`, not the package name. Relative imports (e.g., `from .subpackage.module import func`) rely on the `__package__` attribute being set to the package hierarchy, which only happens when the module is imported as part of a package. Since `main.py` is run as the top-level script, it has no package context, causing an `ImportError`.

Last reviewed: Jun 30, 2026

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.

Loading comments…

Sign in to join the discussion.

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.