{"record":{"id":"d918fac37c19fa4b","repo":"python/cpython","slug":"pre-site-failed-n","errorCode":null,"errorMessage":"pre-site failed:\\n","messagePattern":"pre-site failed:\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Python/pylifecycle.c","lineNumber":1346,"sourceCode":"            goto error;\n        }\n\n        PyObject *res = PyObject_CallNoArgs(func);\n        Py_DECREF(func);\n        if (res == NULL) {\n            goto error;\n        }\n        Py_DECREF(res);\n    }\n\n    Py_DECREF(presite);\n    Py_DECREF(mod_name);\n    Py_XDECREF(func_name);\n    Py_DECREF(module);\n    return;\n\nerror:\n    fprintf(stderr, \"pre-site failed:\\n\");\n    _PyErr_Print(tstate);\n\n    Py_DECREF(presite);\n    Py_XDECREF(mod_name);\n    Py_XDECREF(func_name);\n    Py_XDECREF(module);\n}\n#endif\n\n\nstatic PyStatus\ninit_interp_main(PyThreadState *tstate)\n{\n    assert(!_PyErr_Occurred(tstate));\n\n    PyStatus status;\n    int is_main_interp = _Py_IsMainInterpreter(tstate->interp);\n    PyInterpreterState *interp = tstate->interp;","sourceCodeStart":1328,"sourceCodeEnd":1364,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Python/pylifecycle.c#L1328-L1364","documentation":"Printed at the error label of run_presite (Python/pylifecycle.c) after any exception while processing the pre-site hook: splitting 'mod:func', importing the module, getting the attribute, or calling the function. The message 'pre-site failed:' is followed by the full traceback via _PyErr_Print. It is a hard error path — the hook did not run, and cleanup of partially built objects (mod_name, func_name, module) follows.","triggerScenarios":"Setting -X presite=modname or modname:funcname where modname is not importable, the function name does not exist on the module, or the function itself raises. Any failure between PyUnicode_Substring and the PyObject_Call of the hook.","commonSituations":"Debug/free-threaded workflows where PYTHON_PRESITE points at a module not on sys.path at that early stage; typos in the entry-point spec; hook code assuming site.py already ran.","solutions":["Check the printed traceback: fix the ImportError/AttributeError it names","Ensure the presite module is importable pre-site (stdlib or early sys.path entry) — site-packages may not be available yet","Use the exact 'mod' or 'mod:func' syntax; a leading ':' (pos <= 0) or missing func will land here"],"exampleFix":"# before\nPYTHON_PRESITE=mymodule:main python app.py   # main() raises\n# after\nPYTHON_PRESITE=mymodule:safe_setup python app.py","handlingStrategy":"try-catch","validationCode":"# verify the hook resolves before relying on it (same import rules apply)\npython -c \"import importlib; m,f='myhook:setup'.split(':'); getattr(importlib.import_module(m), f)\"","typeGuard":null,"tryCatchPattern":"# inside the presite function itself — never let exceptions escape\ndef setup():\n    try:\n        ...early instrumentation...\n    except Exception:\n        import sys; sys.__stderr__.write('presite hook degraded\\n')","preventionTips":["Keep presite hooks importable without site-packages (stdlib or core path only)","Use exact 'mod' or 'mod:func' syntax; test the spec with a normal -c import first","Make hooks exception-proof; failures here happen before any user error handling exists"],"tags":["cpython","startup","presite","import","debugging"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}