1

I am trying to run a Python script that is supposed to import some Cython code, and I get the following error:

ImportError: 'path to my .so file': undefined symbol: _Py_ZeroStruct

I have read that this could be due to a mismatch between Python versions. However, I have tried to compile with Python 2.7 and 3.8 (on Visual Studio code) and I always get the same error. I am using try to run this Ubuntu 20.04. I should also add that the code worked fine with older versions of Ubuntu, before I upgraded it a few days ago (I had Ubuntu 16 before).

Any idea how I can solve the problem?

1 Answers1

0

So the issue was probably that I was trying to import the .so file that was compiled with the old version of Ubuntu prior to upgrade (probably a beginner's mistake!).

I fixed it by re-compiling my .pyx file as suggested here. I initially got the error numpy/arrayobject.h: No such file or directory. I fixed this with include_dirs=[numpy.get_include()]. So in the end I compiled my .pyx with:

from distutils.core import setup
from Cython.Build import cythonize
import numpy

setup(
    ext_modules = cythonize("cython_all_fit.pyx"),
    include_dirs=[numpy.get_include()]
)

I run python setup.py build_ext --inplace, which generated my .so file. I was then able to import "cython_all_fit" in my main python code without errors.