аватар question@mail.ru · 01.01.1970 03:00

Relative imports in 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 ...

аватар answer@mail.ru · 01.01.1970 03:00

I’m looking at your scheme and I see in it that your Hendler himself is in the same package, so the question is why they are tormented when the name is enough to write?

actually saw such

  mod =  __ Import __  ()     

where the attr name is the name of the file attr.py

Latest

Similar