Controlling `from package import *` with __all__
Exhibit
Exhibit: # file: mypackage/__init__.py __all__ = ["module_a", "module_b"] # file: mypackage/module_a.py def func(): pass # file: mypackage/module_b.py def func(): pass
Refer to the exhibit. A script executes 'from mypackage import *'. Which functions are available in the global namespace?
Quick Answer
The answer is that no functions are directly available in the global namespace because `__all__` lists modules, not functions. When you execute `from mypackage import *`, Python checks the package’s `__init__.py` for the `__all__` list, and here it contains `['module_a', 'module_b']`—these are module names, so only the modules themselves are imported, not their individual functions like `func`. This tests your understanding of how `__all__` controls the `from package import *` wildcard import, a key concept in the Certified Associate Python Programmer PCAP exam that often appears as a trick question. A common trap is assuming `__all__` exposes functions directly, but it only governs what names are brought into scope at the package level. Remember the memory tip: `__all__` is a bouncer for the package door—it lets in whole modules, not their contents.
⚠ Common exam trap
Many exam-takers assume `__all__` must contain function or variable names, but it can also list submodule names, and `import *` only imports those listed names—not their nested contents—into the global namespace.
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
✓
None, because __all__ lists modules, not functions
When `from mypackage import *` is executed, Python looks for the `__all__` list in the package's `__init__.py` file. In this exhibit, `__all__` is defined as `['module_a', 'module_b']`, which are module names, not function names. The `import *` statement imports the modules listed in `__all__` into the global namespace, not their individual functions. Therefore, `func` from either module is not directly available; you would need to reference them as `module_a.func` or `module_b.func`.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Only func from module_a
Why it's wrong here
Incorrect. The modules themselves are imported, not their functions.
- ✗
It raises an ImportError because __all__ should contain function names
Why it's wrong here
No, __all__ can contain any names; it is perfectly valid to list modules.
- ✓
None, because __all__ lists modules, not functions
Why this is correct
Correct. __all__ defines what names are imported; here it imports the modules, so functions remain in the module namespace.
- ✗
func from both module_a and module_b
Why it's wrong here
Incorrect. Only the module objects are imported, not their contents.
Go deeper
Related to this question
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 →
Same concept, more angles
1 more way this is tested on PCAP
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. A package 'shapes' has an __init__.py file. Which statement must be included in the __init__.py file to allow the syntax 'from shapes import *' to import all submodules 'circle', 'square', and 'triangle'?
medium- A.Set __all__ to an empty list
- ✓ B.__all__ = ['circle', 'square', 'triangle'] and import each submodule
- ✓ C.Import each submodule in __init__.py without defining __all__
- ✓ D.__all__ = ['circle', 'square', 'triangle']
Why B: A wildcard import from a package imports all non-underscore names in the package namespace when __all__ is not defined. Importing each submodule in __init__.py places those names in the package namespace, so 'from shapes import *' will import them. Defining __all__ also works because Python imports the listed submodules. Thus options B, C, and D all satisfy the requirement.
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.