'Golang Embedding Python in Go
I've been reading the articles embedding-python-in-go and the original article packaging-python-code, but I have yet to succeed at running Python.h through cgo.
Can this be done on an m1 mac? if not, should I port everything into a Linux docker container?
what am I missing when setting up this type of project.
below is a simple example from article 1. This leads to the error
./main.go:22:4: could not determine kind of name for C.PyRun_SimpleString
1 package main
2 /*
3 #cgo pkg-config: python3
#include <Python/Python.h>
4 */
5 import "C"
6
7 import (
8 "unsafe"
9 )
10
11 func main() {
12
13 pycodeGo := `
14 import sys
15 for path in sys.path:
16 print(path)
17 `
18
19 defer C.Py_Finalize()
20 C.Py_Initialize()
21 pycodeC := C.CString(pycodeGo)
22 defer C.free(unsafe.Pointer(pycodeC))
23 C.PyRun_SimpleString(pycodeC)
24
25 }
removing lines 20-23 returns error
# command-line-arguments
Undefined symbols for architecture arm64:
"_Py_Finalize", referenced from:
__cgo_672c0788d500_Cfunc_Py_Finalize in _x002.o
(maybe you meant: __cgo_672c0788d500_Cfunc_Py_Finalize)
"_Py_Initialize", referenced from:
__cgo_672c0788d500_Cfunc_Py_Initialize in _x002.o
(maybe you meant: __cgo_672c0788d500_Cfunc_Py_Initialize)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [run] Error 2
kinda at a lost on the architecture error; m1 issue?
importing python.h on mac is #include <Python/Python.h> instead of #include <Python.h> importing Python.h on mac
software specs
$ python3 -V Python 3.9.10
$ go version go version go1.17.8 darwin/arm64
macOS verrsion 11.4, M1 2020.
Python.h is present under/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/Headers/Python.h
reference git repos ardanlabs/python-go go-python3 christian-korneck/python-go
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
