Home:ALL Converter>Building Python-C Extension using CFFI, but Setuptools does not include custom header files in build

Building Python-C Extension using CFFI, but Setuptools does not include custom header files in build

Ask Time:2019-07-09T08:02:25         Author:rafferino

Json Formatter

I'm trying to use the CFFI package in Python to create a Python interface for already existing C-code.

I am able to compile a C library by following this blog post. Now I want to make it so that this python library is available without any fancy updates to the sys.path.

I found that maybe creating a distribution through Python's setuptools setup() function would accomplish this and I got it to mostly work by creating a setup.py file as such

import os
import sys

from setuptools import setup, find_packages

os.chdir(os.path.dirname(sys.argv[0]) or ".")

setup(
    name="SobelFilterTest",
    version="0.1",
    description="An example project using Python's CFFI",
    packages=find_packages(),
    install_requires=["cffi>=1.0.0"],
    setup_requires=["cffi>=1.0.0"],
    cffi_modules=[
        "./src/build_sobel.py:ffi",
        "./src/build_file_operations.py:ffi",
    ],
)

, but I run into this error

build/temp.linux-x86_64-3.5/_sobel.c:492:19: fatal error: sobel.h: No such file or directory

From what I can tell, the problem is that the sobel.h file does not get uploaded into the build folder created by setuptools.setup(). I looked for suggestions of what to do including using Extensions() and writing a MANIFEST.in file, and both seem to add a relative path to the correct header files:

MANIFEST.in
setup.py
SobelFilterTest.egg-info/PKG-INFO
SobelFilterTest.egg-info/SOURCES.txt
SobelFilterTest.egg-info/dependency_links.txt
SobelFilterTest.egg-info/requires.txt
SobelFilterTest.egg-info/top_level.txt
src/file_operations.h
src/macros.h
src/sobel.h

But I still get the same error message. Is there a correct way to go about adding the header file to the build folder? Thanks!

Author:rafferino,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/56943836/building-python-c-extension-using-cffi-but-setuptools-does-not-include-custom-h
yy