LykosAI/StabilityMatrix · error · InvalidOperationException

Venv already exists

Error message

Venv already exists

What it means

Setup() throws this InvalidOperationException when a virtual environment already exists at RootPath and the existsOk parameter was not set to true. The library refuses to run `python -m venv` over an existing venv to avoid corrupting or partially re-creating one. Callers must pass existsOk: true or delete the venv first.

Solutions

  1. Delete the existing venv directory at RootPath before calling Setup, or call PyVenvRunner.DeleteAsync().
  2. Pass existsOk: true if re-using an existing venv is intentional.
  3. Check Exists() before Setup and skip creation when it returns true.

Example fix

// before
await venvRunner.Setup(null, onConsoleOutput, ct);
// after
if (venvRunner.Exists())
{
    await venvRunner.DeleteAsync(ct);
}
await venvRunner.Setup(null, onConsoleOutput, ct);
Defensive patterns

Strategy: validation

Validate before calling

if (venvRunner.Exists())
{
    // skip setup or delete first
    await venvRunner.DeleteAsync(ct);
}

Try / catch

catch (InvalidOperationException) when (venvRunner.Exists()) { /* venv already provisioned; continue */ }

Prevention

When it happens

Trigger: Calling PyVenvRunner.Setup() when Exists() is true (RootPath/pyvenv.cfg or the venv dir already present) without existsOk=true. Seen in practice via MigrateAsync re-running setup on an already-created venv.

Common situations: Re-running a package migration or install step after a previous successful venv creation; retrying setup after a partially failed run that still created the folder; pointing a new PyVenvRunner at a path that already holds a venv.

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/32e6e3b035bf1266. Report an issue: GitHub.

Appendix: source

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

    {
        EnvironmentVariables = env(EnvironmentVariables);
    }

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

    /// <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 string[] { "-m", "virtualenv", Compat.IsWindows ? "--always-copy" : "", RootPath };

        var venvProc = ProcessRunner.StartAnsiProcess(
            BaseInstall.PythonExePath,
            args,
            WorkingDirectory?.FullPath,
            onConsoleOutput
        );

        try
        {
            await venvProc.WaitForExitAsync(cancellationToken).ConfigureAwait(false);

View on GitHub (pinned to af93d6ef57)