How to distribute a .so file generated by cython
I was able to create a .so file on linux using cython in my .pyx script. I can also import my python interpreter successfully.
My question is, how can I get this .so installed on the end user's machine without using cython? I don't want to distribute .pyx or .py or .c files to the end user. And also I don't want the end user to install cython or anything to use my .so file
source to share
You can use ldd
to check static dependencies of your .so. Here are the .so dependencies I built with cython on my Ubuntu desktop:
$ ldd iksolver.so
linux-vdso.so.1 => (0x00007fffc07fe000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fdcabc08000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fdcab903000)
libpython2.7.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0 (0x00007fdcab3d3000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fdcab00b000)
/lib64/ld-linux-x86-64.so.2 (0x00007fdcac068000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fdcaadf4000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fdcaabef000)
libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007fdcaa9ec000)
$
You can see that it depends on many other installed libraries and requires python2.7 to be installed, but everything here is set in the default Ubuntu deployment. Note that nothing is required here for Cython to be installed.
You will be able to copy the .so to /usr/local/lib/python2.7/dist-packages
the destination computer and you will be able to import it as long as you are using the same processor architecture and operating system version.
Of course, if you have any statements import
in your Cython module, you will need to make sure those modules are also present on the target system.
source to share
Easier and better to distribute the python wheel. You will need pip3 on your target machine, but not Cython.
By construction machine:
Build your wheel with this type of setup.py:
from Cython.Build import cythonize
from setuptools import setup, Extension
setup( name='myPackage',
version='1.0',
py_modules=['myPackage'],
ext_modules =
cythonize(Extension("myPackageExtension",
# the extension name
sources=["myPackageExtension.pyx", "Tool1.cpp", "Tool2.cpp"],
# the Cython source and additional C++ source files
language="c++", # generate and compile C++ code
)
)
)
And the next line of commands:
python3 setup.py bdist_wheel
You will get the file .whl
in a folder dist
.
On the target machine:
Copy the wheel file to the target machine then install it using the following command line:
pip3 install myPackage-*-cp34-cp34m-linux_x86_64.whl
(note that your package is built for python version (cp34) and dedicated platform (linux_x86_64), so you need to have the same as building machine (or you can use manylinux to solve this)).
source to share