Courseiva
Modules and Packages →hardMultiple Choice

PCAP Modules and Packages Practice Question

A developer is creating a Python package named 'utils' and wants to control what is imported when a user writes 'from utils import *'. Which file and variable should be defined?

⚠ Common exam trap

Python Institute often tests the misconception that `__all__` can be defined in the importing script or as a function, or that it belongs in a separate module file rather than the package's `__init__.py`.

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

✓

Define __all__ = ['mod1', 'mod2'] in '__init__.py' of the 'utils' package.

In Python, the `__all__` variable in the `__init__.py` file of a package explicitly defines the list of module names that are exported when a user writes `from utils import *`. This controls the public API of the package and prevents unintended internal modules from being imported.

Answer analysis

Option-by-option breakdown

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

  • ✗

    Define __all__ as a function that returns the list of modules in '__init__.py'.

    Why it's wrong here

    The __all__ attribute is not a factory or callback; it must be a sequence of string names, such as a list or tuple. During a from utils import * statement, the import machinery reads __all__ and iterates over it to decide which names to bind. If you assign a function, Python will attempt to iterate over the function object and raise TypeError, because functions are not iterable in this context. There is also no mechanism for Python to invoke a function to lazily compute the public API.

  • ✓

    Define __all__ = ['mod1', 'mod2'] in '__init__.py' of the 'utils' package.

    Why this is correct

    Placing __all__ = ['mod1', 'mod2'] in the package's __init__.py is the canonical way to declare the package's public interface. When from utils import * is executed, the __init__.py code runs first, and the import system reads the __all__ list from the package's namespace, importing only the listed submodules. This also aids static analysis tools and IDE autocomplete by making the intended exports explicit, and prevents unintended names from leaking out in wildcard imports.

  • ✗

    Set the __all__ variable in the script that imports the package.

    Why it's wrong here

    The __all__ variable only takes effect when defined in the module or package that is the target of a star import; it is not a consumer-side setting. A script that simply assigns __all__ = [...] in its own global namespace creates an unrelated global variable and has no influence on the utils package's exports. Even assigning utils.__all__ after the package is imported does not retroactively change a completed from utils import * and is not the documented pattern, since the package itself should own this declaration.

  • ✗

    Define __all__ as a list of module objects in the main module 'utils.py'.

    Why it's wrong here

    For a package, the correct location is __init__.py, not a separate module file called utils.py, because the package's namespace and __all__ live in __init__.py. In addition, __all__ must contain strings — the names of the submodules or attributes — not module objects. When from utils import * is processed, Python uses __all__ entries as attribute names to look up in the package dictionary; if you put module objects there, the star import would fail (or behave unexpectedly) because it expects name strings, and the API contract would be broken.

About these practice questions

Courseiva writes every PCAP question from scratch — 421 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.