3

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?

STerliakov
  • 4,983
  • 3
  • 15
  • 37
kloop
  • 4,537
  • 13
  • 42
  • 66

1 Answers1

1

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.

wim
  • 338,267
  • 99
  • 616
  • 750
DavidW
  • 29,336
  • 6
  • 55
  • 86
  • @wim, thanks, you say "It is not done recursively. Cython can import .py files happily so you don't need to Cythonize it all.", but I do want to have "all" files cythonized. My question is whether I need to cythonize them in the order of the dependency tree (from bottom to top), or whether it doesn't matter. I assume becasuse it is .so, the .so itself would look for other .so in runtime. – kloop Oct 10 '22 at 21:21
  • It doesn't matter what order you do them in. – DavidW Oct 10 '22 at 21:31
  • thanks. so if the build .py creates main.cpython-39-x86_64-linux-gnu.so, I need to do import main or import main.cpython-39-x86_64-linux-gnu? It sounds a bit strange to do the first, because then there is no way to revert back to .py without deleting the .so (?), but then the second is also counter-intuitive. – kloop Oct 10 '22 at 21:45
  • `import main`. The `cpython-39-x86_64-linux-gnu` is an internal tag that means you could have a .so files for different versions of Cython in the same directory. You can't easily revert back to the .py without deleting the .so. – DavidW Oct 11 '22 at 05:35
  • Comment above should read "different versions of *Python* in the same directory" – DavidW Oct 11 '22 at 17:51