Courseiva
Modules and PackagesmediumMultiple ChoiceObjective-mapped

PCAP Modules and Packages Practice Question

A developer is writing a package that contains multiple modules. The package should allow users to import it directly and have all commonly used functions available at the package level. For example, after `import mypackage`, the user should be able to call `mypackage.func1()` without needing to import submodules. Which is the best way to achieve this?

⚠ Common exam trap

Python Institute often tests the distinction between `__all__` (which controls `from package import *` behavior) and actual imports in `__init__.py` (which populate the package namespace), causing candidates to mistakenly believe that `__all__` alone makes functions accessible at the package level.

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

In `__init__.py`, import the desired functions from the submodules, e.g., `from .submodule import func1`.

`__init__.py` is executed when a package is imported, and importing functions from submodules into `__init__.py` makes them directly accessible as attributes of the package object. This allows `mypackage.func1()` to work without requiring the user to import submodules explicitly, satisfying the requirement of a flat namespace at the package level.

Answer analysis

Option-by-option breakdown

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

  • Create a wrapper function in `__init__.py` that delegates calls to the submodule functions.

    Why it's wrong here

    A wrapper function in `__init__.py` that manually delegates calls to submodule functions would require the developer to write and maintain explicit forwarding code for every function, which does not scale and violates DRY principles. It is tempting because `__init__.py` is the correct place to define package-level imports, and in a scenario where only a few functions need selective exposure or custom preprocessing, such manual delegation would be the correct choice.

  • Include `__all__` in each submodule and ensure `__init__.py` is empty.

    Why it's wrong here

    Placing `__all__` inside a submodule changes only how that submodule behaves under a wildcard import (`from submodule import *`); it has no effect on the package's attributes. With an empty `__init__.py`, importing the package does not automatically load submodules or copy their names, so `package.func1` will raise `AttributeError` unless the caller separately imports the submodule. The package namespace is not populated by declarations in child modules.

  • In `__init__.py`, import the desired functions from the submodules, e.g., `from .submodule import func1`.

    Why this is correct

    Importing the desired functions directly into `__init__.py` with relative imports, e.g. `from .submodule import func1`, binds those names in the package's namespace at import time. This is the canonical re-export pattern: after this line, `import package; package.func1` and `from package import func1` both succeed, while the submodule remains accessible as `package.submodule`. It gives the package a stable public API without duplicating logic.

  • Define a list named `__all__` in the package's `__init__.py` that lists the functions.

    Why it's wrong here

    A package-level `__all__` list is merely an export declaration used by `from package import *`; it is not a mechanism for creating names. Unless the listed functions are actually imported or defined in `__init__.py`, they do not exist as attributes of the package, so `import package; package.func1` still fails. Additionally, `__all__` has no effect on ordinary attribute access or on explicit `from package import func1`, which requires `func1` to be present in the package namespace.

About these practice questions

Courseiva writes every PCAP question from scratch — 169 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or 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.