Package Python program as so file and its calling method

References: https://blog.csdn.net/qq_16912257/article/details/80161112
     https://blog.csdn.net/linshenyuan1213/article/details/72677246
1. Write the setup.py file:
    the content of the setup.py file as follows:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize

extensions = [
    Extension("UIBox", ["UIBox.py"],
              include_dirs=["../include"],
              libraries=["../lib/cv2/cv2.cpython-36m-aarch64-linux-gnu.so",
                         "../lib/dlib-19.17.0-py3.6-linux-aarch64.egg/dlib.cpython-36m-aarch64-linux-gnu.so"],
              library_dirs=["../lib/cv2",
                            "../lib/dlib-19.17.0-py3.6-linux-aarch64.egg"]),

    Extension("UIShow", ["UIShow.py"],
              include_dirs=["../include"],
              libraries=["../lib/cv2/cv2.cpython-36m-aarch64-linux-gnu.so",
                         "../lib/dlib-19.17.0-py3.6-linux-aarch64.egg/dlib.cpython-36m-aarch64-linux-gnu.so"],
              library_dirs=["../lib/cv2",
                            "../lib/dlib-19.17.0-py3.6-linux-aarch64.egg"]),
    ……
]

setup(ext_modules=cythonize(extensions))

    Write as many Extensions as there are py files included in the entire project, and set other library paths that need to be called in include_dirs, libraries, library_dirs.
2. Compile the so file and execute the following command:

    python setup.py build_ext
3. Call the so file
    and add the following content to the called py file to
    import sys
    sys.path.append('../lib.linux-aarch64-3.6') #Load the so file (the input parameter is so file path)
    from libSupernode import libSupernode #import related classes in so file

Guess you like

Origin blog.csdn.net/ali1174/article/details/92677377