0

i'm developing a Go application, which should be able to run python scripts. the scripts are located inside scripts/python folder, and i include them in the go binary using //go:embed annotation, and then run them using exec.Command, the following way:

package main

import (
    "embed"
    _ "embed"
    "fmt"
    "os"
    "os/exec"
    "strings"
)    
//go:embed scripts/python/argsTest.py
var argsTestScript string
    
func main() {
   args:= []string{}
   runCommand(args, argsTestScript)
}
func runCommand(args []string, scriptFile string)  {
    c := exec.Command("python", args...)
    c.Stdin = strings.NewReader(scriptFile)
    output, err := c.Output()
    if err != nil {
        fmt.Println("error = ", err)
        return
    }
    fmt.Println("output = ", string(output))
}

when argTest.py simply prints the arguments -

import sys

print('Number of arguments:', len(sys.argv), 'arguments.')
print('Argument List:', str(sys.argv))

if i don't send any arguments it works fine, however when i do send any argument i get exit status 2, and inside stderr i get python: can't open file 'x': [Errno 2] No such file or directory (if i send a single argument x). i understand that this happens because when there are arguments, the exec.Command behaves as if the first one is the python script that should be executed.

one way of overcoming this should be extracting the script file's path (and not its content, which is what argsTestScript var holds). i tried doing so by setting this var to be of type embed.FS instead of string, but i could only find a way to extract the file name, not its full path.

is there any way to achieve this? either trick the exec.Command to receive the arguments after the script file, or extract the script file path? or any other option?

cheers, eRez

5
  • exit status 2 is the exit status, which is usually accompanied by some error output. Look at the output for clues as to why the command failed.
    – JimB
    Apr 13, 2022 at 15:31
  • @JimB when i send single argument x the error output is python: can't open file 'x': [Errno 2] No such file or directory, which - as i wrote yesterday - indicates that exec.Command expects there a script file and not an argument
    – eRez
    Apr 14, 2022 at 6:00
  • You're passing your args as the first argument to the python interpreter, but the actual program is going to come from stdin. There's probably a way to explicitly tell python that the program will be via stdin, and to not look for a file to execute.
    – JimB
    Apr 14, 2022 at 13:36
  • i've found a workaround - to make a temp copy of the script file, extract its path, and send it as the second argument to the python interpreter, followed by the actual agrs
    – eRez
    Apr 15, 2022 at 8:52
  • 1
    Looking at the python -h output, it says to use - as the argument to indicate stdin.
    – JimB
    Apr 15, 2022 at 10:17

1 Answer 1

0

as @JimB mentioned above - when using stdin and sending arguments to the python method - the first argument should simply be -

args:= []string{"-", "x", "y", "z"}

however make sure your python method ignores this hyphen, as the output of my go script will be -

output =  Number of arguments: 4 arguments.
Argument List: ['-', 'x', 'y', 'z']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.