{"record":{"id":"18ded2273873ae82","repo":"python/cpython","slug":"import-warnings-failed-traceback-n","errorCode":null,"errorMessage":"'import warnings' failed; traceback:\\n","messagePattern":"'import warnings' failed; traceback:\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Python/pylifecycle.c","lineNumber":1480,"sourceCode":"#endif\n\n    status = add_main_module(interp);\n    if (_PyStatus_EXCEPTION(status)) {\n        return status;\n    }\n\n    if (is_main_interp) {\n        /* Initialize warnings. */\n        PyObject *warnoptions;\n        if (PySys_GetOptionalAttrString(\"warnoptions\", &warnoptions) < 0) {\n            return _PyStatus_ERR(\"can't initialize warnings\");\n        }\n        if (warnoptions != NULL && PyList_Check(warnoptions) &&\n            PyList_Size(warnoptions) > 0)\n        {\n            PyObject *warnings_module = PyImport_ImportModule(\"warnings\");\n            if (warnings_module == NULL) {\n                fprintf(stderr, \"'import warnings' failed; traceback:\\n\");\n                _PyErr_Print(tstate);\n            }\n            Py_XDECREF(warnings_module);\n        }\n        Py_XDECREF(warnoptions);\n    }\n\n    if (config->site_import) {\n        status = init_import_site();\n        if (_PyStatus_EXCEPTION(status)) {\n            return status;\n        }\n    }\n\n    // Initialize lazy imports based on configuration. Do this after site\n    // module is imported to avoid circular imports during startup.\n    if (config->lazy_imports == 0) {\n        return _PyStatus_ERR(\"PyConfig.lazy_imports=0 is not supported\");","sourceCodeStart":1462,"sourceCodeEnd":1498,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Python/pylifecycle.c#L1462-L1498","documentation":"Printed during init_interp_main (Python/pylifecycle.c) when sys.warnoptions is non-empty and PyImport_ImportModule(\"warnings\") returns NULL. The interpreter proceeds anyway (the exception is printed with _PyErr_Print), but warning filters configured via -W / PYTHONWARNINGS are then inert because the warnings machinery never loaded.","triggerScenarios":"Passing -W option(s) or PYTHONWARNINGS while the 'warnings' module cannot be imported: broken stdlib path (PYTHONHOME/PYTHONPATH misconfiguration), truncated or corrupt installation, a warnings.py shadowed by a local file, or MemoryError during import.","commonSituations":"Virtualenvs/containers with a half-copied stdlib; PYTHONHOME pointing at the wrong prefix; a project file named warnings.py earlier on sys.path; embedded interpreters with an incomplete sys.path.","solutions":["Read the traceback printed right after the message — it names the real import failure","Verify PYTHONHOME/PYTHONPATH and the interpreter's prefix (`python -c 'import sys; print(sys.prefix)'`)","Remove or rename local files that shadow stdlib modules (warnings.py, os.py, etc.)"],"exampleFix":"# before\n$ ls\nwarnings.py\n$ python -W error app.py\n# after (rename the shadowing file)\n$ mv warnings.py my_warnings.py\n$ python -W error app.py","handlingStrategy":"validation","validationCode":"# smoke-test a healthy stdlib before enabling warning filters\npython -c 'import warnings, os, sys; print(sys.prefix)' && python -W error app.py","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never name local modules after stdlib modules (warnings.py, os.py, sys.py)","Validate PYTHONHOME/PYTHONPATH in deployment scripts; wrong prefixes break stdlib imports","Watch stderr at startup: the real ImportError traceback is printed immediately after this message"],"tags":["cpython","startup","import","warnings","stdlib"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}