Go: 运行外部的 Python 脚本

huangapple go评论87阅读模式
英文:

Go: Run External Python script

问题

我尝试按照Go文档的指示调用一个Python脚本,该脚本只输出"Hello",但到目前为止都失败了。

exec.Command("script.py")

或者我还尝试调用一个简单地调用Python脚本的shell脚本,但也失败了:

exec.Command("job.sh")

有什么想法如何实现这个?

编辑

我按照评论中的建议解决了问题,并在exec.Command()中添加了完整路径。

英文:

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().

答案1

得分: 14

你尝试过像这样添加Run()Output()吗:

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

你可以在“如何在Golang中执行简单的Windows DOS命令?”中看到它的用法(适用于Windows,但对Unix也适用)

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

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

或者,使用Output(),就像在“在Go中执行shell命令”中所示:

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

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

fmt.Println(string(out))
英文:

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))

答案2

得分: 8

首先,不要忘记将你的Python脚本设置为可执行(设置权限并在开头添加#!/usr/local/bin/python)。

之后,你可以运行类似于以下的代码(注意它会报告错误和标准输出)。

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())
}
英文:

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())
}

答案3

得分: 1

以下是在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)
}

请注意,这是一段使用Go语言编写的代码,用于在Windows 10上运行Python脚本。它使用os.Getenv函数获取用户配置文件路径,并将其与Anaconda3和python.exe的路径组合起来。然后,它使用exec.Command函数创建一个命令,该命令在cmd中执行Python脚本。最后,它通过cmd.Output函数获取命令的输出,并将其打印出来。如果发生错误,它将使用log.Fatal函数记录错误信息。

英文:

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)
}

huangapple
  • 本文由 发表于 2014年11月20日 00:09:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/27021517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定