{"record":{"id":"8e4e560a62893fb1","repo":"LykosAI/StabilityMatrix","slug":"pyvenv-cfg-not-found-uvvenvrunner","errorCode":null,"errorMessage":"pyvenv.cfg not found","messagePattern":"pyvenv\\.cfg not found","errorType":"exception","errorClass":"FileNotFoundException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Core/Python/UvVenvRunner.cs","lineNumber":205,"sourceCode":"    /// <summary>\n    /// Set current python path to pyvenv.cfg\n    /// This should be called before using the venv, in case user moves the venv directory.\n    /// </summary>\n    private void SetPyvenvCfg(string pythonDirectory, bool force = false)\n    {\n        // Skip if we are not created yet\n        if (!Exists())\n            return;\n\n        // Skip if already set to same value\n        if (lastSetPyvenvCfgPath == pythonDirectory && !force)\n            return;\n\n        // Path to pyvenv.cfg\n        var cfgPath = Path.Combine(RootPath, \"pyvenv.cfg\");\n        if (!File.Exists(cfgPath))\n        {\n            throw new FileNotFoundException(\"pyvenv.cfg not found\", cfgPath);\n        }\n\n        Logger.Info(\"Updating pyvenv.cfg with embedded Python directory {PyDir}\", pythonDirectory);\n\n        var baseExecutable = Path.Combine(\n            pythonDirectory,\n            Compat.IsWindows ? \"python.exe\" : RelativePythonPath\n        );\n\n        var cfg = PyVenvCfg.Load(cfgPath);\n        cfg[\"home\"] = pythonDirectory;\n        cfg[\"base-prefix\"] = pythonDirectory;\n        cfg[\"base-exec-prefix\"] = pythonDirectory;\n        cfg[\"base-executable\"] = baseExecutable;\n        cfg.Save(cfgPath);\n\n        // Update last set path\n        lastSetPyvenvCfgPath = pythonDirectory;","sourceCodeStart":187,"sourceCodeEnd":223,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Core/Python/UvVenvRunner.cs#L187-L223","documentation":"SetPyvenvCfg() rewrites pyvenv.cfg inside the venv to point back at the embedded Python install (in case the venv directory was moved). If the venv root exists (python.exe present) but pyvenv.cfg is missing at <RootPath>/pyvenv.cfg, it throws FileNotFoundException. Called by PipInstall, PipUninstall, PipList, PipShow, PipIndex, and Run.","triggerScenarios":"Calling any Pip* or Run method on a venv whose directory contains a python interpreter but lacks pyvenv.cfg — e.g. the cfg file was deleted, a partially failed venv creation, or a hand-crafted/malformed venv directory at RootPath.","commonSituations":"User or cleanup tool deleted pyvenv.cfg; venv creation was interrupted partway; restoring the directory from an incomplete backup; pointing UvVenvRunner at a folder that has python.exe but is not a real venv.","solutions":["Recreate the venv via Setup(existsOk: true) to regenerate pyvenv.cfg","Restore or recreate pyvenv.cfg in RootPath with home/base-executable keys pointing at the embedded Python","Check File.Exists(Path.Combine(rootPath, \"pyvenv.cfg\")) before calling Pip*/Run methods","Delete the broken venv directory entirely and run Setup() fresh"],"exampleFix":"// before\nawait runner.PipInstall(new ProcessArgs(\"torch\"));\n// after\nif (!File.Exists(Path.Combine(rootPath, \"pyvenv.cfg\")))\n{\n    await runner.Setup(existsOk: true); // regenerate pyvenv.cfg\n}\nawait runner.PipInstall(new ProcessArgs(\"torch\"));","handlingStrategy":"validation","validationCode":"var cfgPath = Path.Combine(rootPath, \"pyvenv.cfg\");\nif (!File.Exists(cfgPath))\n{\n    await venvRunner.Setup(existsOk: true); // regenerate venv metadata\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    await venvRunner.PipInstall(args);\n}\ncatch (FileNotFoundException e) when (e.FileName?.EndsWith(\"pyvenv.cfg\") == true)\n{\n    logger.LogWarning(\"venv metadata missing, rebuilding\");\n    await venvRunner.Setup(existsOk: true);\n}","preventionTips":["Treat pyvenv.cfg as part of the venv's required files; never delete it","Validate both python.exe and pyvenv.cfg exist before using Pip*/Run","Rebuild the venv after any manual directory move/copy","Avoid restoring venv directories from partial backups"],"tags":["python","venv","file-not-found","corrupt-env"],"backgroundTag":"file-not-found","analyzedSha":"af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002","analyzedAt":"2026-09-12T19:02:43.389Z","contentChangedAt":"2026-09-12T19:02:43.389Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}