Saturday, October 30, 2021

[SOLVED] Cython on Termux. ImportError: dynamic module does not define module export function (PyInit_libmc)

Issue

I'm trying to use python/cython on Android within Termux. I got python, cython pip-installed - with no 'obvious' problems. Now I have two simple test-snippets

A 'main' file mct.py

import os
hm=os.path.expanduser('~')
os.chdir(hm+'/projects/python1/v1')
from libmc import sq

print(sq(4))

and a stupidly trivial cython 'library'

cpdef double sq(double x):
  return x*x

I compiled the library the simple way (without the distutils fuzz), i.e. I do

cython -3 mct.pyx

clang -shared -pthread -fpic -fwrapv -fno-strict-aliasing -O2 -Wall -I /data/data/com.termux/files/usr/include/python3.7m/ -o libmc.so -lpython3 mc.c

With that I do get a libmc.so in my projects root. However then, when I try

python mct.py

I get

Traceback (most recent call last):
  File "mct.py", line 4, in <module>
    from libmc import sq
ImportError: dynamic module does not define module export function (PyInit_libmc)

Now I'm stuck. What am I missing here? Any suggestions to get this fixed?

Mark

PS.: Just to clarify, 'normal' Python seems works well for me on that Android-box.


Solution

The name of the pyx for needs to be libmc. Cython will generate an init function based on the filename which currently won't match what the .so file turns out to be called (when Python imports compiled modules it looks for an init function based on the .so filename).



Answered By - DavidW