目的
將項目封裝為python模塊;
需要注意的地方,比如對其他包的依賴,內部結構(子包),版本控制,目標用戶和包的形式(源文件還是二進制文件)
示例
代碼
根目錄包含了各種各樣的配置文件(setup.py 是一個必須的並且最重要的配置文件),代碼本身通常在一個子目錄下,目錄名就是包的名稱。當然最好還有一個測試文件目錄
├── LICENSE ├── MANIFEST.in ├── README.md ├── benyo_zbx │ ├── __init__.py │ └── lib │ ├── __init__.py │ ├── alert.py │ ├── base.py │ ├── event.py │ ├── host.py │ ├── item.py │ ├── service.py │ ├── history.py │ └── trigger.py ├── docs │ ├── test │ ├── documents.txt │ └── releasing.rst ├── tests │ ├── manuals.rst │ ├── changelog.txt │ └── changelog.txt ├── requirements.txt ├── setup.cfg ├── setup.py ├── test-requirements.txt └── requirements.txt
它從setuptools 包中導入了setup()函數;
setup.py 是一個常規的 Python文件; 可以在文件里面做任何你想做的事情,主要工作是用適當的參數去調用 setup() 函數。因為在安裝包的時候,setup() 函數將會被各種各樣的工具以標准的方法調用。
配置文件
setup.py
- name:包的名稱(以及如何在 PYPI 上呈現) - version:這對於保持適當的依賴關系至關重要 - url:包的鏈接,通常為 Github 上的鏈接,或者是 readthedocs 鏈接 - packages:需要包含的子包列表,find_packages()將幫助我們查找 - setup_requires:指定依賴項 - test_suite:測試時運行的工具
詳細代碼如下
import codecs import os import sys try: from setuptools import setup except: from distutils.core import setup def read(fname): return codecs.open(os.path.join(os.path.dirname(__file__), fname)).read() long_des = read("README.rst") platforms = ['linux/Windows']
classifiers = [ 'Development Status :: 3', 'Topic :: Text Processing', 'License :: OSI Approved :: MIT License', 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.7', ] install_requires = [ #'numpy>=1.11.1', #'pandas>=0.19.0' 'requests' ] setup(name='benyo_zbx', version='0.0.1', description='A test module for DataXujing', long_description=long_des, py_modules=['benyo_zbx'], author = "Benyo yuan", author_email = "yuyuhupo@outlook.com" , url = "https://benyo.github.io" , license="Apache License, Version 2.0", platforms=platforms, classifiers=classifiers, install_requires=install_requires )
MANIFEST.in
此文件中包含有不屬於內部包目錄,但你仍想納入進來的文件。這些文件通常是 readme 文件,license 文件以及一些類似的文件。其中,比較重要的一個是 requirements.txt。 pip 使用該文件安裝其他必須的包。
include README.rst include LICENSE include requirements.txt recursive-include benyo_zbx * recursive-include docs * recursive-include tests *
recursive-includ
表明包含子目錄。別急!!!還有一件事要做,就是在setup.py
中將include_package_data
參數設為True。
setup(name='benyo_zbx', version='1.0.0', description='A zabbix api for pypi', long_description=long_des, author="Benyo yuan", packages=['benyo_zbx'], author_email="yuyuhupo@outlook.com", url="https://benyo.github.io", license="Apache License, Version 2.0", platforms=platforms, classifiers=classifiers, install_requires=install_requires, include_package_data=True )
packages 就是這個參數沒有加,導致在安裝python包的時候,不能正常使用;
long_description=__doc__, 從代碼中獲取文檔注釋
install_requires=[ # 依賴列表 'Flask>=0.10', 'Flask-SQLAlchemy>=1.5,<=2.1' ],
dependency_links=[ # 依賴包下載路徑 'http://example.com/dependency.tar.gz' ]
依賴項
您可以在 setup.py 文件的 install_requires 部分和 requirements.txt 文件中指定依賴項。Pip 將會自動安裝 install_requires 中列出的依賴項,而不是 requirements.txt 文件。要安裝后者中指定的依賴項,在運行 pip 時必須明確指定:pip install -r requirements.txt。
install_requires 選項旨在指定所要求模塊的最低主版本號等較抽象的要求。而在 requirements.txt 文件的要求更加具體,通常細致到次版本號。
發布
兩種發布形式:一是源代碼發布;二是二進制版本發布。
源發布版
使用python setup.py sdist build
命令創建發布文件。 下面是創建時的輸出
running sdist running egg_info creating benyo-zbx.egg-info writing requirements to benyo-zbx.egg-info\requires.txt writing benyo-zbx.egg-info\PKG-INFO writing top-level names to benyo-zbx.egg-info\top_level.txt writing dependency_links to benyo-zbx.egg-info\dependency_links.txt writing manifest file 'benyo-zbx.egg-info\SOURCES.txt' file benyo-zbx.py (for module Pyzabbix) not found reading manifest file 'benyo-zbx.egg-info\SOURCES.txt' reading manifest template 'MANIFEST.in' warning: no files found matching 'benyo-zbx' warning: no files found matching 'docs' warning: no files found matching 'tests' writing manifest file 'benyo-zbx.egg-info\SOURCES.txt' running check creating benyo-zbx-0.6.1 creating benyo-zbx-0.6.1\benyo-zbx.egg-info copying files to benyo-zbx-0.6.1... copying LICENSE -> benyo-zbx-0.6.1 copying MANIFEST.in -> benyo-zbx-0.6.1 copying README.rst -> benyo-zbx-0.6.1 copying setup.py -> benyo-zbx-0.6.1 copying benyo-zbx.egg-info\PKG-INFO -> benyo-zbx-0.6.1\benyo-zbx.egg-info copying benyo-zbx.egg-info\SOURCES.txt -> benyo-zbx-0.6.1\benyo-zbx.egg-info copying benyo-zbx.egg-info\dependency_links.txt -> benyo-zbx-0.6.1\benyo-zbx.egg-info copying benyo-zbx.egg-info\requires.txt -> benyo-zbx-0.6.1\benyo-zbx.egg-info copying benyo-zbx.egg-info\top_level.txt -> benyo-zbx-0.6.1\benyo-zbx.egg-info Writing benyo-zbx-0.6.1\setup.cfg creating dist Creating tar archive removing 'benyo-zbx-0.6.1' (and everything under it)
打包過程中會創建一些文件(夾)
├── build │ └── bdist.win-amd64 │ ├── dist │ ├── Pyzabbix-0.6.1.tar.gz │ └── Pyzabbix-0.6.1-py2.7.egg │ └── Pyzabbix.egg-info ├── dependency_link.txt ├── PKG_INFO ├── requires.txt ├── SOURCES.txt └── top_level.txt
測試安裝(兩種方式)
# 當前路徑下 1、python setup.py install # 使用pip安裝 2、pip install dist/Pyzabbix-0.6.1.tar.gz
Processing d:\pypi ???\pyzabbix\dist\benyo-zbx-0.6.1.tar.gz Requirement already satisfied: numpy>=1.11.1 in c:\python27\lib\site-packages (from benyo-zbx==0.6.1) Requirement already satisfied: pandas>=0.19.0requests in c:\python27\lib\site-packages (from benyo-zbx==0.6.1) Requirement already satisfied: pytz>=2011k in c:\python27\lib\site-packages (from pandas>=0.19.0requests->benyo-zbx==0.6.1) Requirement already satisfied: python-dateutil>=2.5.0 in c:\python27\lib\site-packages (from pandas>=0.19.0requests->benyo-zbx==0.6.1) Requirement already satisfied: six>=1.5 in c:\python27\lib\site-packages (from python-dateutil>=2.5.0->pandas>=0.19.0requests->benyo-zbx==0.6.1) Installing collected packages: benyo-zbx Running setup.py install for benyo-zbx... done Successfully installed benyo-zbx-0.6.1
導入包,測試功能是否正常
# python >>> from benyo_zbx import ZabbixFace >>>
測試成功 ! @_@
Wheel(二進制)
Wheels 是打包 Python 代碼和擴展 C 語言的一個相對新的方法;替換了 egg 格式。
創建純 Python 或者平台 Wheels 的命令是:"python setup.py bdist_wheel --universal"
running bdist_wheel running build installing to build\bdist.win-amd64\wheel running install running install_egg_info running egg_info writing requirements to Pyzabbix.egg-info\requires.txt writing benyo-zbx.egg-info\PKG-INFO writing top-level names to benyo-zbx.egg-info\top_level.txt writing dependency_links to benyo-zbx.egg-info\dependency_links.txt reading manifest file 'benyo-zbx.egg-info\SOURCES.txt' reading manifest template 'MANIFEST.in' writing manifest file 'benyo-zbx.egg-info\SOURCES.txt' Copying Pyzabbix.egg-info to build\bdist.win-amd64\wheel\.\benyo-zbx-0.0.1-py2.7.egg-info running install_scripts adding license file "LICENSE" (matched pattern "LICEN[CS]E*") creating build\bdist.win-amd64\wheel\benyo-zbx-0.0.1.dist-info\WHEEL creating 'dist\benyo-zbx-0.0.1-py2.py3-none-any.whl' and adding 'build\bdist.win-amd64\wheel' to it adding 'benyo-zbx-0.0.1.dist-info/LICENSE' adding 'benyo-zbx-0.0.1.dist-info/METADATA' adding 'benyo-zbx-0.0.1.dist-info/WHEEL' adding 'benyo-zbx-0.0.1.dist-info/top_level.txt' adding 'benyo-zbx-0.0.1.dist-info/RECORD' removing build\bdist.win-amd64\wheel
配置 pypi
(1)為了發布包,必須在 pypi.org 注冊一個用戶,注冊郵箱需要驗證。
到 PyPi 注冊
Username: benyo Fullname: benyo yuan 密碼: ********** 郵箱: yuyuhupo@outlook.com
(2)配置 $HOME”.pypirc” 文件
[distutils] index-servers=pypi [pypi] repository = https://upload.pypi.org/legacy/ username = benyo password = **********
上傳到pypi
使用setuptools上傳
# 上傳source 包 python setup.py sdist upload # 上傳pre-compiled包 python setup.py bdist_wheel upload
使用twine上傳
# 使用twine上傳,先安裝twine sudo pip install twine
# 上傳 twine upload dist/*