Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class _NotFoundModuleError(Exception): pass
- _init_re = re.compile(r'^(.*)\{sep}__init__\.(\w+)$'.format(sep=os.sep))
- def import_path(path):
- def _import_module(name):
- try:
- return importlib.import_module(name)
- except ImportError as ex:
- if ex.args[0] == "No module named '{}'".format(name):
- raise _NotFoundModuleError from ex
- raise
- cwd = os.path.abspath(os.path.realpath(os.getcwd()))
- path = os.path.abspath(os.path.realpath(path))
- paths = []
- for p in sys.path:
- p = os.path.abspath(os.path.realpath(p))
- if path.startswith(p):
- paths.append(p)
- if not paths:
- raise ImportError('unable to find module with path {!r}: not in a sys.path'.format(path))
- paths.sort(key=lambda x: len(x), reverse=True)
- init_match = _init_re.match(path)
- if init_match:
- path = init_match.group(1)
- else:
- path = path.rpartition('.')[0]
- was_in_cwd = False
- for syspath in paths:
- if syspath == cwd:
- was_in_cwd = True
- continue
- # Check that syspath is a proper directory prefix,
- # i.e. tail starts with os.sep.
- #
- tail = path[len(syspath.rstrip(os.sep)):]
- if tail[0] != os.sep:
- tail = path
- modname = tail.strip(os.sep).replace(os.sep, '.')
- try:
- return _import_module(modname)
- except _NotFoundModuleError:
- pass
- if was_in_cwd:
- modname = path[len(cwd):].strip(os.sep).replace(os.sep, '.')
- try:
- return _import_module(modname)
- except _NotFoundModuleError:
- pass
- raise ImportError('unable to find module with path {!r}'.format(path))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement