Courseiva
Modules and Packages →mediumMultiple Choice

PCAP Modules and Packages Practice Question

A developer has a project structure with 'my_package/' containing '__init__.py', 'module_a.py', and 'sub_package/' (with its own '__init__.py'). They want to import function 'foo' from 'module_a' inside a script in 'sub_package' using a relative import. Which statement is correct?

⚠ Common exam trap

Python Institute often tests the distinction between relative and absolute imports, and the trap here is that candidates confuse the dot notation ('.' for current package, '..' for parent) with file system paths, leading them to pick 'from .module_a import foo' thinking it refers to the parent directory.

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

✓

from ..module_a import foo

The script is inside 'sub_package/', which is one directory level below 'my_package/'. To import from 'module_a' (located in the parent package 'my_package'), a relative import uses '..' to go up one package level, then specifies the module name. Thus, 'from ..module_a import foo' correctly navigates the package hierarchy.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    from .module_a import foo

    Why it's wrong here

    A single leading dot resolves to the current package, which here is sub_package, not my_package. Attempting `from .module_a import foo` makes Python search for `module_a` inside `sub_package`; if `module_a` actually lives in the parent `my_package`, the import will raise ModuleNotFoundError. To reach the parent, you need two dots, not one.

  • ✓

    from ..module_a import foo

    Why this is correct

    Two leading dots in a relative import ascend one level in the package hierarchy: from a module inside `sub_package`, `..` refers to `my_package`. Therefore `from ..module_a import foo` asks Python to locate `module_a` as a sibling of `sub_package` within that parent package, which exactly matches the described structure. This is the correct way to import `foo` without relying on an absolute top-level path.

  • ✗

    from my_package.module_a import foo

    Why it's wrong here

    This is an absolute import that assumes `my_package` is a top-level package on `sys.path`, not a relative import anchored to the current file's location. If the script is executed from inside the project hierarchy, Python may not have the project root on the search path, causing an ImportError; even when it works, it couples the code to a specific distribution name. For a sibling module inside the same package tree, the relative form is required by the premise of the question.

  • ✗

    from ..my_package.module_a import foo

    Why it's wrong here

    Two dots already move the import context to the parent package `my_package`, so adding `my_package` again makes the target `my_package.my_package.module_a`. Python will interpret this as looking for a package named `my_package` nested inside the very same parent package, which does not exist in the given structure. Thus this path overshoots and then misnames the final location.

About these practice questions

This PCAP question is part of Courseiva's 421-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 →

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.