diff --git a/Lib/importlib/__init__.py b/Lib/importlib/__init__.py --- a/Lib/importlib/__init__.py +++ b/Lib/importlib/__init__.py @@ -46,7 +46,7 @@ finder.invalidate_caches() -def find_spec(name, path=None): +def find_spec(name, *, parent_path=None): """Return the spec for the specified module. First, sys.modules is checked to see if the module was already imported. If @@ -56,12 +56,23 @@ value of 'path' given to the finders. None is returned if no spec could be found. - Dotted names do not have their parent packages implicitly imported. You will - most likely need to explicitly import all parent packages in the proper - order for a submodule to get the correct spec. + If the named module is a submodule, its parent is first imported. + This may be avoided by passing in parent_path as a list or other + sequence. """ if name not in sys.modules: + if name.startswith('.'): + raise ImportError('relative module name {!r} not supported' + .format(name)) + path = parent_path + if path is None: + parent_name = name.rpartition('.')[0] + if parent_name: + parent = import_module(parent_name) + path = getattr(parent, '__path__', None) + if path is None: + return None return _bootstrap._find_spec(name, path) else: module = sys.modules[name]