diff --git a/pkg/functions/runner.go b/pkg/functions/runner.go index f0dedf2bdb..aa51cdb384 100644 --- a/pkg/functions/runner.go +++ b/pkg/functions/runner.go @@ -195,7 +195,7 @@ func runPython(ctx context.Context, job *Job) (err error) { if job.verbose { fmt.Printf("python -m venv .venv\n") } - cmd := exec.CommandContext(ctx, "python", "-m", "venv", ".venv") + cmd := exec.CommandContext(ctx, pythonCmd(), "-m", "venv", ".venv") cmd.Dir = job.Dir() cmd.Stderr = os.Stderr cmd.Stdout = os.Stdout @@ -360,3 +360,11 @@ func choosePort(iface, preferredPort string) (string, error) { } return port, nil } + +func pythonCmd() string { + _, err := exec.LookPath("python") + if err != nil { + return "python3" + } + return "python" +} diff --git a/pkg/oci/python_builder.go b/pkg/oci/python_builder.go index 8e75986b0a..4272590928 100644 --- a/pkg/oci/python_builder.go +++ b/pkg/oci/python_builder.go @@ -24,7 +24,7 @@ func (b pythonBuilder) Base(customBase string) string { if customBase != "" { return customBase } - cmd := exec.Command("python", "-V") + cmd := exec.Command(pythonCmd(), "-V") out, err := cmd.CombinedOutput() if err != nil { return defaultPythonBase @@ -61,7 +61,7 @@ func (b pythonBuilder) WriteShared(job buildJob) (layers []imageLayer, err error if job.verbose { fmt.Printf("python -m venv .venv\n") } - cmd := exec.CommandContext(job.ctx, "python", "-m", "venv", ".venv") + cmd := exec.CommandContext(job.ctx, pythonCmd(), "-m", "venv", ".venv") cmd.Dir = job.buildDir() cmd.Stderr = os.Stderr cmd.Stdout = os.Stdout @@ -205,3 +205,11 @@ func newPythonLibTarball(job buildJob, root, target string) error { func (b pythonBuilder) WritePlatform(ctx buildJob, p v1.Platform) (layers []imageLayer, err error) { return []imageLayer{}, nil } + +func pythonCmd() string { + _, err := exec.LookPath("python") + if err != nil { + return "python3" + } + return "python" +}