pythoncythoncythonize

Am I doing cythonization right?


I am trying to get cython to work on a project.

What I do is the following:

  1. Create a file called make_cython.py

    import distutils.core
    import Cython.Build
    distutils.core.setup(ext_modules = Cython.Build.cythonize("main.py"))
    distutils.core.setup(ext_modules = Cython.Build.cythonize("helper.py"))
    
  2. Run python3 make_cython.py build_ext --inplace

  3. Run for both cythnoized files

    gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing       -Ipython/anaconda3/include/python3.9 -o main2.so main.c
    gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing       -Ipython/anaconda3/include/python3.9 -o helper2.so helper.c
    
  4. Change all import calls that call main.py and helper.py in my scripts to import main2 and import helper2.

Now, I have two questions:

  1. if helper.py and main.py themselves have import for other .py files, do I need to cythonize them too, and then change the imports, and then cythonize the new file, or is it all done recursively?

  2. I get the error when trying to run files that use main2.so (import main2) and helper2.so (import helper2):

    ImportError: helper2.so: undefined symbol: Py_EnterRecursiveCall
    

what did I miss in the compilation?


Solution

  • No.

    A few simple points:

    if helper.py and main.py themselves have import for other .py files, do I need to cythonize them too, and then change the imports, and then cythonize the new file, or is it all done recursively?

    It is not done recursively. Cython can import .py files happily so you don't need to Cythonize it all.

    I get the error when trying to run files [...]

    You'll probably find this goes away if you just use the files that setup.py generates. But it's because you didn't link libpython.