因此,這個檔案的最少內容如下:雖然 distutils 是一個標準庫模組,但建議讀者使用 setuptools 包來代替,因為它對標準的 distutils 做了一些改進。
from setuptools import setup setup( name='mypackage', )其中,name 給出了包的全名。
$Python setup.py --help-commands
tandard commands:
build build everything needed to install
clean clean up temporary files from 'build' command
install install everything from build directory
sdist create a source distribution (tarball, zip file)
register register the distribution with the PyP
bdist create a built (binary) distribution
check perform some checks on the package
upload upload binary package to PyPI
Extra commands:
develop install package in 'development mode'
alias define a shortcut to invoke one or more commands
test run unit t#sts after in-place build
bdist_wheel create a wheel distribution
usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts】...]
or: setup.py --help [cmd1 end2 ...]
or: setup.py --help-commands
or: setup.py cmd --help
[global]
quiet=1
[sdist]
formats=zip,tar
[bdist_wheel】
universal=1
注意,這只是為了便於說明,預設阻止每個命令的輸出可能並不是一個合理的選擇。
include HISTORY.txt
include README.txt
include CHANGES.txt
include CONTRIBUTORS.txt
include LICENSE
recursive-include *.txt *.py
MANIFEST.in 命令的完整列表可以在 distutils 官方文件中找到。
from setuptools import setup setup( name="solrq", # (...) classifiers=[ 'Development Status :: 4 - Beta', 'Intended Audience :: Developers', 'License :: OSI Approved :: BSD License', 'Operating System :: OS Independent', 'Programming Language :: Python', 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.2', 'Programming Language :: Python :: 3.3', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: Implementation :: PyPy', 'Topic :: Internet :: WWW/HTTP :: Indexing/Search', ], )它們在包定義中是完全可選的,但可以對 setup() 介面中可用的基本後設資料提供有用的擴充套件。
由於不時會新增新的分類器,所以在閱讀本教學時,這些數位可能會有所不同。當前可用的 trove 分類器的完整列表可以用 setup.py register --list-classifiers 命令來檢視。
from setuptools import setup setup( name="myproject", version="0.0.1", description="mypackage project short description", long_description=""" Longer description of mypackage project possibly with some documentation and/or usage examples """ install_requires=[ 'dependency1', 'dependency2', 'etc1', ] )這麼做當然可行,但從長遠來看很難維護,並且未來可能會出現錯誤和不一致。setuptools 和 distuitls 都不能從專案原始碼中自動提取各種後設資料資訊,因此需要自己提供這些佶息。
#用元組表示版本,可以簡單比較 VERSION = (0, 1, 1) #利用元組建立字串,以避免出現不一致 __version__ = ".".join([str(x) for x in VERSION])延期的 PEP 396 的另一個建議是,在 distutils 的 setup() 函數中提供的版本應該從 __version__ 派生,反之亦然。Python 打包使用者指南為單一來源的專案版本提供了多種模式,每一種都有自己的優點和侷限性。
from setuptools import setup import os def get_version(version_tuple): #additional handling of a,b,rc tags, this can #be simpler depending on your versioning scheme if not isinstance(version_tuple[-1], int): return '.'.join( map(str, version.tuple[:-1]) )+ version.tuple[-1] return '.'.join(map(str, version_tuple)) #path to the packages __init__ module in project #source tree init = os.path.join( os.path.dirname(__file__), 'src', 'some_package','__init__.py' ) version_line = list( filter(lambda l: l.startswith('VERSION'), open(init)) )[0] # VERSION is a tuple so we need to eval its line of code. # We could simply import it from the package but we # cannot be sure that this package is importable before # finishing its installation VERSION = get_version(eval (version.line.split('=') [-1])) setup( name='some-package', version=VERSION, #... )
try: from pypandoc import convert def read_md(f): return convert(f, 'rst') except ImportError: convert = None print( "warning: pypandoc module not found, could not convert Markdown to RST" ) def readjnd(f): return open(f, 'r').read() #noqa README = os.path.join (os.path.dirname(__file__), 'README.md') setup( name='some-package', long_description=read_md(README), #... )
from setuptools import setup setup( name = 'some-package', install_requires=['falcon', 'requests', 'delorean'] #... )有些 Python 開發者喜歡使用 requirements.txt 檔案來追蹤包的依賴列表。在某些情況下,你可能會找到這麼做的原因,但在大多數情況下,這是專案程式碼沒有正確打包的時代遺留的問題。
from setuptools import setup import os def strip_comments(l): return l.split ('#', 1)[0].strip() def reqs(*f): return list(filter(None, [strip_comments(l) for l in open(os.path.join(os.getcwd(), *f)).readlines()])) setup( name='some-package', install.requires = reqs('requirements.txt') #... )