Best way to package a Python library that includes a C shared library?

后端 未结 3 1497
名媛妹妹
名媛妹妹 2021-02-04 05:00

I have written a library whose main functionality is implemented in C (speed is critical), with a thin Python layer around it to deal with the ctypes nastiness.

相关标签:
3条回答
  • 2021-02-04 05:44

    I'd consider building the python module as a subproject of a normal shared library build. So, use automake, autoconf or something like that to build the shared library, have a python_bindings directory with a setup.py and your python module.

    0 讨论(0)
  • 2021-02-04 05:50

    I had a similar need and found this answer helpful: Python setup.py call makefile don't include binaries.

    I have an ANSI C library in the src directory of my distribution. In the src directory is a Makefile that builds a file called liblsd.so in my package directory (lsd). I call this in setup.py and then tell setup to include the library file using the package_data argument.

    import os.path
    import subprocess
    
    from setuptools import setup
    
    with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as f:
        readme = f.read()
    
    subprocess.call(['make', '-C', 'src'])
    
    setup(name='LSD',
      version='0.0.1',
      description='Python bindings for the LSD line segment detector.',
      long_description=readme,
      author='Geoff Hing',
      author_email='geoffhing@gmail.com',
      url='https://github.com/ghing/python-lsd',
      packages=['lsd'],
      package_data={'lsd': ['liblsd.so']},
      include_package_data=True,
      classifiers=[
          'Development Status :: 1 - Planning',
          'Intended Audience :: Developers',
          'License :: OSI Approved :: MIT License',
          'Operating System :: OS Independent',
          'Programming Language :: Python',
          'Programming Language :: C',
          ],
     )
    
    0 讨论(0)
  • 2021-02-04 06:05

    Given that you followed the instructions on how to create Python extensions in C, you should just enlist the extension modules like in this documentation.
    So the setup.py script of your library should look like this:

    from distutils.core import setup, Extension
    setup(
       name='your_python_library',
       version='1.0',
       ext_modules=[Extension('your_c_extension', ['your_c_extension.c'])],
    )
    

    and distutils knows how to compile your extension to C shared library and moreover where to put it.

    Of course I have no further information about your library, so you probably want to add more arguments to setup(...) call.

    0 讨论(0)
提交回复
热议问题