ProgrammingBackend developer

Explain what happens when a module is imported in Python. How does Python search for and load modules? What common mistakes can occur with improper import organization?

Pass interviews with Hintsage AI assistant

Answer.

When a module is imported in Python, the interpreter searches for it in the directories listed in sys.path. It first looks among standard modules, then among .py, .pyc files, and directories containing a __init__.py file (packages).

  1. If the module has already been imported, the repeated import simply takes it from sys.modules.
  2. If the module is not found in the standard paths, a ModuleNotFoundError exception will be raised.
  3. During import, the module is compiled into bytecode (.pyc) and cached (if there are write permissions).

Example:

# mypkg/__init__.py (can be empty) # mypkg/mod.py # main.py import mypkg.mod
  • The file name, directory structure, and the presence/absence of __init__.py (mandatory for Python <3.3) all matter.

How it searches:

  • First, absolute import.
  • Next, searching in the sys.path directories (the current script's directory always comes first).
  • In packages, the context is important (from . import ..., absolute/relative import).

Trick question.

What happens if there is a file named random.py in the same directory as your script and you try to import the standard module random?

Answer:

Your local file random.py will be imported, not the standard library. A common cause of hard-to-detect bugs is naming collisions with library modules (shadowing). Care should be taken when naming files.

Examples of real errors due to misunderstandings of these nuances.


Story

In a large project, the module email.py accidentally overshadowed the standard email module from the library, and developers struggled for a long time to understand why the email parsing functions from third-party libraries were not working.


Story

In an ML project, the os.path function didn’t work: there was a file named os.py next to the main script that intercepted all calls to the standard module. They spent a month debugging until they found the naming conflict.


Story

In a microservices REST API, cyclical imports occurred when trying to make relative imports of models between several sub-packages. They resolved the issue only after refactoring the project structure and establishing a clear module loading order.