如何在setup.py脚本中为Python扩展模块指定头文件?

时间:2011-07-09 08:19:47

标签: python distutils

如何在setup.py脚本中为Python扩展模块指定头文件?使用源文件列出如下不起作用。但我无法弄清楚在哪里列出它们。

from distutils.core import setup, Extension
from glob import glob

setup(
    name = "Foo",
    version = "0.1.0",
    ext_modules = [Extension('Foo', glob('Foo/*.cpp') + glob('Foo/*.h'))]
)

4 个答案:

答案 0 :(得分:15)

在setup.py之外添加 MANIFEST.in 文件,其中包含以下内容:

graft relative/path/to/directory/of/your/headers/

答案 1 :(得分:5)

我在setuptools上遇到了很多麻烦,它甚至都不再有趣了。 以下是我最终必须使用解决方法来生成带有头文件的工作源代码分发:我使用了package_data。

我分享这个,以便可能为其他人带来恶化。如果您知道更好的工作解决方案,请告诉我。

详情请见此处: https://bitbucket.org/blais/beancount/src/ccb3721a7811a042661814a6778cca1c42433d64/setup.py?fileviewer=file-view-default#setup.py-36

    # A note about setuptools: It's profoundly BROKEN.
    #
    # - The header files are needed in order to distribution a working
    #   source distribution.
    # - Listing the header files under the extension "sources" fails to
    #   build; distutils cannot make out the file type.
    # - Listing them as "headers" makes them ignored; extra options to
    #   Extension() appear to be ignored silently.
    # - Listing them under setup()'s "headers" makes it recognize them, but
    #   they do not get included.
    # - Listing them with "include_dirs" of the Extension fails as well.
    #
    # The only way I managed to get this working is by working around and
    # including them as "packaged data" (see {63fc8d84d30a} below). That
    # includes the header files in the sdist, and a source distribution can
    # be installed using pip3 (and be built locally). However, the header
    # files end up being installed next to the pure Python files in the
    # output. This is the sorry situation we're living in, but it works.

我的OSS项目中有相应的票证: https://bitbucket.org/blais/beancount/issues/72

答案 2 :(得分:3)

如果我没记错,你应该只需要指定源文件,它应该找到/使用标题。

在设置工具手册中,我看到了一些我相信的事情。

“例如,如果您的扩展程序需要在分发根目录下的include目录中包含头文件,请使用include_dirs选项”

Extension('foo', ['foo.c'], include_dirs=['include'])

http://docs.python.org/distutils/setupscript.html#preprocessor-options

答案 3 :(得分:3)

尝试使用标头kwarg来设置()。我不知道它在任何地方都有记录,但它确实有效。

setup(name='mypkg', ..., headers=['src/includes/header.h'])