2013-07-18 77 views
14

爲「寫Setupscript(http://docs.python.org/2/distutils/setupscript.html)蟒蛇文檔提到的依賴關係可以根據第一個Python包創建setup.py文件時,如何指定依賴

> 2.4. Relationships between Distributions and Packages

[...] These relationships can be specified using keyword arguments to the distutils.core.setup() function.

Dependencies on other Python modules and packages can be specified by supplying the requires keyword argument to setup(). The value must be a list of strings. Each string specifies a package that is required, and optionally what versions are sufficient.

To specify that any version of a module or package is required, the string should consist entirely of the module or package name. Examples include 'mymodule' and 'xml.parsers.expat'.

[...]

指定基於這個稀疏信息,而不一個例子,我只是想確保我這樣做是正確的。另外,我找不到在API說明此requires參數http://docs.python.org/2/distutils/apiref.html#distutils.core.setup

因此,它是做過這樣的,例如,

setup(name='MyStuff', 
     version='1.0', 
     requires='os, sys, progressbar', 
     [...] 

我希望有人能給我多一點見解!謝謝!

編輯:

爲了解決distutils.core,setuptools的爭論,人們可以簡單地做

try: 
    from setuptools import setup 
except ImportError: 
    from distutils.core import setup 

是否有意義?

+1

整個Python包裝系統記錄不完全,主要是因爲有太多不同的地方,部分非常好,但矛盾的信息。我總是看現有的項目。如果你沒有特殊需求,你只需要distutils。例如瓶頸就是'setup.py'文件的一個很好的例子,它只使用distutils:https://github.com/kwgoodman/bottleneck/blob/master/setup.py –

+0

@ Jan-PhilipGehrcke:然而'瓶頸'未能指定它在元數據中需要'numpy'。 ''setuptools'現在有了更好的文檔,這要歸功於'distribute'分叉被合併回來:http://pythonhosted.org/setuptools/ –

+0

對,你甚至開始立即導入numpy。 –

回答

19

忽略distutils。如果您想要創建一個程序包來指定pip之類的工具的依賴關係,那麼您需要將setup.py關閉爲setuptools

setuptools依賴性在install_requires上市,這需要一個列表:

setup(name='MyStuff', 
     version='1.0', 
     install_requires=['progressbar'], 
     # ... 
) 

這應該是他們自己的分佈。 ossys是Python包含的模塊,不應列爲

+4

我最近有一個關於這個問題的更長時間的討論,並且確信在目前的情況下,一個很好的習慣用法是'try:從setuptools import setup;除了ImportError:from distutils.core import setup'。 –

+0

@ Jan-PhilipGehrcke只是從'distutils.core''' setup'不支持'install_requires'。因此,如果您嘗試使用它,您將看到如下信息:*/usr/lib/python3.4/distutils/dist.py:260:UserWarning:未知分發選項:'install_requires'*。所以鑑於這種差異和其他差異,我認爲這是一個非常好的習慣用語,除了最簡單的情況之外。 – Six

相關問題