Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
python 3 import magic
# is there a better way to write all the code in magic_imports.py to re-surface a lot of deeply nested
# symbols at the top level?
# ======
# magic_imports.py
# this is just an example but there could be 20 different classes here
# the purpose is to import all the symbols so any file that uses them doesn't have to deal with the full path
from long.stupid.path import Glorp as _Glorp
from long.other.stupid.path import Blarg as _Blarg
# ...
Glorp = _Glorp
Blarg = _Blarg
# ...
# =======
# other.py
from magic_imports import *
Glorp()
@mpdehaan

This comment has been minimized.

Copy link
Owner Author

@mpdehaan mpdehaan commented Nov 11, 2018

this doesn't work...but is maybe close-ish

import importlib  
__all__ = []
def re_export(package, symbol):
    m1 = importlib.import_module(package)
    c1 = getattr(m1, symbol)
        __all__.append(c1)

re_export('opsmop.core.policy', 'Policy')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment