如何在源代码分发中包含头文件?

How to include header file in source distribution?

我正在尝试创建一个包含简单 c 函数的源代码分发。这是我的文件。

# file setup.py
from setuptools import setup

setup(
    name="example",
    version="0.1",
    py_modules=["example", "build"], # If i dont include build.py, it will not find it.
    cffi_modules=["build.py:ffibuilder"],
    install_requires=["cffi"],
    setup_requires=["cffi"],
)
# file "build.py"

from cffi import FFI

ffibuilder = FFI()

SOURCE = """
#include "factorial.h"
"""

ffibuilder.cdef(
    """
long long int factorial(int n);
"""
)

ffibuilder.set_source(
    module_name="_example",
    source=SOURCE,
    sources=["factorial.c"],
    include_dirs=["."],
    library_dirs=["."],
)

if __name__ == "__main__":
    ffibuilder.compile(verbose=True)
// file "factorial.c"
#include "factorial.h"

long long int factorial(int n)
{
    long long int result = 1;
    int i;
    for (i = 1; i <= n; i++)
        result *= i;
    return result;
}
// file "factorial.h"
long long int factorial(int n);

有了这些文件,我 运行 命令

python setup.py sdist

生成文件“dist\example-0.1.tar.gz”。当我尝试使用

安装它时
pip install example-0.1.tar.gz

我明白了

build\temp.win-amd64-3.9\Release\_example.c(570): fatal error C1083: Cannot open include file: 'factorial.h': No such file or directory

那么如何在源代码分发中包含头文件?

在文件setup.py中添加,在setup函数里面package_data={"": ["*.h"]}。这将包括源分发中的所有头文件。

setup(
    name="example",
    version="0.1",
    py_modules=["example", "build"], # If i dont include build.py, it will not find it.
    cffi_modules=["build.py:ffibuilder"],
    install_requires=["cffi"],
    setup_requires=["cffi"],
    package_data={"": ["*.h"]} # <- This line
)