pulumi/pulumi · error

creating virtual environment at '%s': %w

Error message

creating virtual environment at '%s': %w

What it means

After running `python -m venv <venvDir>` via CombinedOutput, a non-zero exit or spawn failure is wrapped as 'creating virtual environment at ...'. The raw venv output (if any) is written to errorWriter first, so this error's %w is the process error and the real cause is usually in the printed venv output.

Source

Thrown at sdk/python/toolchain/pip.go:475

	}

	if venvDir != "" {
		printmsg("Creating virtual environment...")

		// Create the virtual environment by running `python -m venv <venvDir>`.
		if !filepath.IsAbs(venvDir) {
			return fmt.Errorf("virtual environment path must be absolute: %s", venvDir)
		}

		cmd, err := Command(ctx, "-m", "venv", venvDir)
		if err != nil {
			return err
		}
		if output, err := cmd.CombinedOutput(); err != nil {
			if len(output) > 0 {
				fmt.Fprintf(errorWriter, "%s\n", string(output))
			}
			return fmt.Errorf("creating virtual environment at '%s': %w", venvDir, err)
		}

		printmsg("Finished creating virtual environment")
	}

	runPipInstall := func(errorMsg string, arg ...string) error {
		args := append([]string{"-m", "pip", "install"}, arg...)

		// Retry up to 3 times to handle transient PyPI issues (e.g. CDN
		// returning unexpected Content-Type responses).
		const maxAttempts = 3
		var lastErr error
		for attempt := range maxAttempts {
			var pipCmd *exec.Cmd
			if venvDir == "" {
				var err error
				pipCmd, err = Command(ctx, args...)
				if err != nil {

View on GitHub (pinned to 793f7b2e16)

Solutions

  1. Read the venv output written above the error to identify the actual failure.
  2. On Debian/Ubuntu: `apt-get install python3-venv python3-pip` then retry.
  3. Ensure the parent directory of venvDir is writable and has free disk space.
  4. Delete a partially created venvDir and retry.
  5. Verify `python -m venv <dir>` works manually outside Pulumi; if not, fix the Python installation (or pick another interpreter via PULUMI_PYTHON_CMD).

Example fix

# before (Debian/Ubuntu)
$ python3 -m venv venv  # ensurepip missing
# after
$ sudo apt-get install python3-venv python3-pip
$ pulumi install
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-check: venv module present and target writable
if err := exec.Command(pythonBin, "-m", "venv", "--help").Run(); err != nil {
	return errors.New("venv module unavailable; install python3-venv")
}
if err := os.MkdirAll(filepath.Dir(venvDir), 0o755); err != nil {
	return fmt.Errorf("cannot write venv parent dir: %w", err)
}

Try / catch

if err := toolchain.InstallDependencies(ctx, cwd, venvDir, false, true, out, errW); err != nil {
	if strings.Contains(err.Error(), "creating virtual environment") {
		os.RemoveAll(venvDir) // clear partial venv, then retry once
		err = toolchain.InstallDependencies(ctx, cwd, venvDir, false, true, out, errW)
	}
}

Prevention

When it happens

Trigger: `python -m venv` exits non-zero: ensurepip missing, target directory unwritable or already exists with conflicting files, disk full, or the selected python is a broken/limited install (e.g. Debian without python3-venv).

Common situations: Ubuntu/Debian systems where the python.org/apt Python lacks the venv module (python3-venv package); read-only project directory; corporate proxy interfering with ensurepip; running on Python 3.11+ with EXTERNALLY-MANAGED interactions.

Related errors


AI-assisted analysis of pulumi/pulumi@793f7b2e16 (2026-08-31). Data as JSON: /api/errors/0e61202aa4a4630e. Report an issue: GitHub.