依賴關系?
如果你使用Python, 很自然的你會使用其他人在PyPI或者其他地方公開發布的包
setuptools給我們提供了很方便的工具來說明依賴關系, 而且在安裝我們的包的時候會自動安裝依賴包.
我們可以給 funniest joke 添加一些格式, 使用 Markdown.
__init__.py
from markdown import markdown
def joke():
return markdown(u'How do you tell HTML from HTML5?'
u'Try it out in **Internet Explorer**.'
u'Does it work?'
u'No?'
u'It\'s HTML5.')
現在我們的包依賴 markdown
這個包. 我們需要在 setup.py
中添加 install_requires
參數:
from setuptools import setup
setup(name='funniest',
version='0.1',
description='The funniest joke in the world',
url='http://github.com/storborg/funniest',
author='Flying Circus',
author_email='flyingcircus@example.com',
license='MIT',
packages=['funniest'],
install_requires=[
'markdown',
],
zip_safe=False)
為了測試是否可行,我們可以試一試 python setup.py develop
$ python setup.py develop
running develop
running egg_info
writing requirements to funniest.egg-info/requires.txt
writing funniest.egg-info/PKG-INFO
writing top-level names to funniest.egg-info/top_level.txt
writing dependency_links to funniest.egg-info/dependency_links.txt
reading manifest file 'funniest.egg-info/SOURCES.txt'
writing manifest file 'funniest.egg-info/SOURCES.txt'
running build_ext
Creating /.../site-packages/funniest.egg-link (link to .)
funniest 0.1 is already the active version in easy-install.pth
Installed /Users/scott/local/funniest
Processing dependencies for funniest==0.1
Searching for Markdown==2.1.1
Best match: Markdown 2.1.1
Adding Markdown 2.1.1 to easy-install.pth file
Using /.../site-packages
Finished processing dependencies for funniest==0.1
當我們安裝funniest包的時候, pip install funniest
也會同時安裝 markdown
.
不在PyPI中的包?
有時候, 你需要一些按照setuptools格式組織的安裝包, 但是它們沒有在PyPI發布. 在這種情況下, 你可以在 dependency_links
中填入下載的URL, 可能需要在URL中加一些其他信息, setuptools將根據URL找到和安裝這些依賴包.
舉個例子, Github上的包可以按照下面的格式填寫URL:
setup(
...
dependency_links=['http://github.com/user/repo/tarball/master#egg=package-1.0']
...
)