Skip to the solution
HardDSA16 min read

How does the Python import system work?

902 views

The problem

Question presented to candidate: "Walk me through what actually happens when Python runs import foo.bar. Where does it look, what objects are involved, why does a module's code run only once, and why do circular imports sometimes fail with 'partially initialized module'?"

What a strong answer should cover:

  • import is a run-time statement (IMPORT_NAME in bytecode), not a textual include; the first stop is the sys.modules cache, which is why a module body executes only once per process
  • On a cache miss, each finder on sys.meta_path is asked find_spec(name, path); the defaults are BuiltinImporter, FrozenImporter and PathFinder, which walks sys.path (or the parent package __path__) using path-entry finders from sys.path_hooks, cached in sys.path_importer_cache
  • The finder returns a ModuleSpec (name, loader, origin, submodule_search_locations); the loader creates the module, it is put in sys.modules before the body runs, then exec_module runs the code; on error the entry is removed
  • Dotted imports import each parent first (foo, then foo.bar using foo.__path__); import foo.bar binds foo, from foo import bar binds bar; relative imports are resolved against __package__
  • Circular imports work only when the half-initialised module already has the names being asked for; fixes are importing the module instead of names, moving the import, or restructuring
  • Source files are compiled to __pycache__/*.cpython-3XX.pyc, revalidated by source mtime and size by default (hash-based pycs since 3.7, PEP 552); importlib.reload re-executes into the same object

Clarifying questions expected:

  • Are we talking about CPython's default machinery, or a custom setup (zip apps, frozen apps, a custom finder)?
  • Is the interest in how to debug a specific import error (wrong module picked up, circular import)?

Code / implementation expected: Optional โ€” a custom MetaPathFinder / Loader, or a circular-import repro and its fix.

  • import
  • modules

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

16 min read ยท 14 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

PythonThe cache, the spec, execute-once, reload and a circular import
PythonWrite your own finder and loader on sys.meta_path

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 121 of 139 in Python

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Python questions
Up nextHard

How does typing.Never enable exhaustive match checking?

Continue