diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -922,7 +922,7 @@ raise TypeError('module name must be str, not {}'.format(type(name))) if level < 0: raise ValueError('level must be >= 0') - if package: + if level > 0: if not isinstance(package, str): raise TypeError('__package__ not set to a string') elif package not in sys.modules: diff --git a/Lib/test/test_importlib/import_/test_relative_imports.py b/Lib/test/test_importlib/import_/test_relative_imports.py --- a/Lib/test/test_importlib/import_/test_relative_imports.py +++ b/Lib/test/test_importlib/import_/test_relative_imports.py @@ -207,6 +207,11 @@ with self.assertRaises(KeyError): self.__import__('sys', level=1) + def test_relative_import_no_package_exists_absolute(self): + with self.assertRaises(SystemError): + self.__import__('sys', {'__package__': '', '__spec__': None}, + level=1) + (Frozen_RelativeImports, Source_RelativeImports