You need to enable JavaScript to run this app. Windows下Go的CGO调用Python函数的pkg-config配置问题
优惠活动
大模型
产品
解决方案
定价
更多

Windows下Go的CGO调用Python函数的pkg-config配置问题

问题:Windows下通过Go的CGO调用Python CSV读取函数的环境配置问题

需求:必须使用CGO库调用Python 3.10/11编写的CSV读取函数,无法用os/exec替代。

环境配置过程

Windows上无法通过choco安装pkg-config,因此通过安装MSYS2和gcc完成pkg-config的配置。

执行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

所用Go代码

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中的.pc文件内容

# 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

解决方法

通过将PYTHONHOME环境变量设置为C:\msys64\mingw64,成功解决了该问题。


内容的提问来源于stack exchange,提问作者Nudam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 23:25:14