Courseiva
Modules and PackageseasyMultiple ChoiceObjective-mapped

PCAP Modules and Packages Practice Question

A developer creates a package named 'mypkg' with an __init__.py file. Inside the package, there is a module 'utils.py'. Which of the following is the correct way to import the function 'helper' from 'utils' from outside the package?

⚠ Common exam trap

Python Institute often tests the distinction between importing a module versus importing an attribute from a module, and the trap here is that candidates confuse the `import` statement (which only accepts modules/packages) with the `from ... import` statement (which can import any object), leading them to choose Option A or D.

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 mypkg.utils import helper

It uses the standard Python syntax for importing a specific name from a submodule within a package: `from package.module import name`. This directly imports the `helper` function into the current namespace, making it callable without any prefix. The `__init__.py` file marks `mypkg` as a package, and `utils.py` is a module inside it, so `from mypkg.utils import helper` is the proper way to access `helper` from outside the 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.

  • import mypkg.utils.helper

    Why it's wrong here

    This statement attempts to bind the top-level name `mypkg` in the namespace, but Python's import machinery does not automatically make `mypkg.utils.helper` accessible as a chained attribute just because the submodule was imported. Merely executing `import mypkg.utils.helper` does not create a reference to `helper`; you would still need to reference the full dotted path or `import mypkg.utils.helper as helper`. The syntax is legal, but it does not give you direct access to the function `helper` — you must use the full dotted path.

  • import mypkg; mypkg.utils.helper

    Why it's wrong here

    This two-step approach fails because the first import only binds `mypkg` (and possibly `mypkg.utils` if it was already imported, but not guaranteed). Without an explicit `import mypkg.utils` or `import mypkg.utils.helper`, the attribute `utils` is not automatically loaded as an attribute of `mypkg`; the package's `__init__.py` may not pre-import it. Thus, accessing `mypkg.utils.helper` raises `AttributeError` unless the package author has explicitly imported `utils` in `mypkg/__init__.py`. Even if it worked, it is fragile and depends on package internals, not a reliable import statement.

  • from mypkg.utils import helper

    Why this is correct

    This is the correct and idiomatic form because it explicitly names the submodule `utils` and the object `helper` in the `from ... import ...` syntax. The statement `from mypkg.utils import helper` tells Python to load `mypkg/utils` (the submodule) and then extract the attribute `helper` from that module's namespace, binding it locally as `helper`. This is the standard approach for importing a function or variable from a submodule directly, avoiding the need for dotted attribute chains.

  • from mypkg import utils.helper

    Why it's wrong here

    This is invalid syntax because the `from ... import ...` statement expects a list of attribute names, not a dotted path on the right side of the `import` keyword. The token `utils.helper` is parsed as an attribute expression, but Python's grammar only allows identifiers (or parenthesized lists of identifiers with aliases) after `import` in a `from` statement. To import from a submodule, you must put the dotted module path on the left (`from mypkg.utils`) and the single object on the right (`import helper`). Using `from mypkg import utils.helper` incorrectly treats `helper` as a submodule of `utils`, which is not allowed in import syntax.

Visual reference

Client Server SYN (seq=100) SYN-ACK (seq=200, ack=101) ACK (ack=201) Connection established — data transfer begins

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.