nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
Questions
4 of 5
1What is the difference between requirements.txt and a modern pyproject.toml-based dependency specification?
2What is the difference between a module and a package in Python?
3What is the purpose of a virtual environment, and why should dependencies not be installed globally?
4What are circular imports, and how would you resolve one?
5How does Python resolve module imports, and what role does sys.path play?
PythonPython
Basics
Control Flow and Functions
Data Structures
Comprehensions & Functional Programming
Iterators, Generators & Decorators
Object-Oriented Programming
Exception Handling & Debugging
Concurrency & Parallelism
Performance & Optimization
Testing
Security
Modules, Packaging & Environment
Type Hinting & Modern Python
System Design & Architecture with Python
Best Practices & Design Patterns
Edge Cases & Tricky Interview Questions
04 / 05
nextRound

AI-powered interview preparation platform. Practice with curated questions, mock interviews, and personalized learning paths to crack your dream tech interview.

Quick Links

  • Technologies
  • Mock Interviews
  • Saved Questions
  • Pricing

Company

  • About Us
  • Contact Us

Legal

  • Privacy Policy
  • Terms of Use

© 2026 nextRound. All rights reserved.

What are circular imports, and how would you resolve one?

Difficulty: 8/10
Circular Imports, Import Errors, Refactoring

Circular imports occur when modules import each other; break the cycle with restructuring or deferred imports

A circular import happens when module A imports B and B imports A, directly or through a chain. Python starts executing A, reaches the import of B, starts executing B, which reaches the import of A. Since A is already in sys.modules but only partially initialized, the import may succeed but the name A is not yet defined, leading to an ImportError or an AttributeError later. The cleanest fix is to restructure so the cycle does not exist: move shared code into a third module that both can import, or use dependency injection so one module receives the other as a parameter instead of importing it. If restructuring is not practical, defer the import into the function or method where it is needed, so it runs after both modules have finished loading. Using from future import annotations or TYPE_CHECKING for type-only imports also avoids cycles at runtime.

  1. 1

    Restructure: extract shared code into a lower-level module that neither A nor B depends on circularly.

  2. 2

    Defer: move the import inside the function that needs it, so it happens at call time, not import time.

  3. 3

    Type-only: use from typing import TYPE_CHECKING and guard imports that are only needed for annotations.

  4. 4

    Dependency injection: pass the collaborator as an argument rather than importing the module.

  5. 5

    Use importlib.import_module for dynamic, late-bound imports.

  6. 6

    Common mistake: hiding the cycle with local imports everywhere, which makes the code harder to follow and can hide design problems.

  7. 7

    Common mistake: assuming a partial import is safe because it did not raise. The name may be defined later, causing failures far from the import site.

  8. 8

    Version note: circular imports behave the same across Python 3 versions. PEP 563 (postponed annotations) was optional and is not the default; 3.11+ still evaluates annotations unless from future import annotations is used.

Scenario Questions

0-2 years experience

  1. 1You see ImportError: cannot import name 'X' from partially initialized module. What does that mean?
  2. 2How do you break a simple two-module cycle?

2-5 years experience

  1. 1Two modules import each other and the import fails only in some execution orders. How do you make it deterministic?
  2. 2You need to use a class from module B inside a function in module A, but A and B form a cycle. What is the minimal fix?

5-8 years experience

  1. 1You have a large codebase with many cycles. How do you detect them systematically and prioritize fixes?
  2. 2A circular import appears only in production because of import order differences. How do you reproduce and fix it?

8+ years experience

  1. 1Design a module architecture that prevents cycles by construction, including dependency direction rules and enforcement in CI.
  2. 2Explain how to use import graphs, static analysis, and runtime tracing to eliminate cycles in a legacy monolith without a big-bang rewrite.

Follow-up Questions

  • Why can a circular import sometimes succeed but fail later with an AttributeError?
  • When is a local import the right solution versus a design smell?
Sharethis question

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.