Avatar ·

Relative imports in Python

📁 python, вопрос, помощь

For some time (as relative imports canceled) I can’t normally solve the following question:

Suppose, the structure of the program

  App/ __init __. Py foo/ __init __. Py handler.py    

as from the module handler.py load something from the packet foo

The whole point is that the name of the package foo is unknown, unknown, it is unknown. I download it with Importlib .

while I found such a crutch: I cut the variable __ name __ , which in handler.py is equal 'foo.Handler' , i.e. So

  foo_name = __name __. rsplit ( '.' ,  1 ) [ 0 ]     

and then I load the module I need

  Needle_module Importlib.import_module ( ''. ' + & lt; the name of the module & gt;, foo_name)     

but, somehow, this is not koserly ...