Index: Python/import.c =================================================================== --- Python/import.c (revision 78292) +++ Python/import.c (working copy) @@ -2130,7 +2130,14 @@ if (parent == NULL) return NULL; - head = load_next(parent, Py_None, &name, buf, &buflen); + /* If level is '-1', then we will allow relative or absolute + imports. Setting the alternative module to 'Py_None' causes + the module name lookup to be resolved using an absolute lookup if + a relative lookup in the parent module fails. Otherwise, we + only want absolute lookups to be peformed, thus we set the + alternative module equal to the parent module. */ + head = load_next(parent, level == -1 ? Py_None : parent, + &name, buf, &buflen); if (head == NULL) return NULL; Index: Lib/test/badrelimport.py =================================================================== --- Lib/test/badrelimport.py (revision 0) +++ Lib/test/badrelimport.py (revision 0) @@ -0,0 +1,2 @@ +# As seen in issue 7902 ... +from .os import walk Index: Lib/test/test_import.py =================================================================== --- Lib/test/test_import.py (revision 78292) +++ Lib/test/test_import.py (working copy) @@ -418,6 +418,15 @@ from . import relimport self.assertTrue(hasattr(relimport, "RelativeImport")) + def test_relimport_bad(self): + try: + from . import badrelimport + except ImportError, err: + self.assertEqual("No module named os", + err.args[0]) + else: + self.fail("bad relative import didn't raise an exception") + def test_issue3221(self): def check_absolute(): exec "from os import path" in ns