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

Joh*_*åde 21 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'))]
)
Run Code Online (Sandbox Code Playgroud)

iEl*_*ric 16

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

graft relative/path/to/directory/of/your/headers/
Run Code Online (Sandbox Code Playgroud)

  • 大!但是我很惊讶你需要一个简单的做法来做这么简单的事情. (3认同)
  • 不,乳清不会安装.MANIFEST不是用于安装,而是用于源代码分发(setup.py sdist). (2认同)
  • @Éric Araujo:我很惊讶地了解到 distutils 知道源文件而不是头文件。 (2认同)

小智 7

尝试将标题 kwarg 设置为 setup()。我不知道它在任何地方都有记录,但它有效。

setup(name='mypkg', ..., headers=['src/includes/header.h'])
Run Code Online (Sandbox Code Playgroud)

  • @Éric Araujo:我试过了,但没有用。我做了“setup.py sdist”,标题没有包含在 Foo-0.1.0.zip 中。 (3认同)
  • 在 sdist 命令的代码中实际上有一条注释说不包含标头,而在 build_ext(它定义了 sdist 用来构建其文件列表的一部分的 get_source_file 函数)中的另一条注释说“不是吗?如果我们也知道头文件的名称,那就很整洁了”。distutils 关闭了新功能(我们现在在 distutils2 上工作),但我倾向于认为这是一个错误并在 Python 2.7 和 3.2 的下一个版本中修复它。稍后我将打开关于 bugs.python.org 的报告。 (3认同)

bla*_*ais 5

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

我正在分享这个,以便潜在地拯救别人的恶化.如果您知道更好的工作解决方案,请告诉我.

详情请见:http: //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.
Run Code Online (Sandbox Code Playgroud)

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