一、背景
想讓自己寫的python庫可以使用pip install xxx
安裝。
二、環境準備
- 注冊PYPI賬號
- 已經寫好的能正常使用的庫/方法/項目(可以本地調用)
- 安裝依賴庫
setuptools
和twinw
pip install setuptools
pip install twine # 簡化將庫發布到PYPI流程的工具
pip install wheel # 制作whl文件需要
三、步驟
1. 調整目錄結構
參考python項目打包結構
|--mypackage__init__.py__version__.py|--common|--config|--src
setup.py
requirements.txt
LICENSE
README.rst
MANIFEST.in
.gitnore
目錄文件的作用:
- MANIFEST.in
當你使用 Python 的打包工具(如 setuptools 或 distutils)創建分發包時,
它們只會自動包含項目目錄中的 Python 模塊和包。但是,在某些情況下,你可
能還需要在分發包中包含一些其他的文件,如文檔、配置文件、數據文件等。這
時,就可以使用 MANIFEST.in 文件來指定要包含的其他文件。
include DrissionPage/configs/configs.ini
include DrissionPage/*.pyi
include DrissionPage/*/*.py
include DrissionPage/*/*.pyi
- setup.py
# -*- coding:utf-8 -*-
from setuptools import setup, find_packageswith open("README.md", "r", encoding='utf-8') as fh:long_description = fh.read()setup(name="DrissionPage",version="3.2.31",author="g1879",author_email="g1879@qq.com",description="Python based web automation tool. It can control the browser and send and receive data packets.",long_description=long_description,long_description_content_type="text/markdown",license="BSD",keywords="DrissionPage",url="https://gitee.com/g1879/DrissionPage",include_package_data=True,packages=find_packages(),zip_safe=False,install_requires=['lxml','requests','cssselect','DownloadKit>=1.0.0','FlowViewer>=0.3.0','websocket-client','click','tldextract'],classifiers=["Programming Language :: Python :: 3.6","Development Status :: 4 - Beta","Topic :: Utilities","License :: OSI Approved :: BSD License",],python_requires='>=3.6',entry_points={'console_scripts': ['dp = DrissionPage.commons.cli:main',],},
)
字段說明:
-
version:這個簡單,就是包的發布的版本,可以直接寫在這,也可以從其他地方引用過來。
-
long_description:默認是rst(reStructuredText )格式的,也可以是其他格式,使用
long_description_content_type
字段指定,這個里面的內容是顯示在pypi包首頁上。 -
packages:申明你的包里面要包含的目錄,比如 [‘mypackage’, ‘mypackage_test’] 可以是這種使用我的示例,讓setuptools自動決定要包含哪些包
-
install_requires:申明依賴包,安裝包時pip會自動安裝
-
LICENSE
BSD 3-Clause LicenseCopyright (c) 2020, g1879
All rights reserved.Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:* Redistributions of source code must retain the above copyright notice, thislist of conditions and the following disclaimer.* Redistributions in binary form must reproduce the above copyright notice,this list of conditions and the following disclaimer in the documentationand/or other materials provided with the distribution.* Neither the name of the copyright holder nor the names of itscontributors may be used to endorse or promote products derived fromthis software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- README.md
項目簡介,markdown格式
2. 打包上傳
項目按照上述結構組織好之后,就可以進行打包了
- check setup.py
python setup.py check
若沒問題,無報錯信息。
- 生成打包文件
python setup.py bdist_wheel # 編譯生成wheel文件,會產生build,dist,XX.egg-info三個文件夾
pip wheel --wheel-dir=whl文件保存路徑 setup.py文件所在路徑 # 直接生成whl文件,不產生中間編譯結果
python setup.py sdist # 生成tar包,即源碼包
- 上傳生成的包
python setup.py sdist upload # 上傳source包
python setup.py bdist_wheel upload # 上傳二進制包
- 包的安裝
# 源碼包安裝,解壓+編譯+安裝
python setup.py build
python setup.py install
# whl包
pip install XX.whl
- 測試庫是否可以正常安裝
python setup.py develop
# 該方法不會真正的安裝包,而是在系統環境中創建一個軟鏈接指向包實際所在目錄。
# 這邊在修改包之后不用再安裝就能生效,便于調試