dynamic module does not define init function (PyIn

2019-02-06 18:01发布

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?

标签: python cython
1条回答
爷、活的狠高调
2楼-- · 2019-02-06 18:42

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.

查看更多
登录 后发表回答