Courseiva
Modules and PackageshardMultiple ChoiceObjective-mapped

Why Sibling Packages Need Relative Imports in Python

A package 'mypackage' has subpackages 'sub1' and 'sub2'. In sub1/__init__.py, there is: from sub2 import helper. When importing mypackage, an ImportError occurs: No module named 'sub2'. What is the most likely cause?

Quick Answer

The correct choice is a relative import, specifically from .sub2 import helper, because when a sibling package uses a bare name like from sub2 import helper, Python searches the top-level module namespace rather than the parent package’s subpackages. Since sub2 is not installed as an independent top-level module, the import fails with an ImportError. This concept tests your understanding of Python’s module resolution order and the importance of explicit relative imports within packages, a common trap on the Certified Associate Python Programmer PCAP exam. The exam often presents a scenario where a subpackage tries to import from a sibling, and the trick is remembering that without a dot prefix, Python assumes an absolute import. A helpful memory tip: think of the dot as saying “look next to me in the same folder,” so from .sibling import module always resolves within the parent package.

⚠ Common exam trap

Python Institute often tests the distinction between absolute and relative imports in packages, trapping candidates who assume that sibling subpackages are automatically visible to each other without using dot-based relative imports.

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

The import should be from .sub2 import helper (relative import).

When a subpackage (sub1) tries to import from a sibling subpackage (sub2) using a bare name (from sub2 import helper), Python looks for 'sub2' as a top-level module, not as a sibling within the same parent package. Since 'sub2' is not installed as a top-level module, an ImportError occurs. Using a relative import (from .sub2 import helper) explicitly tells Python to look for sub2 as a sibling package under the same parent, resolving the import correctly.

Answer analysis

Option-by-option breakdown

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

  • Sub2 is not installed in the Python environment.

    Why it's wrong here

    sub2 is part of the package, not an external module.

  • Sub2 must be imported before sub1 in the package's __init__.py.

    Why it's wrong here

    Import order does not affect module resolution.

  • Sub1 should not have an __init__.py file.

    Why it's wrong here

    Subpackages require __init__.py.

  • The import should be from .sub2 import helper (relative import).

    Why this is correct

    Relative imports are required to locate sibling packages within a package.

Visual reference

Client Recursive Resolver Root DNS (13 root servers) TLD DNS (.com, .org, …) Authoritative example.com query IP addr answer

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 →

How Courseiva writes practice questions · Editorial policy

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. Given package structure: pack/__init__.py, pack/subpack/__init__.py, pack/subpack/mod.py. Inside pack/__init__.py, which import statement correctly imports mod.py using a relative import?

hard
  • A.from . import subpack.mod
  • B.from subpack import mod
  • C.from ..subpack import mod
  • D.from .subpack import mod

Why D: `from .subpack import mod` uses a leading dot to indicate a relative import from the current package (`pack`), then navigates into `subpack` and imports `mod`. This is the proper syntax for importing a module from a subpackage within the same parent package.

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.