diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 698ee23780ae10..42582fced27f22 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1149,6 +1149,13 @@ _PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb) } } + // gh-161863: Interpreter can be not fully initialized + // when calling this function. May be not safe to call the import. + PyInterpreterState *interp = _PyInterpreterState_GET(); + if (interp->imports.importlib == NULL) { + goto fallback; + } + // Try first with the stdlib traceback module PyObject *print_exception_fn = PyImport_ImportModuleAttrString( "traceback",