pulumi/pulumi · error

invalid Python version format: %q

Error message

invalid Python version format: %q

What it means

`ParsePythonVersion` extracts `major.minor[.patch][pre-release]` from strings like `Python 3.11.4` using a regex. If the trimmed string does not match at least `major.minor`, the version string is unparseable and this error is returned.

Source

Thrown at sdk/python/toolchain/toolchain.go:306

	cmd, err := commandFunc(ctx, "-c", "import sys; print(sys.executable)")
	if err != nil {
		return "", err
	}
	out, err := cmd.Output()
	if err != nil {
		return "", fmt.Errorf("failed to get python executable path: %w", err)
	}
	return strings.TrimSpace(string(out)), nil
}

func ParsePythonVersion(versionString string) (semver.Version, error) {
	versionString = strings.TrimSpace(versionString)
	versionString = strings.TrimPrefix(versionString, "Python ")

	re := regexp.MustCompile(`^(\d+)\.(\d+)(?:\.(\d+))?(?:(a|b|rc|dev)(\d+))?`)
	matches := re.FindStringSubmatch(versionString)
	if len(matches) < 3 {
		return semver.Version{}, fmt.Errorf("invalid Python version format: %q", versionString)
	}

	major, minor := matches[1], matches[2]
	patch := matches[3]
	if patch == "" {
		patch = "0"
	}

	normalizedVersion := fmt.Sprintf("%s.%s.%s", major, minor, patch)

	// Convert Python pre-release naming to semver format
	if matches[4] != "" {
		preReleaseType := matches[4]
		preReleaseNum := matches[5]
		switch preReleaseType {
		case "a":
			normalizedVersion += "-alpha." + preReleaseNum
		case "b":

View on GitHub (pinned to 793f7b2e16)

Solutions

  1. Run `python --version` yourself and confirm it prints a standard `Python X.Y.Z` line
  2. Remove PATH entries where wrappers/shims print extra output before the version
  3. Ensure the resolved interpreter is a real CPython (not the Windows Store alias) and reinstall if it emits nothing
  4. If a pre-release like `3.13.0a1` still fails, pin a stable release

Example fix

// before
Python 3.11.4 (main, Jun 20 2023) [clang ...] wrapped by shim printing banner
// after
$ python --version
Python 3.11.4
Defensive patterns

Strategy: validation

Validate before calling

out, _ := exec.Command("python", "--version").CombinedOutput()
ver := strings.TrimSpace(string(out))
matched, _ := regexp.MatchString(`^Python \d+\.\d+(\.\d+)?`, ver)
if !matched {
    return fmt.Errorf("interpreter reported unparseable version: %q", ver)
}

Prevention

When it happens

Trigger: The command that reports the Python version returned empty output, a locale/OS-specific message, an error banner, or a version with an unusual format (e.g. `3` alone, or `Python 3.11.4rc1 (main, ...) [...]` prefixed by compiler text) that the regex cannot match.

Common situations: Interpreter on PATH is a shim/wrapper script printing extra text; output captured includes a shell banner or deprecation notice; a minimal Python build reporting only `3.12`; empty output because the version command failed silently.

Related errors


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