簡體   English   中英

C Python模塊— ImportError:找不到符號:_Py_InitModule4_64

[英]C Python Module — ImportError: Symbol not found: _Py_InitModule4_64

我正在開始用C語言編寫Python 3模塊的過程。CI的代碼已經可以很好地編譯了(我在文章底部編譯了代碼)。 我編譯:

python3 setup.py build_ext --inplace

生成的.so文件放置在當前目錄中。 啟動python3之后,當我導入模塊時,出現此錯誤(用於截斷路徑的三點):

>>> import helloWorld
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(..., 2): Symbol not found: _Py_InitModule4_64
  Referenced from: .../helloWorld.cpython-36m-darwin.so
  Expected in: flat namespace
 in .../helloWorld.cpython-36m-darwin.so

如何獲得實現符號_Py_InitModule4_64的信息

我正在運行macOS High Sierra如果這意味着任何事情


helloWorld.cpython-36m-darwin.so運行nm顯示_Py_InitModule4_64未定義,那么這是否證明編譯過程中存在問題?

nm helloWorld.cpython-36m-darwin.so 
                 U _Py_BuildValue
                 U _Py_InitModule4_64
0000000000000eb0 t _helloWorld
0000000000001060 d _helloWorld_docs
0000000000001020 d _helloworld_funcs
0000000000000e80 T _inithelloWorld
                 U dyld_stub_binder

test.c:

#include <Python/Python.h>

static PyObject* helloWorld(PyObject* self) {
   return Py_BuildValue("s", "Hello, Python extensions!!");
}

static char helloWorld_docs[] =
   "helloWorld( ): Any message you want to put here!!\n";

static PyMethodDef helloworld_funcs[] = {
   {"helloWorld", (PyCFunction)helloWorld,
   METH_NOARGS, helloWorld_docs},
   {NULL}
};

void inithelloWorld(void) {
   Py_InitModule3("helloworld", helloworld_funcs, "Extension module example!");
}

setup.py:

from distutils.core import setup, Extension

setup(name = 'helloWorld', version = '1.0', \
    ext_modules = [Extension('helloWorld', ['test.c'])])

您是使用Python 2 C API編寫模塊的( 各種Py_InitModule函數僅適用於Python 2),但是您嘗試對其進行編譯並使用Python 3進行運行Py_InitModule的C層在Python 2和3之間發生了很大變化。 ,據我所知,還沒有用於C代碼的2to3工具。

您需要編寫與Python 3 API兼容的代碼才能在Python 3上工作。 最簡單(也是3.0-3.4支持的唯一方法)的轉換是單階段初始化 (使用PyModule_Create ),但是多階段初始化的行為更像是Python中定義的模塊(例如,可以完全卸載它們的方式不是單相模塊無法實現)。 入口點名稱的結構也發生了變化,從initMODULENAME更改為PyInit_MODULENAME ,因此您也需要對其進行更新。

我強烈建議閱讀Python 3擴展模塊教程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM