LykosAI/StabilityMatrix · error · FileNotFoundException

pyvenv.cfg not found

Error message

pyvenv.cfg not found

What it means

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.

Solutions

  1. Recreate the venv via Setup(existsOk: true) to regenerate pyvenv.cfg
  2. Restore or recreate pyvenv.cfg in RootPath with home/base-executable keys pointing at the embedded Python
  3. Check File.Exists(Path.Combine(rootPath, "pyvenv.cfg")) before calling Pip*/Run methods
  4. Delete the broken venv directory entirely and run Setup() fresh

Example fix

// before
await runner.PipInstall(new ProcessArgs("torch"));
// after
if (!File.Exists(Path.Combine(rootPath, "pyvenv.cfg")))
{
    await runner.Setup(existsOk: true); // regenerate pyvenv.cfg
}
await runner.PipInstall(new ProcessArgs("torch"));
Defensive patterns

Strategy: validation

Validate before calling

var cfgPath = Path.Combine(rootPath, "pyvenv.cfg");
if (!File.Exists(cfgPath))
{
    await venvRunner.Setup(existsOk: true); // regenerate venv metadata
}

Try / catch

try
{
    await venvRunner.PipInstall(args);
}
catch (FileNotFoundException e) when (e.FileName?.EndsWith("pyvenv.cfg") == true)
{
    logger.LogWarning("venv metadata missing, rebuilding");
    await venvRunner.Setup(existsOk: true);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12). Data as JSON: /api/errors/8e4e560a62893fb1. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Core/Python/UvVenvRunner.cs:205

    /// <summary>
    /// Set current python path to pyvenv.cfg
    /// This should be called before using the venv, in case user moves the venv directory.
    /// </summary>
    private void SetPyvenvCfg(string pythonDirectory, bool force = false)
    {
        // Skip if we are not created yet
        if (!Exists())
            return;

        // Skip if already set to same value
        if (lastSetPyvenvCfgPath == pythonDirectory && !force)
            return;

        // Path to pyvenv.cfg
        var cfgPath = Path.Combine(RootPath, "pyvenv.cfg");
        if (!File.Exists(cfgPath))
        {
            throw new FileNotFoundException("pyvenv.cfg not found", cfgPath);
        }

        Logger.Info("Updating pyvenv.cfg with embedded Python directory {PyDir}", pythonDirectory);

        var baseExecutable = Path.Combine(
            pythonDirectory,
            Compat.IsWindows ? "python.exe" : RelativePythonPath
        );

        var cfg = PyVenvCfg.Load(cfgPath);
        cfg["home"] = pythonDirectory;
        cfg["base-prefix"] = pythonDirectory;
        cfg["base-exec-prefix"] = pythonDirectory;
        cfg["base-executable"] = baseExecutable;
        cfg.Save(cfgPath);

        // Update last set path
        lastSetPyvenvCfgPath = pythonDirectory;

View on GitHub (pinned to af93d6ef57)