# We make an alias for these modules which the metaflow_extensions author # wants to expose but that may not be loaded yet lazy_load_custom_modules = { "metaflow.datatools.%s" % k: "metaflow_extensions.datatools.%s" % k for k in addl_modules } for n, o in extension_module.__dict__.items(): if not n.startswith("__") and not isinstance(o, types.ModuleType): globals()[n] = o elif (isinstance(o, types.ModuleType) and o.__package__ and o.__package__.startswith("metaflow_extensions")): lazy_load_custom_modules["metaflow.datatools.%s" % n] = o if lazy_load_custom_modules: from metaflow import _LazyLoader sys.meta_path = [_LazyLoader(lazy_load_custom_modules)] + sys.meta_path finally: # Erase all temporary names to avoid leaking things for _n in [ "ver", "n", "o", "e", "lazy_load_custom_modules", "extension_module", "_LazyLoader", "addl_modules", ]: try: del globals()[_n] except KeyError:
if not (isinstance(e, ModuleNotFoundError) and e.path is None): print("Cannot load metaflow_custom exceptions -- " "if you want to ignore, uninstall metaflow_custom package") raise else: # We load into globals whatever we have in extension_module # We specifically exclude any modules that may be included (like sys, os, etc) # *except* for ones that are part of metaflow_custom (basically providing # an aliasing mechanism) lazy_load_custom_modules = {} for n, o in extension_module.__dict__.items(): if not n.startswith('__') and not isinstance(o, types.ModuleType): globals()[n] = o elif isinstance(o, types.ModuleType) and o.__package__ and \ o.__package__.startswith('metaflow_custom'): lazy_load_custom_modules['metaflow.%s' % n] = o if lazy_load_custom_modules: from metaflow import _LazyLoader sys.meta_path.append(_LazyLoader(lazy_load_custom_modules)) finally: # Erase all temporary names to avoid leaking things for _n in [ 'ver', 'n', 'o', 'e', 'lazy_load_custom_modules', 'extension_module', '_LazyLoader' ]: try: del globals()[_n] except KeyError: pass del globals()['_n']