Search code examples
pythonpackagingdistutils

How do I package an unchanged C extension as part of my new Python package?


I've released a new version of a Python package to pypi without changing the C extension. Since I have only changed the Python code, not the C code, how do I package the shared libraries I have compiled for several platforms without having to recompile?


Solution

  • As an example for a library called 'somelib' with compiled libraries in two subdirectories, lib-i386 and lib-amd64:

    MANIFEST.in contains the following:

    include __init__.py
    include setup.py
    include somelib/*
    include somelib/lib-i386/*
    include somelib/lib-amd64/*
    

    setup.py contains (I've omitted lines unessential to the versioning issue):

    # Determine machine arhitecture
    arch = os.uname()[4]
    libname = "lib-%s" % (arch,)
    lib_files = glob.glob('./somelib/' + libname + '/*')
    data_files = [('somelib', 
                  lib_files + ['__init__.py', 'somelib/README.TXT']),]
    
    setup(
       ... 
       data_files=data_files
    )
    

    All the library objects are inside the package, but only the ones specific to 'arch' are installed.

    HTH.