Run python script in project folder

I am trying to run a python script in my Go project folder. However I have know idea how to run it.

What I have so far:

func runProgram(target string) {
	output, err := exec.Command(target).Output()
	if err != nil {
		panic(err.Error())
	}
	fmt.Println(string(output))
}

What I am executing the function with:

func auditTask() {
	runProgram("linuxScanner")
}

I keep getting the error:

panic: exec: “linuxScanner”: executable file not found in $PATH

I also need to be able to bundle the python script ideally in the same executable as the Go code

If the executable is not in PATH, you need to specify the path to the executable either absolute or relative (with leading ./ for the current working dir).

Exactly as Norbert says. You must have linuxScanner in one of the directories specified by the $PATH variable or use an relative path ./linuxScanner . is current directory or an absolute path like /home/xxx/my-folder/another-folder/linuxScanner.

If you want to put the script inside the go program. You could do it something like this:

package main

# Backticks are used for multiline raw strings
var pythonProgram = `
#!/usr/bin/python

a = "World"
print "Hello " + a
`

And then write the program to a temporary file with https://golang.org/pkg/io/ioutil/#TempFile and then execute that file.

He should even be able to write that to stdin of python to run it. That way he doesn’t need to create temporary files which might come with permission issues (but usually doesn’t)

How would I go about doing that?

This is from memory, also I’m not sure how python needs to be invoked to get its input from stdin, you might therefore need to tweak a bit:

package main

import (
	"bytes"
	"fmt"
	"os/exec"
	"strings"
)

func main() {
	cmd := new(exec.Cmd)
	r := strings.NewReader(`print("hello")`)
	var o bytes.Buffer

	cmd.Path = "python"
	cmd.Stdin = r
	cmd.Stdout = &o

	if err := cmd.Run(); err != nil {
		panic(err)
	}

	fmt.Println("%v", o.String())
}

Thanks! but unfortunately, I’m getting this error:

fork/exec python: no such file or directory

Sorry… I was able to take a look at my old project which did something similar and it seems as if I misremembered how to create the exec.Cmd struct.

Here is a small patch that repairs initialisation, as well as the fmt.Println call :wink:

--- x   2018-10-06 08:28:45.734796606 +0200
+++ main.go     2018-10-06 08:27:34.618183208 +0200
@@ -10,3 +10,3 @@
 func main() {
-       cmd := new(exec.Cmd)
+       cmd := exec.Command("python")
        r := strings.NewReader(`print("hello")`)
@@ -14,3 +14,2 @@

-       cmd.Path = "python"
        cmd.Stdin = r
@@ -22,3 +21,3 @@

-       fmt.Println("%v", o.String())
+       fmt.Println(o.String())
 }

Thank so much!

One more question, how would I go about running a whole python script, instead ‘print(“hello”)’?

You can put any string you like there.

Sorry, this might sound like a noob question, but how would go about putting a string that’s about 100 lines long

Like this:

const longString = `#!/usr/bin/env python

lorem
ipsum
dolor
est
imet
and
many
more
lines
with
actual
code
`

First, thank you for helping me!

I have one more problem (hopefully the last). I keep getting this error:

panic: exit status 1

then after the stacktrace, I get this too:

exit status 2

I assume you have just taken if err := cmd.Run(); err != nil { panic(err) } from my example as is?

So your python script is doing something like exit(1), this is understood as an errornous execution (everything unequal to 0 is) and therefore give you the useless error, that execution failed due to exit status x. Parsing this message is the only way to get the exitcode reliably.

The second exit status 2 is the exit status of your go program. go sometimes prints this information for some reason.

It does it then you use the go run command

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.