Courseiva

PCAP · topic practice

Modules and Packages practice questions

The Modules and Packages domain covers how Python locates, loads, and reuses code across files and directories. For PCAP you must read import statements, predict namespace effects, and reason about package __init__.py behavior. Questions present small module trees or import errors and ask you to identify the cause or correct outcome.

Courseiva uses original exam-style practice questions designed for learning and revision. The goal is to understand the concepts, recognise exam patterns, and improve through explanations — not memorise copied exam dumps.

Editorial oversight:Johnson Ajibi· MSc IT Security, IEEE Senior Member
20 questionsDomain: Modules and Packages

What the exam tests

What to know about Modules and Packages

Be able to trace Python's import search order, explain what __init__.py executes, and diagnose circular ImportErrors. The most important thing: know that the script directory precedes installed packages on sys.path, so local module names can shadow library modules.

The import search order: built-in modules, then sys.path entries including the script directory.

How __init__.py runs on package import and can re-export names via from package.module import name.

Circular imports where module A imports B while B imports A, causing ImportError or partial modules.

Isolating dependencies with virtual environments such as venv and pip, avoiding global conflicts.

Watch out for

Common Modules and Packages exam traps

  • ▸Assuming __init__.py is optional in older Python or that it must contain code; an empty file still marks a package.
  • ▸Believing sys.path starts with site-packages; the script's directory or current directory is searched first.
  • ▸Thinking a circular import always fails; it may succeed partially, leaving names undefined until later execution.

Practice set

Modules and Packages questions

20 questions · select your answer, then reveal the explanation

A developer creates a package named 'analytics' with the following structure:

analytics/ __init__.py stats.py models/ __init__.py regression.py

The developer wants the statement 'from analytics import *' to import only the functions 'mean' and 'std' from stats.py. What should be added to analytics/__init__.py?

A developer is creating a package that contains a subpackage. The subpackage has an __init__.py file that imports a module named 'helper' from the parent package. Which import statement in the subpackage's __init__.py will correctly import 'helper'?

Which TWO of the following are valid ways to import a module named 'math' and give it an alias 'm'?

You are a developer at a company that builds a data processing pipeline. The pipeline consists of several Python modules organized in a package called 'pipeline'. The package structure is:

pipeline/ __init__.py load.py transform.py analyze.py

The pipeline is deployed on a server where Python 3.8 is installed. The server also has a globally installed package called 'pipeline' (from a different project) in the site-packages directory. When you run your scripts that import 'pipeline', you get unexpected behavior because Python is importing the wrong package. You need to ensure that your local 'pipeline' package is used instead of the global one. You cannot uninstall the global package because it is used by another application. You have the following options:

A) Modify the PYTHONPATH environment variable to include the directory containing your 'pipeline' package before the site-packages directory. B) Rename your local 'pipeline' package to something else and update all imports. C) Use a virtual environment specific to your project and install your package there. D) Add an __init__.py file with a special import hook to override the global package.

Which course of action is the most appropriate and reliable?

Question 5mediummultiple choice
Study the full Python automation breakdown →

A developer is troubleshooting a Python application that fails to import a custom module named 'utils'. The file 'utils.py' exists in the same directory as the main script. Which of the following is the most likely cause of the import failure?

A Python package 'shapes' has the following __init__.py:

# shapes/__init__.py
from .circle import Circle
from .square import Square

A user writes:

import shapes

c = shapes.Circle(5)

This works correctly. However, when the user writes:

import shapes.circle

c = shapes.circle.Circle(5)

It fails with AttributeError: module 'shapes' has no attribute 'circle'. What is the most likely reason?

Question 7mediummultiple choice
Study the full Python automation breakdown →

You are working on a Python-based data processing pipeline that runs on a Linux server. The pipeline consists of several custom packages and modules located in /opt/mypipeline. The directory structure includes:

/opt/mypipeline/ __init__.py core/ __init__.py processor.py utils/ __init__.py logger.py

The main entry point is /opt/mypipeline/run.py, which imports modules from core and utils. The pipeline is executed using the command:

python /opt/mypipeline/run.py

Recently, the system administrator added a new Python package 'external_lib' to /opt/external_lib, and updated the PYTHONPATH environment variable to include /opt/external_lib. However, after this change, the pipeline fails to start with the following error:

ImportError: cannot import name 'process' from 'core.processor' (unknown location)

You check the PYTHONPATH and find it contains:

/opt/mypipeline:/opt/external_lib

The 'core.processor' module defines a function 'process' that is imported in run.py as:

from core.processor import process

The 'core' package has an __init__.py file. The 'external_lib' package also has a subpackage named 'core' with an __init__.py. What is the most likely cause of the import error?

Given the exhibit, why does `dir()` show no names from `mypackage` after executing `from mypackage import *`?

Exhibit

Refer to the exhibit.

```
# file: mypackage/__init__.py
from . import module1
from .module2 import func2
__all__ = ['module1', 'func2']
```

```
# file: mypackage/module2.py
def func2():
    print('func2')
```

```python
>>> from mypackage import *
>>> dir()
['__builtins__', ...]
```

A script is located in the parent directory of a package named 'mypackage'. Which import statement correctly imports the 'foo' module from the 'mypackage' package?

Refer to the exhibit. A script executes 'from mypackage import *'. Which functions are available in the global namespace?

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
Question 11mediummultiple choice
Study the full Python automation breakdown →

A Python application needs to load a configuration file that should be placed in the same directory as the main script. The developer uses the __file__ attribute to get the script's path. Which expression correctly obtains the directory containing the main script?

Which TWO of the following are valid ways to import a function 'foo' from a module 'bar' that is located in a package 'mypackage'?

Question 13mediummultiple choice
Study the full Python automation breakdown →

A Python script placed in /opt/myapp/script.py fails with ImportError when run from a cron job with the command: python /opt/myapp/script.py. The script works when run manually from the /opt/myapp/ directory. The script contains the line: from . import config. The config module is located in /opt/myapp/lib/config.py with an __init__.py in /opt/myapp/lib/. What is the most likely cause of the failure?

A developer appends a custom path to sys.path and then tries to import a function from a module: import sys; sys.path.append('/custom/libs'); from mylib import func. The import fails with ModuleNotFoundError. mylib is a directory. What is the most likely cause?

A developer runs pip install requests. Later, they need to find out where the module is installed on the system. Which command shows the location?

In module_b.py, the developer writes: from mypackage import module_a. When running a script that imports mypackage, an ImportError occurs. Which change should solve the issue?

Exhibit

Consider the following package structure:
mypackage/
    __init__.py
    module_a.py
    subpackage/
        __init__.py
        module_b.py

Two modules, 'module_a' and 'module_b', import each other. When 'module_a' is imported first, it tries to import 'module_b', which in turn tries to import 'module_a' again, causing a circular import error. Which of the following is the most effective strategy to resolve this circular dependency?

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'?

Which TWO of the following are valid ways to import a module 'math' in Python?

Which THREE of the following are recommended techniques to avoid circular imports in Python?

Free account

Track your progress over time

Create a free account to save your results and see which topics improve across sessions.

Focused Modules and Packages sessions

Start a Modules and Packages only practice session

Every question in these sessions is drawn from the Modules and Packages domain — nothing else.

Related practice questions

Related PCAP topic practice pages

Move into related areas when this topic feels solid.

Frequently asked questions

What does the PCAP exam test about Modules and Packages?
Be able to trace Python's import search order, explain what __init__.py executes, and diagnose circular ImportErrors. The most important thing: know that the script directory precedes installed packages on sys.path, so local module names can shadow library modules.
How should I use these practice questions?
Select your answer before revealing the explanation. Then read why each option is right or wrong — this active recall approach builds retention far faster than re-reading notes.
Can I practise just Modules and Packages questions in a focused session?
Yes — the session launcher on this page draws every question from the Modules and Packages domain. Use a 10-question session first to gauge your baseline, then move to 20 or 30 once the weak spots are clear.
Where can I practise other PCAP topics?
Use the topic links above to move to related areas, or go back to the PCAP question bank to see all topics.
Are these real exam questions or dumps?
These are original practice questions written to test the same concepts the PCAP exam covers. They are not copied from any real exam or dump site.