import inspect
import os
def find_object(path):
if path.count(":") != 1:
raise ValueError(f'python path {path!r} does not have the form "module:object"')
modulepath, objectpath = path.split(":")
obj = __import__(modulepath)
for a in modulepath.split(".")[1:]:
obj = getattr(obj, a)
for a in objectpath.split("."):
obj = getattr(obj, a)
return obj
def import_sibling_modules(exceptions=None):
frame = inspect.stack()[1]
mod = inspect.getmodule(frame[0])
name = os.path.basename(mod.__file__) excs = {"__init__.py", name}
if exceptions:
excs.update(exceptions)
modpath = mod.__name__ if not name.startswith("__init__.py"):
modpath = modpath.rsplit(".", 1)[0]
for f in os.listdir(os.path.dirname(mod.__file__)): if f.endswith(".py") and f not in excs:
__import__(modpath + "." + f[:-3])