Speedy
Speedy

Reputation: 120

Cython ImportError

Following file as helloworld.pyx:

print("Hello World")

Following file as setup.py:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [Extension("helloworld",["helloworld.pyx"]
setup(
    name = 'HW',
    cmdclass = {'build_ext': build_ext},
    ext_modules = ext_modules
)

After i use python setup.py build_ext --inplace i got my *.so file

So, i rename the *.so to hw.so, for getting a shorter import name.

But if i sart python and type in: import hw i got this error:

ImportError: dynamic module does not define init function (PyInit_hw)

I was doing the exact thing bout 3 hours ago and all was ok. But i tryed something out from this side: http://sourceforge.net/p/ubertooth/mailman/message/31699880/

I tryed the following:

cmake -DPYTHON_EXECUTABLE=$(which python2) \
-DPYTHON_INCLUDE_DIR=$(echo /usr/include/python2*) \
-DPYTHON_LIBRARY=$(echo /usr/lib/libpython2.*.so) \

because i wanted to fix something. I replaced all "2" with "3" cause i am working with python3.4

After i made this i always got the error above. Did i destroy any path? How can i undo it? Thank u for your help

Upvotes: 2

Views: 1918

Answers (1)

Jon Lund Steffensen
Jon Lund Steffensen

Reputation: 630

When looking at the Python 3 documentation on "Extending Python with C or C++" we see that

The initialization function must be named PyInit_name(), where name is the name of the module, and should be the only non-static item defined in the module file.

(https://docs.python.org/3/extending/extending.html#the-module-s-method-table-and-initialization-function)

This means that we cannot just change the file name of the module without changing the init function. We have to compile the module with the final name in the first place. Renaming the .so file after compiling it will not work.

Upvotes: 2

Related Questions