LykosAI/StabilityMatrix · error · InvalidOperationException

Venv already exists

Error message

Venv already exists

What it means

Setup() creates a uv-managed Python virtual environment at RootPath. By default it refuses to overwrite an existing venv: if PythonPath (e.g. <RootPath>/Scripts/python.exe) already exists and existsOk is false, it throws this InvalidOperationException before running uv. This guards against accidentally re-initializing or clobbering a working venv.

Solutions

  1. Pass existsOk: true to Setup() if re-creating over the existing venv is intended
  2. Delete the existing venv directory at RootPath before calling Setup()
  3. Check Exists() first and skip Setup() when the venv is already present and healthy

Example fix

// before
await venvRunner.Setup();
// after
if (!venvRunner.Exists())
{
    await venvRunner.Setup();
}
// or, to intentionally rebuild:
await venvRunner.Setup(existsOk: true);
Defensive patterns

Strategy: validation

Validate before calling

if (venvRunner.Exists() && !allowRecreate)
{
    return; // venv already set up
}

Try / catch

try
{
    await venvRunner.Setup();
}
catch (InvalidOperationException e) when (e.Message == "Venv already exists")
{
    logger.LogDebug("Venv already present, skipping setup");
}

Prevention

When it happens

Trigger: Calling Setup() (directly or via MigrateAsync) on a UvVenvRunner whose RootPath already contains a created venv, without passing existsOk: true.

Common situations: Re-running a package install/one-click installer after a previous successful setup; app migration logic hitting a venv that already exists from an earlier version; retrying setup after a partial failure that still left python.exe in place.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


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

Appendix: source

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

    /// <returns>True if the venv has a Scripts\python.exe file</returns>
    public bool Exists() => PythonPath.Exists;

    private FilePath UvExecutablePath =>
        new(GlobalConfig.LibraryDir, "Assets", "uv", Compat.IsWindows ? "uv.exe" : "uv");

    /// <summary>
    /// Creates a venv at the configured path.
    /// </summary>
    public async Task Setup(
        bool existsOk = false,
        Action<ProcessOutput>? onConsoleOutput = null,
        CancellationToken cancellationToken = default
    )
    {
        if (!existsOk && Exists())
        {
            throw new InvalidOperationException("Venv already exists");
        }

        // Create RootPath if it doesn't exist
        RootPath.Create();

        // Create venv (copy mode if windows)
        var args = new ProcessArgsBuilder(
            "venv",
            RootPath.ToString(),
            "--allow-existing",
            "--python",
            BaseInstall.PythonExePath
        );

        var venvProc = ProcessRunner.StartAnsiProcess(
            UvExecutablePath,
            args.ToProcessArgs(),
            WorkingDirectory?.FullPath,

View on GitHub (pinned to af93d6ef57)