1

Python imports are confusing, but I thought I finally understood them until I stumbled upon this behaviour (in 3.9.1). What is happening here?

Take this package structure:

countries/
├── __init__.py  # from . import greece
├── greece.py
└── spain.py

If I do import countries, the namespace dir(countries) only contains greece, as expected.

But if instead I start my session with:

from countries import spain
import countries

The namespace dir(countries) contains both greece and spain !

I know that __init__.py is run under the hood when I do the first import. What I don't understand is how python remembers to include both greece and spain in the countries namespace. Is it keeping the countries namespace saved somewhere under the hood after running from countries import spain, and then running import countries just adds it to the local namespace?

Jeffery
  • 629
  • 1
  • 7
  • 17

1 Answers1

3

You are correct. When importing a module python keeps it in a list of imported modules. When you from countries import spain and then import the root, python knows that spain is a submodule of the parent countries. so when you import countries it just adds the submodules that you don't have to the parent.

see here for the actual logic: https://github.com/python/cpython/blob/f32c7950e0077b6d9a8e217c2796fc582f18ca08/Lib/importlib/_bootstrap.py#L1007

testfile
  • 2,145
  • 1
  • 12
  • 31