英文:
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中添加python3
或cmd.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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论