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

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

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

问题

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

  1. python3 ./GanTTY/main.py gantt test

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

  1. {
  2. Name: "project",
  3. Usage: "add a new project with gantt chart",
  4. Action: func(c *cli.Context) error {
  5. cmd := exec.Command("./GanTTY/main.py", "gantt", "test")
  6. err:= cmd.Run()
  7. if err != nil {
  8. log.Fatal(err)
  9. }
  10. fmt.Println("opend project")
  11. return nil
  12. },
  13. },

并运行Go程序:

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

它给我返回以下错误:

  1. 2022/04/03 11:42:19 fork/exec ./GanTTY/main.py: exec format error
  2. 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

  1. 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

  1. {
  2. Name: "project",
  3. Usage: "add a new project with gantt chart",
  4. Action: func(c *cli.Context) error {
  5. cmd := exec.Command("./GanTTY/main.py", "gantt", "test")
  6. err:= cmd.Run()
  7. if err != nil {
  8. log.Fatal(err)
  9. }
  10. fmt.Println("opend project")
  11. return nil
  12. },
  13. },

and run the go program

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

it gives me this error

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

答案1

得分: 1

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

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

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

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

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

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

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

  1. cmd := exec.Command("python3","main.py", "gantt", "test")
  2. 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:

确定