LykosAI/StabilityMatrix · error · FileNotFoundException

pyvenv.cfg not found

Error message

pyvenv.cfg not found

What it means

SetPyvenvCfg() throws FileNotFoundException when RootPath/pyvenv.cfg does not exist. It needs this file to rewrite the base (embedded) Python directory pointers after the base install moved. Called by PipList, PipShow, PipIndex, Run, and RunDetached, so any pip/run operation on a venv missing this marker file fails.

Solutions

  1. Ensure Setup() completed successfully for this RootPath before calling pip/run methods.
  2. Verify RootPath points at the venv root (the folder containing pyvenv.cfg), not a subdirectory.
  3. Recreate the venv if pyvenv.cfg was deleted or the venv is corrupted.
  4. Check Exists()/cfgPath on disk before invoking the operation.

Example fix

// before
await venvRunner.PipInstall(args);
// after
if (!File.Exists(Path.Combine(venvRunner.RootPath, "pyvenv.cfg")))
{
    await venvRunner.Setup(null, null, ct);
}
await venvRunner.PipInstall(args);
Defensive patterns

Strategy: validation

Validate before calling

bool venvReady = File.Exists(Path.Combine(venvRunner.RootPath, "pyvenv.cfg"));
if (!venvReady) throw new InvalidOperationException("Venv not initialized");

Try / catch

catch (FileNotFoundException e) when (e.FileName?.Contains("pyvenv.cfg") == true)
{
    // re-run Setup or surface 'environment not provisioned' to user
}

Prevention

When it happens

Trigger: Calling PipList/PipShow/PipIndex/Run/RunDetached on a PyVenvRunner whose RootPath does not contain pyvenv.cfg — i.e. Setup() was never completed on this directory, or the cfg file was deleted, or RootPath was repointed at a non-venv directory.

Common situations: RootPath set to an empty or wrong directory; venv creation failed midway leaving no pyvenv.cfg; user manually deleted pyvenv.cfg or copied a venv without it; calling Run* on a runner that was never Set up.

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/23dcbf5438b525ad. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Core/Python/PyVenvRunner.cs:199

    /// <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)