( UPDATE 3 ) contains questions that I would like to receive answers to. UPDATE 2 refers to the fixes that I was trying to understand and fix this problem)
I am trying to get a Python extension module to wrap the C library (in this case, this is just an example described in the Python Cookbook (3rd edition.) The problem is that I ran into a classic error
ImportError: dynamic module does not define init function (initsample)
when i try to import a module.
The book itself has the following comment:
For all subsequent recipes, suppose the previous code is found in a file named sample.c, these definitions are found in a file called sample.h, and that it was com- compiled into the libsample library, which can be linked to other C code. Exact details compilation and binding vary from system to system, but this is not the main focus. It is assumed that if you are working with C code, you already understood this.
And I think I'm wrong. This is what I am doing: firstly, I have the following files:
➜ Sample ls
csample.pxd sample.c sample.h sample.pyx setup.py
The contents of the files correspond to this github repository containing the files described in the book. Assuming everything is copied correctly. Then i compilesample.c
➜ Sample gcc -c -fPIC -I/path/to/python2.7 sample.c
This creates sample.o, and I proceed to create a shared library:
➜ Sample gcc -shared sample.o -o libsample.so
It creates libsample.soand finally runs the file setup.py:
➜ Sample python setup.py build_ext --inplace
running build_ext
skipping 'sample.c' Cython extension (up-to-date)
building 'sample' extension
creating build
creating build/temp.linux-x86_64-2.7
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/path/to/include/python2.7 -c sample.c -o build/temp.linux-x86_64-2.7/sample.o
gcc -pthread -shared build/temp.linux-x86_64-2.7/sample.o -L. -L/path/to/lib -lsample -lpython2.7 -o /path/to/sample.so
. ?
setup.py ( , ):
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import sys
import os
import shutil
for root, dirs, files in os.walk(".", topdown=False):
for name in files:
if (name.startswith("sample") and not(name.endswith(".pyx") or name.endswith(".pxd") or name.endswith(".h"))):
os.remove(os.path.join(root, name))
for name in dirs:
if (name == "build"):
shutil.rmtree(name)
ext_modules = [
Extension('sample',
['sample.pyx'],
libraries=['sample'],
library_dirs=['.'])]
setup(
name = 'Sample extension module',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules
)
2
, ,
python setup.py build_ext --inplace
sample.c cythonized.
, setup.py cython sample.c, , cythonized sample.c ( libsample.so, sample.o, sample.so, ), sample.pyx :
>>> import sample
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: ./sample.so: undefined symbol: divide
sample.c , ( ) , , - sample.pyx (, .)
, ,
ImportError: dynamic module does not define init function (initsample)
. , :
➜ Sample gcc -c -fPIC -I/path/to/python2.7 sample.c
➜ Sample gcc -shared sample.o -o libsample.so
➜ Sample python setup.py build_ext
running build_ext
cythoning sample.pyx to sample.c
warning: sample.pyx:27:42: Use boundscheck(False) for faster access
building 'sample' extension
creating build
creating build/temp.linux-x86_64-2.7
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/path/to/include/python2.7 -c sample.c -o build/temp.linux-x86_64-2.7/sample.o
gcc -pthread -shared build/temp.linux-x86_64-2.7/sample.o -L. -L/path/to/lib -lsample -lpython2.7 -o /path/to/Sample/sample.so
➜ Sample python
>>> import sample
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: libsample.so: cannot open shared object file: No such file or directory
cythoning sample.pyx to sample.c . libsample.so Sample. ?
3:
, ,
ImportError: libsample.so: cannot open shared object file: No such file or directory
, LD_LIBRARY_PATH. :
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/Sample
? , - setup.py? , , , :
sample.c python setup.py build_ext --inplace ?sample.c , ?sample.pyx, ( skipping 'sample.c' Cython extension (up-to-date) )ImportError: libsample.so: cannot open shared object file: No such file or directory?
4. setup.py:
runtime_library_dirs=['./'],
, libsample.so , setup.py.