尝试在Golang中执行Python可执行文件时出现”exec format error”错误。

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

exec format error when trying to excute a python excutable in golang

问题

我正在尝试编写一个命令行界面(CLI),用于执行来自https://github.com/timeopochin/GanTTY的Python文件。在终端中执行以下命令:

python3 ./GanTTY/main.py gantt test

它将创建一个新的交互式甘特图。然而,当我在我的Go代码中这样做时,像这样:

{
  Name: "project",
  Usage: "add a new project with gantt chart",
  Action: func(c *cli.Context) error {
      cmd := exec.Command("./GanTTY/main.py", "gantt", "test")
      err:= cmd.Run()
      if err != nil {
          log.Fatal(err)
      }
      fmt.Println("opend project")
      return nil
  },
},

并运行Go程序:

go run program.go add project //"add"和"project"是命令和子命令

它给我返回以下错误:

2022/04/03 11:42:19 fork/exec ./GanTTY/main.py: exec format error
exit status 1
英文:

Im trying to write a cli that execute a python file from https://github.com/timeopochin/GanTTY. When excute in terminal using

python3 ./GanTTY/main.py gantt test

it will create a new interactive gantt chart. However when i do this in my go code, like this

{
  Name: "project",
  Usage: "add a new project with gantt chart",
  Action: func(c *cli.Context) error {
      cmd := exec.Command("./GanTTY/main.py", "gantt", "test")
      err:= cmd.Run()
      if err != nil {
          log.Fatal(err)
      }
      fmt.Println("opend project")
      return nil
  },
},

and run the go program

go run program.go add project //"add" and "project" are command and sub command

it gives me this error

2022/04/03 11:42:19 fork/exec ./GanTTY/main.py: exec format error
exit status 1

答案1

得分: 1

你需要在Windows中添加python3cmd.exe /c python3

cmd := exec.Command("python3", "GanTTY/main.py", "gantt", "test")

使用cmd.Dir来设置Python文件相对于当前工作目录的目录。

cmd := exec.Command("python3", "main.py", "gantt", "test")
cmd.Dir = "GanTTY"
英文:

You need to add python3 or cmd.exe /c python3 in windows.

cmd := exec.Command("python3","GanTTY/main.py", "gantt", "test")

use cmd.Dir to set directory of python file relative to current wd

cmd := exec.Command("python3","main.py", "gantt", "test")
cmd.Dir = "GanTTY"

huangapple
  • 本文由 发表于 2022年4月4日 02:45:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/71728912.html
匿名

发表评论

匿名网友

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

确定