本文介绍了转到:运行外部Python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试按照Go Docs的要求调用一个Python脚本,该脚本仅从GO输出"Hello",但直到现在都失败了.

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

或者我也尝试调用仅调用python脚本的shell脚本,但也失败了:

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?

编辑

我按照注释中的建议解决了,并将完整路径添加到exec.Command().

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

推荐答案

您尝试添加 Run() Output() ,在:

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

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

您可以在"如何在Golang中执行简单的Windows DOS命令?中看到它(对于Windows,但同样的想法也适用于Unix)

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

或者,使用Output()如"在Go中执行shell命令":

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

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

fmt.Println(string(out))

这篇关于转到:运行外部Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:10