Introduction:
One of the best methods of speeding up your python codes is turning it into a cython code. For turning a python script to cython, you need to save it with a pyx extension and then compile it using a setup file. For example,
say you have a file named helloworld.pyx. To compile this, your code inside setup file will look like:
from setuptools import setup from Cython.Build import cythonize setup( ext_modules = cythonize("helloworld.pyx") )
Now, after doing this, you have to run this setup file using the following command which creates a .c and a .so file.
$ python setup.py build_ext --inplace
The Problem:
So even after compiling, you may end up getting: importerror:... undefined symbol: _py_ZeroStruct when you try and import the compiled cython module.
Why does it happen? and the solution:
the source of this problem, according to the stackoverflow is the mismatch between the python version of the compiled cython code and the python file in which we import the module. It is necessary to mention the python version in the compilation command to get the correct version of cython module. So I changed the command line above to this:
$ python3 setup.py build_ext --inplace
and voila! the next time the correct compiled version gets called and we can use it without the hassles.
Comments
Post a Comment