{"record":{"id":"7b8bd9cb76a9bd07","repo":"PyO3/pyo3","slug":"called-append-to-inittab-but-a-python-interprete","errorCode":null,"errorMessage":"called `append_to_inittab` but a Python interpreter is already running.","messagePattern":"called `append_to_inittab` but a Python interpreter is already running\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/macros.rs","lineNumber":220,"sourceCode":"            wrapped_pymodule::_PYO3_DEF\n                .make_module(py)\n                .expect(\"failed to wrap pymodule\")\n        }\n    };\n}\n\n/// Add the module to the initialization table in order to make embedded Python code to use it.\n/// Module name is the argument.\n///\n/// Use it before [`Python::initialize`](crate::marker::Python::initialize) and\n/// leave feature `auto-initialize` off\n#[cfg(not(any(PyPy, GraalPy, all(Py_LIMITED_API, Py_GIL_DISABLED))))]\n#[macro_export]\nmacro_rules! append_to_inittab {\n    ($module:ident) => {\n        unsafe {\n            if $crate::ffi::Py_IsInitialized() != 0 {\n                ::core::panic!(\n                    \"called `append_to_inittab` but a Python interpreter is already running.\"\n                );\n            }\n            $crate::ffi::PyImport_AppendInittab(\n                $module::__PYO3_NAME.as_ptr(),\n                ::core::option::Option::Some($module::__pyo3_init),\n            );\n        }\n    };\n}\n","sourceCodeStart":202,"sourceCodeEnd":231,"githubUrl":"https://github.com/PyO3/pyo3/blob/ac9b6899d348be4d54614d060dea53a645a12e36/src/macros.rs#L202-L231","documentation":"append_to_inittab! must register a module before the interpreter starts, because PyImport_AppendInittab only works pre-initialization. PyO3 checks Py_IsInitialized() and panics if an interpreter is already running.","triggerScenarios":"Calling append_to_inittab!(my_module) after Python::with_embedded_python_interpreter / Py_Initialize has already been invoked in the process.","commonSituations":"Embedding apps that initialize Python in main and later try to register an extension module; registering modules lazily on first use after interpreter startup; re-registering modules on second run within the same process.","solutions":["Call append_to_inittab! before any interpreter initialization (before Py_Initialize / with_embedded_python_interpreter)","Register all modules in one place at program startup","If the interpreter must already be running, load the module dynamically (PyImport_AddModule + init function) instead of inittab","For embedded multi-phase setups, restructure so module registration happens in the init callback before startup"],"exampleFix":"// before\nPython::with_embedded_python_interpreter(|py| { ... });\nappend_to_inittab!(my_module); // panics\n// after\nappend_to_inittab!(my_module);\nPython::with_embedded_python_interpreter(|py| { ... });","handlingStrategy":"validation","validationCode":"if unsafe { pyo3::ffi::Py_IsInitialized() } == 0 {\n    append_to_inittab!(my_module); // safe\n} else {\n    // use dynamic module loading instead\n}","typeGuard":"fn inittab_registrable() -> bool { unsafe { pyo3::ffi::Py_IsInitialized() } == 0 }","tryCatchPattern":null,"preventionTips":["Always register inittab modules at program startup before Py_Initialize","Centralize interpreter bootstrap so module registration cannot run after init","For post-init needs, use PyImport module creation APIs instead of inittab"],"tags":["pyo3","embedding","inittab","initialization-order"],"backgroundTag":"module-registration-after-interpreter-init","analyzedSha":"ac9b6899d348be4d54614d060dea53a645a12e36","analyzedAt":"2026-09-05T09:20:35.319Z","contentChangedAt":"2026-09-05T09:20:35.319Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}