PCAP Modules and Packages Practice Question
A Python package 'analytics' contains a subpackage 'models' with module 'regression.py'. Inside 'regression.py', there is a function 'linear_fit' that depends on 'numpy'. The developer wants to ensure that 'numpy' is imported only once and available throughout the package. Where should the import 'import numpy as np' be placed?
⚠ Common exam trap
The trap is the misconception that package-level __init__.py imports are necessary to avoid multiple imports or to share names across modules. In reality, Python's caching prevents redundant execution, and name visibility is not automatic.
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 each module that uses numpy, add 'import numpy as np'.
To use numpy in regression.py, that module must have its own import statement, e.g., 'import numpy as np'. Python caches modules in sys.modules, so loading happens only once regardless of where the import statement appears. Thus, repeating 'import numpy as np' in each module that needs it is both correct and efficient, and it respects namespace isolation.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
In the '__init__.py' of the 'models' subpackage.
Why it's wrong here
Putting the import in models/__init__.py binds numpy only as an attribute of the models package object (models.np), and that code runs only when models is explicitly imported. Sibling subpackages and the analytics root will not see np unless they execute their own imports, so this fails to provide a shared package-wide namespace. It also couples numpy use to the models subpackage, making availability accidental if models isn't imported.
- ✓
In each module that uses numpy, add 'import numpy as np'.
Why this is correct
Adding 'import numpy as np' in every module that uses numpy is the standard explicit approach, but it only populates that module's local namespace with np. It does not define numpy as a package attribute such as analytics.np, and it does not establish a single import point that all package code can rely on. If the version or alias ever changes, every module must be edited individually, which defeats the goal of a shared common import.
- ✗
In the '__init__.py' of the 'analytics' top-level package.
Why it's wrong here
When the analytics package itself is imported, its __init__.py is executed first, so importing numpy there binds it to the top-level package namespace as analytics.np. This gives all subpackages and modules a single, canonical reference (from analytics import np or analytics.np) without repeating the import in each file. It also ensures numpy is imported only once per Python process, regardless of how many package components are loaded.
- ✗
In a separate file 'common_imports.py' and import that file everywhere.
Why it's wrong here
Creating a common_imports.py module only moves the import to yet another namespace; each consumer still needs to import from that module, and without 'from common_imports import np' the name np won't be visible at all. This adds an extra file for no benefit, as the standard idiom for package-level shared imports is to declare them in the package's __init__.py. It also means importing common_imports has no effect on the analytics package namespace unless explicitly re-exported.
Go deeper
Related to this question
About these practice questions
One of 421 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 →
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.