Courseiva
Modules and Packages →easyMultiple Choice

PCAP Modules and Packages Practice Question

A developer wants to use a function 'calculate' from a module 'math_ops' that is located in a sibling directory '../shared/' relative to the current script. What is the correct way to import it using an absolute import assuming the shared directory is a package with __init__.py and the project root is in sys.path?

⚠ Common exam trap

Python Institute often tests the distinction between absolute and relative imports, and the trap here is that candidates confuse the dot notation for relative imports with absolute imports, or assume that '..' is valid syntax for absolute imports when it is not.

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 shared.math_ops import calculate

Absolute imports in Python use the project root as the base, not relative paths. Since the project root is in sys.path, 'from shared.math_ops import calculate' directly references the 'shared' package at the top level, regardless of the current script's location. This is the standard absolute import syntax as defined in PEP 328.

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 .shared.math_ops import calculate

    Why it's wrong here

    A single leading dot indicates a relative import that starts from the current package. This syntax is only valid inside a properly initialized package (where the module's __package__ attribute is set), so if the developer's script is executed directly as a top-level module, this raises ImportError: attempted relative import with no known parent package. Even if it did run inside a package, it would resolve to a shared package nested within the current package's parent, not the top-level shared package the developer intends. Since the goal is to import from the top-level shared package, this absolute-import scenario is incorrectly expressed as a relative import.

  • ✗

    from ..shared.math_ops import calculate

    Why it's wrong here

    Two leading dots mean the import is relative to two levels above the current package, so Python searches for shared.math_ops by first moving up two package levels and then looking for a submodule named shared. This requires the current module to be at least two package levels deep and have a fully defined package hierarchy; a standalone script run directly will not meet this requirement and will raise an error. Even in a correctly nested package, this resolves to a shared module that is a sibling of the package two levels up, not the top-level shared package on sys.path. Thus it fails both syntactically and semantically for the developer's intended absolute import.

  • ✓

    from shared.math_ops import calculate

    Why this is correct

    This is an absolute import: Python searches for the shared package by scanning the directories listed in sys.path, which normally includes the directory containing the script, the standard library, and site-packages. If shared is a top-level package in that path (e.g., a folder with an __init__.py in the project root or a namespace package), the import will succeed. This is the correct and recommended way to import from a top-level package because it is unambiguous and matches the developer's intent. The from ... import statement also directly binds the calculate function into the current namespace, making the call site simple and explicit.

  • ✗

    import ..shared.math_ops

    Why it's wrong here

    The import statement does not support relative imports with leading dots; PEP 328 specifies that relative imports are only allowed in the from package import module form. As written, import ..shared.math_ops will raise a SyntaxError because the parser does not accept dots as the first token of a module name after the import keyword. The valid relative-form equivalent would be from ...shared.math_ops import something, but even that would require a package context. In this scenario the developer needs an absolute import, so the correct statement would be import shared.math_ops or, better, the from form shown in the correct answer.

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 →

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.