Courseiva
Question 505 of 169
Modules and PackagesmediumMultiple ChoiceObjective-mapped

Resolve Circular Imports Best Practice

A team is developing a large Python application with multiple modules. They encounter an ImportError when module A tries to import from module B, and module B tries to import from module A. What is the most likely cause and best practice to resolve this?

Quick Answer

The correct answer is to restructure the code to eliminate circular dependencies by extracting shared logic into a third module. This resolves the ImportError because Python’s module initialization is sequential—when module A begins importing module B, and B immediately tries to import the still-incomplete module A, Python raises an error due to the partially loaded namespace. On the PCAP exam, this question tests your understanding of Python’s import system and the principle of dependency inversion, often appearing as a scenario where lazy imports or moving imports inside functions are tempting but inferior solutions. The best practice for circular import resolution is always architectural: isolate the common functionality that both modules need into a separate, independent module that neither depends on. A common trap is thinking that rearranging import statements or using `importlib` fixes the root cause, but these only mask the design flaw. Memory tip: think “triangle to line”—if A and B form a loop, pull the shared code into C to create a clean, one-way dependency chain.

⚠ Common exam trap

Python Institute often tests the misconception that moving imports or using wildcard imports can fix circular dependencies, when in fact only restructuring the code or using lazy imports (as a temporary workaround) addresses the root cause.

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

Restructure the code to eliminate circular dependencies by extracting shared logic into a third module.

Circular imports occur when two modules depend on each other at the top level, causing an ImportError due to incomplete module initialization. The best practice is to restructure the code to eliminate the circular dependency, typically by extracting the shared functionality into a third module that both A and B can import without mutual dependence. This approach aligns with Python's module loading mechanism, which executes a module fully before making its names available for import.

Answer analysis

Option-by-option breakdown

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

  • Use 'from module import *' to bring all names into the namespace.

    Why it's wrong here

    Worse practice; can cause namespace pollution and still fails on circular import.

  • Use lazy imports (inside functions) to defer the import until runtime.

    Why it's wrong here

    Works but hides design flaws; not best practice for maintainability.

  • Restructure the code to eliminate circular dependencies by extracting shared logic into a third module.

    Why this is correct

    Best practice; removes the circular dependency entirely.

  • Move all imports from module A to the bottom of the file.

    Why it's wrong here

    Bottom imports do not change execution order; still circular.

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

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, 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. Which THREE of the following are recommended techniques to avoid circular imports in Python?

hard
  • A.Manually check sys.modules before importing.
  • B.Use the __all__ variable to control what is exported.
  • C.Restructure the code to move shared functionality into a separate module.
  • D.Use lazy imports inside functions or methods.
  • E.Use absolute imports instead of relative imports.

Why C: Moving shared functionality into a separate module breaks the circular dependency chain at the import level. When two or more modules depend on each other, Python's import system may raise an ImportError or produce partially initialized modules, leading to AttributeError. Restructuring eliminates the mutual dependency by creating a common module that both original modules can import safely.

Last reviewed: Jun 30, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

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.