2011-07-09 26 views
16

如何在Python擴展模塊的setup.py腳本中指定頭文件?如下所示用源文件列出它們不起作用。但我不知道還有什麼地方可以列出它們。如何在Python擴展模塊的setup.py腳本中指定頭文件?

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'))] 
) 
+0

嘗試導入setuptools的,而不是distutils.core。然後我相信它會自動拾取頭文件。如果這不起作用,請添加一個MANIFEST.in,如下所示 –

回答

13

添加MANIFEST.in文件之外的設置。 py與以下內容:

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

這不會告訴distutils安裝頭文件?我不想安裝頭文件;它們只能用作編譯器的輸入文件,只能安裝編譯器生成的pyd文件 – user763305

+2

不,不會安裝乳清文件MANIFEST不適用於安裝,但是對於源代碼分發(setup.py sdist)。 –

+2

太棒了!但是我很驚訝你需要一個MANIFEST來做這件簡單的事情。 – user763305

3

如果我沒有記錯,你應該只需要指定源文件,它應該找到/使用頭文件。

在setup-tools手冊中,我看到了一些與此相關的信息。

「舉個例子,如果你的擴展需要分發根目錄下的include目錄的頭文件,使用include_dirs選項」

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

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

+1

我試過了。它沒有找到標題。 – user763305

+0

我編輯了我的問題,看看它是否有效。 :) –

+0

試過ext_modules = [擴展('Foo',glob('Foo/*。cpp'),include_dirs = ['Foo'])]。不起作用;頭文件不會被添加到包中。 – user763305

3

嘗試標頭kwarg到setup()。我不知道它記錄在任何地方,但它的工作原理。

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

這應該確實有效。我不知道該文件是否因疏忽而被忽略,或者它是否應該是內部細節。 –

+3

@ÉricAraujo:我嘗試過了,但沒有奏效。我做了「setup.py sdist」,頭文件不包含在Foo-0.1.0.zip中。 – user763305

+3

sdist命令的代碼中實際上有一個說明,表示不包含頭文件,而另一個在build_ext(它定義了sdist用於構建其文件列表的一部分的get_source_file函數)中的另一個註釋是:「不會如果我們也知道頭文件的名字,它會很整潔「。 distutils關閉了新的功能(我們現在在distutils2上工作),但我傾向於考慮這個bug並修復它用於Python 2.7和3.2的下一個版本。稍後我會在bugs.python.org上打開一個報告。 –

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

相關問題