使用 Golang 中的 CGO 库调用用 Python 编写的函数的问题(通过在 Windows 上使用 pkg-config)

问题描述 投票:0回答:0

我正在尝试从 GoLang 调用一个用 Python 3.10/11 编写的简单函数来读取 CSV 文件。为此,我正在使用 CGO 库(不能改为 os/exec)。最大的问题是在 Windows 上安装 pkg-config - 我无法使用 choco 安装,所以我通过安装 MSYS2 和 gcc 来安装。但是当调用“go run main.go”时我得到这个错误:

Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Python path configuration:
  PYTHONHOME = (not set)
  PYTHONPATH = (not set)
  program name = 'python3'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = 'C:/Users/x/AppData/Local/Temp/go-build1370166474/b001/exe/main.exe'
  sys.base_prefix = 'D:/a/msys64/mingw64'
  sys.base_exec_prefix = 'D:/a/msys64/mingw64'
  sys.platlibdir = 'lib'
  sys.executable = 'C:/Users/x/AppData/Local/Temp/go-build1370166474/b001/exe/main.exe'
  sys.prefix = 'D:/a/msys64/mingw64'
  sys.exec_prefix = 'D:/a/msys64/mingw64'
  sys.path = [
    'D:/a/msys64/mingw64/lib/python310.zip',
    'D:/a/msys64/mingw64/lib/python3.10',
    'D:/a/msys64/mingw64/lib/lib-dynload',
    '',
  ]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
 
Current thread 0x000055ec (most recent call first):
  <no Python frame>
exit status 1

去代码:

package main  

// #cgo pkg-config: python-3.10
// #include <Python.h>
import "C"
import (
    "fmt"
    "unsafe"
)

func main(){

    // Initialize the Python interpreter
    C.Py_Initialize()

    // Import the module containting the function to be called
    str := C.CString("exec")
    module := C.PyImport_ImportModule(str)

    // Get a reference to the function
    str1 := C.CString("CSV_READER")
    function := C.PyObject_GetAttrString(module, str1)

    // Get arguments
    file := C.CString("file.csv")
    defer C.free(unsafe.Pointer(file))
    arg1 := C.PyUnicode_FromString(file)

    delimiter := C.CString(";")
    defer C.free(unsafe.Pointer(delimiter))
    arg2 := C.PyUnicode_FromString(delimiter) 

    args := C.PyTuple_New(2)
    C.PyTuple_SetItem(args, 0, arg1)
    C.PyTuple_SetItem(args, 1, arg2)

    // Call a function
    result := C.PyObject_CallObject(function, args)

    cstr := C.PyUnicode_AsUTF8(result)
    gostr := C.GoString(cstr)
    fmt.Println(gostr)

    C.Py_DecRef(arg1)
    C.Py_DecRef(arg2)

    C.Py_DecRef(args)
    C.Py_DecRef(result)

    C.Py_DecRef(function)
    C.Py_DecRef(module)

    C.Py_Finalize()

}

来自 mingw 的 PGK 代码:

# See: man pkg-config
prefix=/mingw64
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include 

Name: Python
Description: Build a C extension for Python
Requires:
Version: 3.10
Libs.private:  -lm -lversion -lshlwapi
Libs: -L${libdir} -lpython3.10
Cflags: -I${includedir}/python3.10

我也试图将我自己的目录放在 .pc 中,它指向已安装的 python 但后来我得到了这个:

C:\Program Files\Go\pkg\tool\windows_amd64\link.exe: running gcc failed: exit status 1
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lpython311: No such file or directory    
collect2.exe: error: ld returned 1 exit status
python c go cpython cgo
© www.soinside.com 2019 - 2024. All rights reserved.