PCAP Modules and Packages Practice Question
Consider the following directory structure: project/ main.py pkg/ __init__.py mod1.py subpkg/ __init__.py mod2.py From main.py, you write: from pkg.subpkg import mod2 Which THREE of the following are true regarding relative imports?
⚠ Common exam trap
Python Institute often tests the misconception that relative imports can be used from any module, including top-level scripts, or that `__init__.py` is always mandatory for a package; the trap here is that candidates may think `main.py` can use `from . import pkg` because it is in the same directory as `pkg/`, ignoring that relative imports require the importing module to be part of a package.
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
✓
Relative imports only work when the importing module is part of a package.
Relative imports (using dot notation like `..` or `.`) are only valid when the importing module is itself part of a package. This is because relative imports rely on the `__package__` attribute to resolve the import path; if the module is not inside a package (e.g., a top-level script), `__package__` is `None` or empty, and relative imports will raise an `ImportError`.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Relative imports only work when the importing module is part of a package.
Why this is correct
Relative imports are not intrinsically tied to the filesystem path of the file; they are only valid when the interpreter has assigned the importing module a non-empty `__package__` attribute, which happens only when that module is imported as part of a package (e.g., `import pkg.subpkg.mod2`). If you run a module directly, Python sets `__package__` to `None`, so a statement like `from . import sibling` fails with "attempted relative import with no known parent package" because there is no package context to anchor the leading dot.
- ✓
In mod2.py, you can use `from .. import mod1` to import mod1 from pkg.
Why this is correct
Because `mod2.py` lives inside `pkg/subpkg/`, its `__package__` attribute is set to `"pkg.subpkg"` when it is imported normally. In a relative import, each dot consumes one package level from the current `__package__`: one dot refers to `pkg.subpkg`, and two dots go up one level to `pkg`. Thus `from .. import mod1` asks Python to look inside the `pkg` package for a submodule named `mod1`, which is exactly where that module resides, so the import is valid.
- ✗
In main.py, you can use `from . import pkg` to import pkg.
Why it's wrong here
`main.py` sits at the project root and is intended to be run as a script (e.g., `python main.py`). When Python executes a script, it sets the `__name__` variable to `"__main__"` and `__package__` to `None`, so the leading dot in `from . import pkg` has no package name to resolve against; the dot is not a filesystem path shortcut. Even though `pkg` is a sibling directory, relative imports only work inside a package hierarchy, so this line would raise an `ImportError`; the correct approach is an absolute import such as `import pkg`.
- ✗
A directory must contain an __init__.py file to be a package for relative imports to function.
Why it's wrong here
This statement is too absolute because PEP 420 (introduced in Python 3.3) created implicit namespace packages, allowing directories without `__init__.py` to be treated as packages during import. For a *regular* package, an `__init__.py` is conventionally required, and in the given structure those files exist; however, the option claims it is a universal precondition for relative imports, which is false. More fundamentally, what makes a relative import legal is not the presence of `__init__.py` in the importing module's directory, but the importing module being loaded as part of a package with a properly set `__package__`.
- ✓
The dot notation is interpreted based on the module's __name__ and __package__ attributes.
Why this is correct
Python resolves relative imports by reading the importing module's `__package__` attribute, which stores the fully qualified package name, and by using `__name__` to identify whether the module is being run as a script or imported as part of a package. Combined, these attributes tell the import machinery exactly how many levels to ascend: one dot means the current package, two dots mean its parent, and so on. For example, if `__package__` is `"pkg.subpkg"`, then `from .. import mod1` is translated into an absolute import of `pkg.mod1`. This design is why modifying `__package__` manually or running modules with `-m` can change how relative imports behave.
Visual reference
Go deeper
Related to this question
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 →
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.