Courseiva
Modules and Packages →mediumMultiple Select

PCAP Modules and Packages Practice Question

Which TWO statements about the __init__.py file in a Python package are true?

⚠ Common exam trap

Python Institute often tests the misconception that `__init__.py` is mandatory for all packages, including namespace packages, and that it must contain code to be valid, when in fact it can be empty and is optional for namespace packages.

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

✓

It can define the __all__ list to control what is exported with 'from package import *'.

The `__all__` list in `__init__.py` explicitly defines the public API of a package, controlling which names are exported when a client uses `from package import *`. This is a standard Python mechanism to prevent unintended internal modules from being exposed.

Answer analysis

Option-by-option breakdown

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

  • ✗

    It must contain at least one import statement to be valid.

    Why it's wrong here

    The __init__.py file is not required to contain imports to be valid; an empty file suffices to mark a directory as a regular package. Package initialization code can include imports, but the absence of any import statement does not affect the package's importability or validity.

  • ✗

    It is mandatory for all packages, including namespace packages.

    Why it's wrong here

    Since Python 3.3, namespace packages exist precisely to avoid requiring __init__.py; they are created automatically when a directory is on sys.path and contains no __init__.py. Thus, claiming that __init__.py is mandatory for all packages is false, because namespace packages are a valid, first-class package type that exists without it.

  • ✗

    It prevents submodules from being imported directly.

    Why it's wrong here

    The presence of __init__.py does not act as a barrier to importing submodules; you can still use `import package.submodule` or `from package import submodule` without issue. Its role is package initialization, not access control, so submodules remain importable directly regardless of whether the __init__.py file exists.

  • ✓

    It can define the __all__ list to control what is exported with 'from package import *'.

    Why this is correct

    Defining `__all__` in __init__.py is the canonical way to specify which names are exported when `from package import *` is used, effectively controlling the package's public API. Only the names listed in `__all__` (and not explicitly excluded) are bound in the importing namespace, allowing explicit control over what users can access via star imports.

  • ✓

    It is executed when the package is first imported.

    Why this is correct

    When the package is imported, the Python interpreter executes the code in its __init__.py within the package's namespace, which is where initialization logic like importing submodules, setting __version__, or configuring logging belongs. This execution happens only once because the module object is cached in sys.modules; subsequent imports refer to the cached object without re-running the file.

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.