0

I have a module mymodule with several submodules, let’s say

mymodule/
   __init__.py
   module1.py
   module2.py
   ...

For side-effects reasons, I need all those submodules to be run when mymodule is imported.

I tried adding the following to __init__.py

from . import *

But that didn’t work (the submodules were not imported when mymodule was)

I solved it by using pkgutil as described in the answers to this question

My question is the following :

Why doesn’t from . import * work as one would expect?

(when from . import module1 for examples works as expected)

tbrugere
  • 755
  • 7
  • 17

1 Answers1

0

See Python documentation on from module import *

the command from module import * doesn’t actually import all names in the module, instead it imports all names listed in the __all__ variable in the module.

And, citing the python doc

If __all__ is not defined, the statement from sound.effects import * does not import all submodules from the package sound.effects into the current namespace; it only ensures that the package sound.effects has been imported (possibly running any initialization code in __init__.py) and then imports whatever names are defined in the package. This includes any names defined (and submodules explicitly loaded) by __init__.py. It also includes any submodules of the package that were explicitly loaded by previous import statements.

tbrugere
  • 755
  • 7
  • 17