{"record":{"id":"659837b9a3220280","repo":"LykosAI/StabilityMatrix","slug":"python-linked-library-not-found","errorCode":null,"errorMessage":"Python linked library not found","messagePattern":"Python linked library not found","errorType":"exception","errorClass":"FileNotFoundException","httpStatus":null,"severity":"critical","filePath":"StabilityMatrix.Core/Python/PyRunner.cs","lineNumber":200,"sourceCode":"    /// Initialize Python runtime with a specific installation\n    /// </summary>\n    private async Task InitializeWithInstallation(PyInstallation installation)\n    {\n        if (PythonEngine.IsInitialized)\n            return;\n\n        Logger.Info(\"Setting PYTHONHOME={PythonDir}\", installation.InstallPath.ToRepr());\n\n        // Append Python path to PATH\n        var newEnvPath = Compat.GetEnvPathWithExtensions(installation.InstallPath);\n        Logger.Debug(\"Setting PATH={NewEnvPath}\", newEnvPath.ToRepr());\n        Environment.SetEnvironmentVariable(\"PATH\", newEnvPath, EnvironmentVariableTarget.Process);\n\n        Logger.Info(\"Initializing Python runtime with DLL: {DllPath}\", installation.PythonDllPath);\n        // Check PythonDLL exists\n        if (!File.Exists(installation.PythonDllPath))\n        {\n            throw new FileNotFoundException(\"Python linked library not found\", installation.PythonDllPath);\n        }\n\n        Runtime.PythonDLL = installation.PythonDllPath;\n        PythonEngine.PythonHome = installation.InstallPath;\n        PythonEngine.Initialize();\n        PythonEngine.BeginAllowThreads();\n\n        // Redirect stdout and stderr\n        StdOutStream = new PyIOStream();\n        StdErrStream = new PyIOStream();\n        await RunInThreadWithLock(() =>\n            {\n                var sys =\n                    Py.Import(\"sys\") as PyModule ?? throw new NullReferenceException(\"sys module not found\");\n                sys.Set(\"stdout\", StdOutStream);\n                sys.Set(\"stderr\", StdErrStream);\n            })\n            .ConfigureAwait(false);","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Core/Python/PyRunner.cs#L182-L218","documentation":"PyRunner.InitializeWithInstallation configures the embedded Python runtime (Python.NET) with the installation's DLL before calling PythonEngine.Initialize(). If the Python DLL (e.g. python310.dll) is missing at installation.PythonDllPath, a FileNotFoundException carrying the DLL path is thrown, because initializing without the DLL would crash or fail obscurely.","triggerScenarios":"Initializing the Python runtime when the installation's PythonDllPath file does not exist — incomplete/corrupted install, path misconfigured in the installation record, or the DLL was deleted/AV-quarantined.","commonSituations":"Interrupted Python runtime download, antivirus removing python dll, moving the app data folder without reconfiguring paths, broken installation metadata pointing at a stale path.","solutions":["Re-download/reinstall the shared Python runtime via the installation manager","Verify installation.PythonDllPath points to an existing file; fix the installation record if stale","Check antivirus quarantine for the python DLL and whitelist the folder","Recreate the environment or re-point the app's data directory to the correct install"],"exampleFix":"// before\nif (!File.Exists(installation.PythonDllPath)) throw ... // dll missing\n// after\nif (!File.Exists(installation.PythonDllPath))\n{\n    await installationManager.ReinstallSharedRuntimeAsync(installation.Version);\n}\nawait pyRunner.Initialize(installation);","handlingStrategy":"validation","validationCode":"if (!File.Exists(installation.PythonDllPath))\n    throw new InvalidOperationException($\"Python DLL missing: {installation.PythonDllPath}\");","typeGuard":null,"tryCatchPattern":"try\n{\n    await pyRunner.Initialize();\n}\ncatch (FileNotFoundException ex)\n{\n    logger.LogError(ex, \"Python DLL missing; reinstalling shared runtime\");\n    await installationManager.ReinstallSharedRuntimeAsync();\n    await pyRunner.Initialize();\n}","preventionTips":["Validate PythonDllPath exists after any runtime download or move","Exclude app-data from antivirus scans that can remove DLLs","Re-run environment setup after app updates that change install paths"],"tags":["python","pythonnet","file-not-found"],"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"}