Courseiva
Modules and PackagesmediumMultiple ChoiceObjective-mapped

PCAP Modules and Packages Practice Question

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?

⚠ Common exam trap

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.

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.

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.

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.

  • 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.

Visual reference

Client Recursive Resolver Root DNS (13 root servers) TLD DNS (.com, .org, …) Authoritative example.com query IP addr answer

About these practice questions

One of 169 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 →

How Courseiva writes practice questions · Editorial policy

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.