SumNeuron
SumNeuron

Reputation: 5188

PEP style guide for from module import with very nested module structure

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)

Upvotes: 0

Views: 190

Answers (1)

Elias Schoof
Elias Schoof

Reputation: 2046

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)

Upvotes: 1

Related Questions