有一个thinningpip包,目前只使用Python2编译。在
当我用sudo pip install thinning
安装它,然后尝试import thinning
时,我得到一个错误:
ImportError: /usr/lib/python3.5/site-packages/thinning.cpython-35m-x86_64-linux-gnu.so: undefined symbol: Py_InitModule3
我想这是因为Python3不再使用Py_InitModule3
。
以下是完整的c源文件:
我已经开始读Porting Extension Modules to Python 3,但我必须承认我几乎听不懂。在
我试图通过一些其他代码调整将Py_InitModule
更改为python3模拟PyModule_Create
,但没有成功。不幸的是,这个细化模块对我们的应用程序来说是一个很难依赖的模块。所以,我现在很困惑,没有时间和知识如何将这个模块移植到Python3。在
发生了什么变化:
注意:我无法真正深入了解
guo_hall_thinning
函数本身的功能。我所知道的是,它使用numpy C-API
的一小部分来获取和返回作为ndarray
的数据;我找不到任何关于它们的文档被修改,所以应该可以继续使用。在现在,确实改变了模块的初始化方式;通过这一点,我可以帮助您将其导入到Python3发行版中。我也在使用
3.5
来实现这一点,尽管我相信3.x
家族的老版本之间的差异不应该存在于或者是向后兼容的。在如您所述,一般信息在Porting to Python 3文档中提供,其中包含有关Module Initialization and state中初始化阶段的详细信息。新的变化在PEP 3121中进行了描述,这本书本身是一本不错但富有挑战性的读物。在
现在,它的要旨可以归纳为两点:
A)模块现在定义在一个专用的^{} 结构中:
这个新结构包含一些额外的成员,这些成员保存模块的名称和文档。为了简单起见,成员^{}, ^{}, ^{} and ^{} provide additional control during initialization/finalization but, we can opt to leave them as ^{}. 以及设置为
m_size
的模块m_size
是为了简单起见,设置这些值通常是为了支持多个解释器/多个初始化而完成的,应该更为棘手。在因此,简而言之,
^{pr2}$thinning
模块的新模块结构可以如下所示:aa第一期就到了!在
新的初始化函数,也就是说你需要给
initthinning
一个大的整容。新的模块初始化函数返回一个} 使用我们定义的结构并返回初始化的模块。现在更漂亮了,看起来像这样:
PyObject *
,现在命名为PyInit_<module_name>
。在里面(嘿,明白了吗?)new modules are created with ^{安装模块:
所有这些都是为了初始化模块。您可以下载模块(正如您所做的那样,我相信)找到
thinning_folder/src/c_thinning.c
文件,并将之前的所有内容替换为:包括以下内容:
然后,导航到包含
setup.py
的顶层目录并运行:像往常一样。可能会弹出一些编译警告,但可以安全地忽略这些警告。如果一切顺利,您将获得成功的安装,并且以下情况不会导致严重的seg fault:
它似乎在运行:):
我进一步编辑了
c_thinning.c
中的源代码,以打印出每次迭代中更改的元素的数量。它似乎在改变事情,但我不明白它使用的是什么基本标准,因为我还没有读过相应的论文。在简言之,
guo_hall_thinning(ndarr)
显然是在适当的位置进行了“细化”。这意味着在执行之后,作为参数提供的原始数组将被更改。所以,检查一下表格:总是} )。在
True
(Hint: check for equality between numpy arrays with ^{我做了一个测试,你可以直观地看到正在发生的变化,我相信这个测试也可以在你的机器上重现:
所以我猜它确实有用:-)。在
相关问题 更多 >
编程相关推荐