Python traps · Chapter 13 of 16

A circular import

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Time
  • 3 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python traps are free. This one comes with the all labs plan.

Why you need this

So the cycle only bites when one particular file is imported first, which makes the entry point decide. Every quick test that happened to import the "good" file first passed. The "consider renaming" hint is Python 3.13 guessing at the usual cause of this error; when the folder is on PYTHONPATH instead, the same failure reads cannot import name 'RIDERS' from partially initialized module 'riders' (most likely due to a circular import), which is the honest version.

Three common ways to break a cycle:

  • import the module and look the name up when a function runs (zones.ZONES inside the function, not a name copied at import time);
  • import inside the function that needs it, so the import runs when both files are complete;
  • move what both need into a third module that imports neither.
# zones.py, importing inside the function
ZONES = {"west": ["Andheri", "Bandra"], "east": ["Powai", "Chembur"]}


def riders_in(zone):
    from riders import RIDERS
    return [name for name, area in RIDERS.items() if area in ZONES[zone]]
$ python3 -c "import riders, zones; print(zones.riders_in('east'))"
['Vikas Yadav']

Catching the ImportError is not one of them: the program starts, and the module is left without the name it needed.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.py: the worker's ImportError, on purpose

    Write reproduce() in repro.py. Inside it, not at the top of the file, import menu and then orders, the way worker.py does, inside a try. Return "ok" when both imports work, and the ImportError's message, str(error), when one fails.

    The check runs your reproduce() twice, each time in a fresh python3 where nothing is imported yet: beside the original orders.py and menu.py it must return the message, which starts with cannot import name 'PRICES' from, and beside fixed files it must return "ok". So it must really import the files, menu first. No real waiting and no input(): reproduce() must finish within 3 seconds.

  2. Task 2

    orders.py: the worker starts, whichever file loads first

    Fix the cycle between orders.py and menu.py, in either file or both. A fresh Python that imports menu and then orders (as worker.py does) must work, one that imports orders and then menu (as web.py does) must work, and so must importing either one alone.

    Keep order_total(dishes) (the sum of the dishes' prices), label(order_id, dishes) ("D2041: dal-tadka, roti, roti (₹170)"), PRICES, THALIS and thali_price (10% off the thali's dishes, rounded down) as they are, and do not change web.py or worker.py. The check imports your files both ways round, each in a fresh python3, and works out its own totals, labels and thali prices. Catching the ImportError is not a fix: orders.py is left without prices, and the first total fails.

  3. Task 3

    test_orders.py: a test that would have caught it

    Write a pytest file, test_orders.py, that would have caught this bug before the worker reached production. The check runs your file three times: beside the original orders.py and menu.py, where at least one test must fail; beside the fixed files, where every test must pass; and beside a wrong fix that catches the ImportError and leaves orders.py with an empty price list, where at least one test must fail.

    Inside one pytest run the modules are imported once, so each test starts a fresh python3 with subprocess, in the test file's folder, imports the files in one order, works out a total and prints it; the test asserts it exited with code 0 and printed the right number. Test both orders: menu first and orders first. No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.py: the worker's ImportError, on purpose
  • orders.py: the worker starts, whichever file loads first
  • test_orders.py: a test that would have caught it

More in Time

Build "A circular import" yourself

DabbaDrop runs on Python, and Kunal, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Python traps are free. This one comes with the all labs plan.