0

This question is related to a couple of questions in SO like this one and this other one. Unfortunately the solution there is not working for me so far. I have a module.pxc file which I am compiling by a setup.py file, such as the following one:

# setup.py

module_extension = Extension(
name="iolif",
sources=["/home/maurizio/Ongoing.Projects/c_libraries/dcomplex_libc.c",
         "/home/maurizio/Ongoing.Projects/c_libraries/special_functions_libc.c",
         "/home/maurizio/Ongoing.Projects/c_libraries/models/freq_cv_libc.c",
         "module.pyx"],
libraries=['gsl', 'gslcblas', 'm'],
# library_dirs=["lib"],
include_dirs=["/home/maurizio/Ongoing.Projects/pycustommodules",
              "/home/maurizio/Ongoing.Projects/c_libraries",
              "/home/maurizio/Ongoing.Projects/c_libraries/models"]
)
setup(
    name="iolif",
    ext_modules=cythonize([module_extension])
)   

From command line, in the same directory of module.pxc, when writing python setup.py build_ext --inplace compilation works fine, and the iolif.so library is produced. The issue is that I can only import this library if I use Python2.7, whereas if I attempt to import it in Python3.x I get the known ImportError: dynamic module does not define module export function (PyInit_iolif).

Googling around, and as pointed out in the two questions linked above, it seems that this is due to the fact that cython is looking at Python2.7 rather than Python3.x (which is the one I work with instead). Accordingly, I attempted asking cythonize in my setup.py to use Python3.x by:

...
setup(
    name="iolif",
    ext_modules=cythonize([module_extension],
                           compiler_directives={'language_level': "3"})
)       

but it still does not work. The last compilation message indeed produces:

gcc -pthread -shared -Wl,-z,relro -Wl,--as-needed -Wl,-z,now - 
specs=/usr/lib/rpm/redhat/redhat-hardened-ld build/temp.linux-x86_64-2.7/pylif_io.o 
build/temp.linux-x86_64-2.7/home/maurizio/Ongoing.Projects/c_libraries/dcomplex_libc.o 
build/temp.linux-x86_64-2.7/home/maurizio/Ongoing.Projects/c_libraries/special_functions_libc.o build/temp.linux-x86_64-2.7/home/maurizio/Ongoing.Projects/c_libraries/models/freq_cv_libc.o -L/usr/lib64 
-lgsl -lgslcblas -lm -lpython2.7 -o /home/maurizio/Ongoing.Projects/DePitta.PNAS/Software/LIF.Analysis/iolif.so

where you can see that it is still linking with the -lpython2.7 library (whereas it should use for example -lpython3.7m). How do I solve it? What am I missing?

maurizio
  • 745
  • 1
  • 7
  • 25
  • 2
    You should call setup.py with Python3 and not Python2. That is just how distutils work. – ead Feb 04 '20 at 11:39

1 Answers1

1

Easy solution. My python command was still associated with python2.7 (I recently moved to Python3.x). Sorry about it. Hence:

python3 setup.py build_ext --inplace

will make the trick. Indeed compilation now reads:

gcc -pthread -shared -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -g build/temp.linux-x86_64-3.7/pylif_io.o build/temp.linux-x86_64-3.7/home/maurizio/Ongoing.Projects/c_libraries/dcomplex_libc.o build/temp.linux-x86_64-3.7/home/maurizio/Ongoing.Projects/c_libraries/special_functions_libc.o build/temp.linux-x86_64-3.7/home/maurizio/Ongoing.Projects/c_libraries/models/freq_cv_libc.o -L/usr/lib64 -lgsl -lgslcblas -lm -lpython3.7m -o /home/maurizio/Ongoing.Projects/DePitta.PNAS/Software/LIF.Analysis/iolif.cpython-37m-x86_64-linux-gnu.so

as required.

maurizio
  • 745
  • 1
  • 7
  • 25
  • @samcarter_is_at_topanswers.xyz Thanks. I thought so. But then it is not solving the issue. AR has not been provided for Linux since ages. And other presentation-feidnly options like `pdfpc` won't work under GNOME. Plus Fedora 30+ uses Wayland which makes things even more complicated... – maurizio Jun 07 '20 at 09:35