溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

詳解Python打包分發(fā)工具setuptools

發(fā)布時(shí)間:2020-08-26 13:44:18 來(lái)源:腳本之家 閱讀:309 作者:云之澤 欄目:開(kāi)發(fā)技術(shù)

Python打包分發(fā)工具setuptools:曾經(jīng) Python 的分發(fā)工具是 distutils,但它無(wú)法定義包之間的依賴(lài)關(guān)系。setuptools 則是它的增強(qiáng)版,能幫助我們更好的創(chuàng)建和分發(fā) Python 包,尤其是具有復(fù)雜依賴(lài)關(guān)系的包。其通過(guò)添加一個(gè)基本的依賴(lài)系統(tǒng)以及許多相關(guān)功能,彌補(bǔ)了該缺陷。他還提供了自動(dòng)包查詢(xún)程序,用來(lái)自動(dòng)獲取包之間的依賴(lài)關(guān)系,并完成這些包的安裝,大大降低了安裝各種包的難度,使之更加方便,將程序打包以后可以可以安裝到自己的虛擬環(huán)境中,也可以上傳到PyPI,這樣非常方便大項(xiàng)目開(kāi)發(fā)

setuptools使用

pip 安裝:

$ pip install setuptools

第一個(gè)安裝文件 在目錄 learn_setup 下新建安裝文件 setup.py,然后創(chuàng)建包 myapp 模擬要打包源碼包:

├── myapp
│ └── __init__.py
└── setup.py

setup.py 文件內(nèi)容如下:

from setuptools import setup
setup(
 name='firstApp001', # 應(yīng)用名
 version='0.0.1', # 版本號(hào)
 packages=['myapp'], # 包括在安裝包內(nèi)的 Python 包
)

使用安裝文件創(chuàng)建 wheel 有了上面的 setup.py 文件,我們就可以打出各種安裝包,主要分為兩類(lèi):sdist 和 bdist。 Source distribution 使用 sdist 可以打包成 source distribution,支持的壓縮格式有:

詳解Python打包分發(fā)工具setuptools

使用方式為:

$ python setup.py sdist --formats=gztar,zip

目錄下便會(huì)多出 dist 和 *.egg-info 目錄,dist 內(nèi)保存了我們打好的包,上面命令使用 --formats 指定了打出 .tar.gz 和 .zip 包,如果不指定則如上表根據(jù)具體平臺(tái)默認(rèn)格式打包。 包的名稱(chēng)為 setup.py 中定義的 name, version以及指定的包格式,格式如:firstApp01-0.0.1.tar.gz。

Built distribution 使用 bdist 可以打出 built distribution,和源碼包相比,由于預(yù)先構(gòu)建好,所以安裝更快:

詳解Python打包分發(fā)工具setuptools

使用上,和 sdist 一樣,可以使用 --formats 指定包格式。如:

$ python setup.py bdist --formats=rpm

同時(shí)為了簡(jiǎn)化操作,setuptools 提供了如下命令:

詳解Python打包分發(fā)工具setuptools

所以上面打 rpm 包可以使用:

$ python setup.py bdist_rpm

Wheel Wheel 也是一種 built 包,而且是官方推薦的打包方式。也許你曾經(jīng)遇見(jiàn)或使用過(guò) egg 包,但現(xiàn)在wheel 是官方推薦的打包方式 使用 wheel 打包,首先要安裝 wheel:

$ pip install wheel

然后使用 bdist_wheel 打包:

$ python setup.py bdist_wheel

執(zhí)行成功后,目錄下除了 dist 和 *.egg-info 目錄外,還有一個(gè) build 目錄用于存儲(chǔ)打包中間數(shù)據(jù)。 wheel 包的名稱(chēng)如 firstApp01-0.0.1-py3-none-any.whl,其中 py3 指明只支持 Python3??梢允褂脜?shù) --universal,包名如 mfirstApp-0.0.1-py2.py3-none-any.whl,表明 wheel 包同時(shí)支持 Python2 和 Python3使用 universal 也成為通用 wheel 包,反之稱(chēng)為純 wheel 包。

安裝 Wheel 上一節(jié)的示例應(yīng)用沒(méi)有任何內(nèi)容。下面添加模塊 greet 并重新打包。

# one.py
def hello():
 print('Hello, welcome to setuptools!')

使用 bdist_wheel 再次打包后,我們可以使用 pip 安裝到本地 Python 的 site-packages 目錄。

$ pip install dist/fisrtApp001-0.0.1-py3-none-any.whl

現(xiàn)在和其他使用 pip 安裝的三方庫(kù)一樣使用:

from one.greet import hello
hello()

應(yīng)用開(kāi)發(fā)過(guò)程中會(huì)頻繁變更,每次安裝都需要先卸載舊版本很麻煩。使用 develop 開(kāi)發(fā)模式安裝的話(huà),實(shí)際代碼不會(huì)拷貝到 site-packages 下,而是除一個(gè)指向當(dāng)前應(yīng)用的鏈接(*.egg-link)。這樣當(dāng)前位置的源碼改動(dòng)就會(huì)馬上反映到 site-packages。使用如下

$ pip install -e .  # 或者 python setup.py develop

要是需要卸載,就使用pip uninstall 上傳 Wheel 到 PyPI Wheel 包可以自己使用和傳輸給其他人使用,但是維護(hù)更新不方便,而 PyPI 作為 Python 的 軟件倉(cāng)庫(kù),讓所有人可以方便的上傳和下載,以及管理三方庫(kù)。

注冊(cè) PyPI 賬號(hào) 登錄 pypi.python.org/pypi,進(jìn)入 Register 注冊(cè)賬號(hào)。

安裝 twine雖然 setuptools 支持使用 setup.py upload 上傳包文件到 PyPI,但只支持 HTTP 而被新的 twine 取代,同樣的,需要先安裝 twine:

$ pip install twine

使用 twine 上傳 使用 upload:

$ twine upload dist/*

輸入 username 和 password 即上傳至 PyPI。如果不想每次輸入賬號(hào)密碼,可以在家目錄下創(chuàng)建 .pypirc 文件,內(nèi)容如下:

[distutils]
index-servers =
 pypi
 pypitest

[pypi]
username: 
password:

[pypitest]
repository: https://test.pypi.org/legacy/
username: 
password:

填上自己的賬號(hào)密碼即可,這里配置了官方的 pypi 和 pypitest,若要配置其他倉(cāng)庫(kù),按格式添加。回到 PyPI 主頁(yè)即可看到上傳的 firstApp001

詳解Python打包分發(fā)工具setuptools

setup() 參數(shù)

詳解Python打包分發(fā)工具setuptools
詳解Python打包分發(fā)工具setuptools

上面的 setup.py 安裝文件內(nèi),我們已經(jīng)使用了 setup() 一些參數(shù):name, version, packages。 version 項(xiàng)目版本號(hào),一般由三部分組成:MAJOR, MINOR, MAINTENANCE。

  • MAJOR version when they make incompatible API changes,
  • MINOR version when they add functionality in a backwards-compatible manner, and
  • MAINTENANCE version when they make backwards-compatible bug fixes. 版本號(hào)的選擇參見(jiàn): packaging.python.org/tutorials/d… packages:列出項(xiàng)目?jī)?nèi)需要被打包的所有 package。一般使用 setuptools.find_packages() 自動(dòng)發(fā)現(xiàn)。
  • packages=find_packages(exclude=['contrib', 'docs', 'tests*'])
  • # exclude 用于排除不打包的 package

description:項(xiàng)目的簡(jiǎn)短描述,一般一句話(huà)就好,會(huì)顯示在 PyPI 上名字下端。

description='My first Python project'

對(duì)項(xiàng)目的完整描述,使用 long_description。如果此字符串是 rst 格式的,PyPI 會(huì)自動(dòng)渲染成 HTML 顯示。也可指定使用 markdown。

long_description=long_description,
long_description_content_type='text/x-rst'

url:通常為 GitHub上 的鏈接或者 readthedocs 的鏈接。

url='https://github.com/pypa/sampleproject'

author:作者信息

author='example',
author_email='example@example.com'

license:項(xiàng)目許可證

license='MIT'

關(guān)于各種許可證的介紹和選擇,參考:choosealicense.com/ classifiers:項(xiàng)目分類(lèi),完整可選項(xiàng)參考: pypi.python.org/pypi?%3Aact…

classifiers=[
 # How mature is this project? Common values are
 # 3 - Alpha
 # 4 - Beta
 # 5 - Production/Stable
 'Development Status :: 3 - Alpha',

 # Indicate who your project is intended for
 'Intended Audience :: Developers',
 'Topic :: Software Development :: Build Tools',

 # Pick your license as you wish (should match "license" above)
  'License :: OSI Approved :: MIT License',

 # Specify the Python versions you support here. In particular, ensure
 # that you indicate whether you support Python 2, Python 3 or both.
 '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',
],

keywords:項(xiàng)目關(guān)鍵詞列表

keywords='sample setuptools development'

project_urls:項(xiàng)目相關(guān)額外連接,如代碼倉(cāng)庫(kù),文檔地址等。

project_urls={
 'Documentation': 'https://packaging.python.org/tutorials/distributing-packages/',
 'Funding': 'https://donate.pypi.org',
 'Say Thanks!': 'http://saythanks.io/to/example',
 'Source': 'https://github.com/pypa/sampleproject/',
 'Tracker': 'https://github.com/pypa/sampleproject/issues',
}

install_requires:項(xiàng)目依賴(lài)的 Python 庫(kù),使用 pip 安裝本項(xiàng)目時(shí)會(huì)自動(dòng)檢查和安裝依賴(lài)。

install_requires=['pyyaml']

依賴(lài)的安裝參考: packaging.python.org/discussions… python_requires:指定項(xiàng)目依賴(lài)的 Python 版本

python_requires='>=3'

package_data:項(xiàng)目依賴(lài)數(shù)據(jù)文件,數(shù)據(jù)文件必須放在項(xiàng)目目錄內(nèi)且使用相對(duì)路徑

package_data={
 'myapp': ['data/*.yml'],
}

如果不指定作為目錄的鍵為空串,則代表對(duì)所有模塊操作(下例中將包含所有包內(nèi) data 目錄下的 yaml 文件):

package_data={
 '': ['data/*.yml'],
}

data_files:如果數(shù)據(jù)文件存在于項(xiàng)目外,則可以使用 data_files 參數(shù)或者 MANIFEST.in 文件進(jìn)行管理。如果用于源碼包,則使用 MANIFEST.in;如果用于 wheel,則使用 data_files。

data_files=[(‘mydata', [‘data/conf.yml'])]

上述設(shè)置將在打包 wheel 時(shí),將 data/conf.yml 文件添加至 mydata 目錄。(data_files 不能使用路徑通配符) 此外,scripts, py_modeles, entry_points, console_scripts 等參數(shù)參考: packaging.python.org/tutorials/d… 其他初始化文件 在閱讀 Github 上的 Python 庫(kù)時(shí),除了最基本核心的 setup.py 文件和主程序之外,還會(huì)看到其他一些文件。本節(jié)將介紹它們的作用和使用方法。

setup.cfg 包含了構(gòu)建時(shí)候的一些默認(rèn)參數(shù),如:

[bdist_wheel]
universal=1

用于在使用 bdist_wheel 的時(shí)候的默認(rèn)設(shè)置 --universal 參數(shù) 。

README.rst/README.md:項(xiàng)目說(shuō)明文檔,使用 reStrutruedText 可以在 PyPI 上很好的渲染,但 Markdown 則支持不夠好。

MANIFEST.in:此文件在打源碼包的時(shí)候告訴 setuptools 還需要額外打包哪些文件。

# Include the README
include *.md
# Include the license file
include LICENSE.txt
# Include the data files
recursive-include data *

LICENSE.txt:項(xiàng)目許可說(shuō)明文件 setuptools 默認(rèn)打包的文件:README.rst/README.md、setup.cfg、MANIFEST.in 所以其他的文件,如 LICENSE.txt,在源碼包時(shí)需要手動(dòng)在 MANIFEST.in 里添加 include,在 wheel 包時(shí)需要在 setup.cfg 添加:

[metadata]
license_file = LICENSE.txt


PyPI 上傳推薦配置

setup.py 
name
version
author
author_email
url
packages
description
package_data/data_files
setup.cfg
MANIFEST.in
README.rst
LICENSE.txt
<項(xiàng)目>

官網(wǎng)例子參考:

from setuptools import setup, find_packages
setup(
 name="HelloWorld",
 version="0.1",
 packages=find_packages(),
 scripts=['say_hello.py'],

 # Project uses reStructuredText, so ensure that the docutils get
 # installed or upgraded on the target machine
 install_requires=['docutils>=0.3'],

 package_data={
  # If any package contains *.txt or *.rst files, include them:
  '': ['*.txt', '*.rst'],
  # And include any *.msg files found in the 'hello' package, too:
  'hello': ['*.msg'],
 },

 # metadata to display on PyPI
 author="Me",
 author_email="me@example.com",
 description="This is an Example Package",
 keywords="hello world example examples",
 url="http://example.com/HelloWorld/", # project home page, if any
 project_urls={
  "Bug Tracker": "https://bugs.example.com/HelloWorld/",
  "Documentation": "https://docs.example.com/HelloWorld/",
  "Source Code": "https://code.example.com/HelloWorld/",
 },
 classifiers=[
  'License :: OSI Approved :: Python Software Foundation License'
 ]

 # could also include long_description, download_url, etc.
)

參考: setuptools.readthedocs.io/en/latest/s…

總結(jié)

以上所述是小編給大家介紹的Python打包分發(fā)工具setuptools,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI