13

I have tried following the Go Docs in order to call a python script which just outputs "Hello" from GO, but have failed until now.

exec.Command("script.py")

or I've also tried calling a shell script which simply calls the python script, but also failed:

exec.Command("job.sh")

Any ideas how would I achieve this?

EDIT

I solved following the suggestion in the comments and adding the full path to exec.Command().

Claudiu S
  • 1,587
  • 6
  • 22
  • 40

3 Answers3

14

Did you try adding Run() or Output(), as in:

exec.Command("script.py").Run()
exec.Command("job.sh").Run()

You can see it used in "How to execute a simple Windows DOS command in Golang?" (for Windows, but the same idea applies for Unix)

c := exec.Command("job.sh")

if err := c.Run(); err != nil { 
    fmt.Println("Error: ", err)
}   

Or, with Output() as in "Exec a shell command in Go":

cmd := exec.Command("job.sh")
out, err := cmd.Output()

if err != nil {
    println(err.Error())
    return
}

fmt.Println(string(out))
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I followed your first suggestion and got exec: "job.sh": executable file not found in $PATH. Doing echo $PATH I get: /home/claudiu/cloud/goServer/. Changing the code to run: exec.Command("~/cloud/goServer/src/job.sh").Run() I got exec: "~/cloud/goServer/src/job.sh": stat ~/cloud/goServer/src/job.sh: no such file or directory – Claudiu S Nov 19 '14 at 18:02
  • 1
    @ClaudiuS could you replace `~` with the full path? (like I suppose `/home/claudiu/...`) – VonC Nov 19 '14 at 18:05
  • ah! getting exec: "/home/claudiu/cloud/goServer/src/job.sh": permission denied...so it's just a chmod problem now. Thanks! – Claudiu S Nov 19 '14 at 18:07
  • Actually, can you set chmod +x as an argument in exec.Command()? – Claudiu S Nov 19 '14 at 18:07
  • Can you also tell if the code flow in go file waits until the external python script ends or this is like a async process where go code just spawns the process and moves ahead without waiting for the script to end? If there are concurrent calls to that same go code, will it spawn new process with same script in every call? – ahirnish Aug 14 '19 at 13:35
  • @ahirnish In this instance, that would be a synchronous process, waiting for the command (here a python script) to terminate before proceeding. – VonC Aug 14 '19 at 13:37
8

First of all do not forget to make your python script executable (permissions and #!/usr/local/bin/python at the beginning).

After this you can just run something similar to this (notice that it will report you errors and standard output).

package main

import (
    "log"
    "os"
    "os/exec"
)

func main() {
    cmd := exec.Command("script.py")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    log.Println(cmd.Run())
}
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
1

Below worked for me on Windows 10

python := path.Clean(strings.Join([]string{os.Getenv("userprofile"), "Anaconda3", "python.exe"}, "/"))
script := "my_script.py"
cmd := exec.Command("cmd", python, script)
out, err := cmd.Output()
fmt.Println(string(out))
if err != nil {
    log.Fatal(err)
}
Jairo Lozano
  • 3,883
  • 3
  • 20
  • 29
Robio
  • 21
  • 1