Issue #14583: Fix importlib bug when a package's __init__.py would first import one of its modules then raise an error.
diff --git a/Python/import.c b/Python/import.c
index fd42a89..a8b1aa3 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -1633,19 +1633,20 @@
                 goto error_with_unlock;
             }
 
+            if (PyUnicode_GET_LENGTH(PyTuple_GET_ITEM(partition, 1)) == 0) {
+                /* No dot in module name, simple exit */
+                Py_DECREF(partition);
+                final_mod = mod;
+                Py_INCREF(mod);
+                goto exit_with_unlock;
+            }
+
             front = PyTuple_GET_ITEM(partition, 0);
             Py_INCREF(front);
             Py_DECREF(partition);
 
             if (level == 0) {
-                final_mod = PyDict_GetItem(interp->modules, front);
-                if (final_mod == NULL) {
-                    PyErr_Format(PyExc_KeyError,
-                                 "%R not in sys.modules as expected", front);
-                }
-                else {
-                    Py_INCREF(final_mod);
-                }
+                final_mod = PyObject_CallFunctionObjArgs(builtins_import, front, NULL);
                 Py_DECREF(front);
             }
             else {
@@ -1682,6 +1683,8 @@
                                                   fromlist, builtins_import,
                                                   NULL);
     }
+
+  exit_with_unlock:
   error_with_unlock:
 #ifdef WITH_THREAD
     if (_PyImport_ReleaseLock() < 0) {
diff --git a/Python/importlib.h b/Python/importlib.h
index 292308f..c88fc80 100644
--- a/Python/importlib.h
+++ b/Python/importlib.h
Binary files differ