0

Suppose I need the function foo and that foo just so happens to be defined under library.lgmodule.medmodule.smmodule.nichemodule.utils.something.else

Is there a cleaner way to write:

from library.lgmodule.medmodule.smmodule.nichemodule.utils.something.else import foo

e.g. akin to the multi-line import:

from module.utiles import (foo, bar, baz, ban, ana,
    some, more, funcs, etc)
petezurich
  • 9,280
  • 9
  • 43
  • 57
SumNeuron
  • 4,850
  • 5
  • 39
  • 107
  • 1
    I think that this problem is telling you that you need a cleaner architecture for your modules. The nesting looks too deep for me. Answering only to what you asked, I think you have always to mention the path somewhere, even if you hide it behind an imported alias name. – progmatico Nov 30 '18 at 14:11
  • @progmatico could be, but alas I did not design the library. Anyway it isn't too big of a deal – SumNeuron Nov 30 '18 at 14:12

1 Answers1

1

You could use importlib.import_module and use some kind of string formatting. For example:

from importlib import import_module

path = '.'join[
    'library',
    'lgmodule',
    'medmodule',
    'smmodule',
    'nichemodule',
    'utils',
    'something',
    'else'
]
foo = import_module('{}.foo'.format(path)
Elias Strehle
  • 1,722
  • 1
  • 21
  • 34
  • 1
    This is an *alternative* way to import the module, but that does not look *cleaner* to me. – Delgan Nov 30 '18 at 15:38