动态模块未定义初始化函数(PyInit_fuzzy) [英] dynamic module does not define init function (PyInit_fuzzy)

查看:140
本文介绍了动态模块未定义初始化函数(PyInit_fuzzy)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python3.4,并且尝试安装模糊模块

  https://pypi.python。 org / pypi / Fuzzy。 

由于提到它仅适用于Python2,因此我尝试使用cython对其进行转换。这些是我遵循的步骤:


  1. cython Fuzzy.pyx

  2. gcc -g -02 -fpic python-config --cflags -c Fuzzy.c -o Fuzzy.o

  3. 与double_metaphone.c相同/ li>
  4. gcc -shared -o Fuzzy.so Fuzzy.o double_metaphone.o python-config --libs

当我尝试导入模糊时出现错误:

 动态模块未定义初始化函数(PyInit_fuzzy)

问题是什么?这是因为python2和python3冲突吗?如何解决这个问题?

解决方案

此问题已通过快速注释解决,但为了给出一些答案而发布了答案更多详细信息...



非常简短的答案是将 python-config 的所有实例替换为 python3-config python3.4-config



随后出现不必要的细节



OP试图在Python 3中使用Pyrex模块(这个问题尚不明确),因此对其进行了重建由于Cython最初基于Pyrex,因此在Cython中使用这种方法是明智的选择。被包含在内。 python-config 为系统上的默认Python版本生成相关的编译器/链接器选项,在编写本文时,该默认版本通常为Python 2(在我的系统上,它包括 -I / usr / include / python2.7 -I / usr / include / x86_64-linux-gnu / python2.7 )。因此,它为Python 2构建了模块。使用 python3.4-config 可确保包含正确的版本。



在从Python 2到Python 3的转换中,导入C模块时调用的函数从 init< modulename> 更改为 PyInit_< modulename>。 ,大概是为了帮助确保您只能导入针对正确版本构建的模块。因此,当使用Python 2构建模块时,它仅创建 initfuzzy ,因此在导入时找不到 PyInit_fuzzy


I am using Python3.4 and I am trying to install the module fuzzy

https://pypi.python.org/pypi/Fuzzy. 

Since it is mentioned it works only for Python2, I tried to convert it using cython. These are the steps that I followed:

  1. cython fuzzy.pyx
  2. gcc -g -02 -fpic python-config --cflags -c fuzzy.c -o fuzzy.o
  3. did the same for double_metaphone.c
  4. gcc -shared -o fuzzy.so fuzzy.o double_metaphone.o python-config --libs

When I tried to import fuzzy I got an error:

dynamic module does not define init function (PyInit_fuzzy)

What is the issue? Is this because of the python2 and python3 clash? How to resolve this?

解决方案

This was solved with a quick comment, but posted as an answer for the sake of giving a bit more detail...

The very short answer is to replace all instances of python-config for python3-config or python3.4-config.

Unnecessary detail follows

OP was trying to use a Pyrex module in Python 3 (this isn't especially clear from the question), and hence rebuilding it in Cython is a sensible approach to take, since Cython was originally based on Pyrex.

Cython generates code that should compile to work in Python 2 or 3, depending on which headers are included. python-config generates relevant compiler/linker options for the default version of Python on the system which at the time of writing is typically Python 2 (on my system it includes -I/usr/include/python2.7 -I/usr/include/x86_64-linux-gnu/python2.7). Therefore it builds the module for Python 2. Using the python3.4-config ensures that the right version is included.

In the changeover from Python 2 to Python 3 the function called when C modules are imported was changed from init<modulename> to PyInit_<modulename>, presumably to help ensure that you can only import modules built for the correct version. Therefore when the module is built with Python 2 it only creates initfuzzy, and therefore fails to find PyInit_fuzzy on import.

这篇关于动态模块未定义初始化函数(PyInit_fuzzy)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆