用Python將庫打包發(fā)布到pypi
如果需要將自己寫好的python打包,并發(fā)布到pypi,這樣其他人就可以直接通過pip install
來安裝對(duì)應(yīng)的包,可以參考如下教程
1. 注冊(cè)pypi賬號(hào)并創(chuàng)建token
首先訪問https://pypi.org/ 并注冊(cè)賬號(hào)
然后跳轉(zhuǎn)到賬號(hào)設(shè)置
然后選擇API token->Add API token
輸入token name并在Scope中選擇Entire account(第一次需要選擇Entire account)
然后在本地,修改.pypirc
文件
輸入的內(nèi)容為:
[pypi] username = __token__ password = {token}
只需要修改{token}
為自己的token即可
2. 編寫setup.py和setup.cfg
setup.cfg的內(nèi)容為
[metadata] license_files = LICENSE.txt
LICENSE.txt是license文件,需要自行編寫
setup.py在根目錄下,一個(gè)示例為
from setuptools import setup import compileall from os import path # 讀取readme文件,這樣可以直接顯示在主頁上 this_directory = path.abspath(path.dirname(__file__)) with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f: long_description = f.read() compileall.compile_dir("src") setup( name='my-python', version='1.0.2', packages=['src', 'src.main', 'src.main.config'], url='https://github.com/hTangle', license='Apache 2.0', author='hTangle', author_email='', description='', keywords='', python_requires='>=3.4, <4', long_description=long_description, long_description_content_type='text/markdown', install_requires=['requests'] )
具體的字段含義如下:
name
: 包名
version
: 版本號(hào),支持如下形式
1.2.0.dev1 # Development release 1.2.0a1 # Alpha Release 1.2.0b1 # Beta Release 1.2.0rc1 # Release Candidate 1.2.0 # Final Release 1.2.0.post1 # Post Release 15.10 # Date based release 23 # Serial release
description
: 包描述,會(huì)放在如下圖所示的位置處
url
: 包的鏈接,可以使用github鏈接,pypi會(huì)自動(dòng)獲取到倉庫的信息,示例如下:
author
: 作者
license
: 許可證
classifiers
: 分類,示例如下:
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.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: 3.9', ],
keywords
: 關(guān)鍵字,和論文的關(guān)鍵字類似
project_urls
: 一些項(xiàng)目的其他鏈接,示例如下
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', },
packages
: 需要打包的目錄,需要以根目錄為起點(diǎn),可以使用
find_packages
自動(dòng)查找包,注意不要漏寫
install_requires
: 包依賴的其他包
python_requires
: python的版本需求
package_data
: 需要的額外的文件,例如包強(qiáng)依賴一個(gè)本地文件,可以使用如下
package_data={ 'sample': ['package_data.dat'], },
3. 打包
打包命令為
python setup.py cmd
cmd可以取值為
bdist_wheel : create a wheel distribution
bdist_egg : create an “egg” distribution
sdist : create a source distribution (tarball, zip file, etc.)
bdist : create a built (binary) distribution
bdist_dumb : create a “dumb” built distribution
bdist_rpm : create an RPM distribution
bdist_wininst : create an executable installer for MS Windows
打包為tar.gz
python setup.py sdist
打包好的文件再dist目錄下
4. 上傳
可以首先使用twine
對(duì)包進(jìn)行檢查
twine check dist/*
輸出如下
再運(yùn)行上傳命令
twine upload dist/*
到此這篇關(guān)于用Python將庫打包發(fā)布到pypi的文章就介紹到這了,更多相關(guān)python打包到pypi內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文帶你精通Python中*args和**kwargs的應(yīng)用技巧
如果能在Python中創(chuàng)建適應(yīng)不同場(chǎng)景的函數(shù),而無需每次都重寫它們,會(huì)使得操作簡(jiǎn)潔方便,這就是*args和**kwargs的魔力所在,下面我們就來看看它們的具體一些應(yīng)用技巧吧2024-03-03100行Python代碼實(shí)現(xiàn)每天不同時(shí)間段定時(shí)給女友發(fā)消息
這篇文章主要介紹了100行Python代碼,每天不同時(shí)間段定時(shí)給女友發(fā)消息,本文給出了實(shí)現(xiàn)思路,代碼簡(jiǎn)單易懂非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09python調(diào)用excel_vba的兩種實(shí)現(xiàn)方式
本文主要介紹了python調(diào)用excel_vba的兩種實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01解決Pytorch半精度浮點(diǎn)型網(wǎng)絡(luò)訓(xùn)練的問題
這篇文章主要介紹了解決Pytorch半精度浮點(diǎn)型網(wǎng)絡(luò)訓(xùn)練的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05Opencv圖像處理:如何判斷圖片里某個(gè)顏色值占的比例
這篇文章主要介紹了Opencv圖像處理:如何判斷圖片里某個(gè)顏色值占的比例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的QQ截圖
大家好,本篇文章主要講的是Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的QQ截圖,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下的相關(guān)資料2022-02-02python嵌套字典比較值與取值的實(shí)現(xiàn)示例
這篇文章主要給大家介紹了關(guān)于python嵌套字典比較值與取值的實(shí)現(xiàn)方法,詳細(xì)介紹了python字典嵌套字典的情況下獲取某個(gè)key的value的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),需要的朋友們下面來一起看看吧。2017-11-11Python真題案例之錯(cuò)位鍵盤?單詞長(zhǎng)度?字母重排詳解
這篇文章主要介紹了python實(shí)操案例練習(xí),本文給大家分享的案例中主要任務(wù)有錯(cuò)位鍵盤、單詞長(zhǎng)度、字母重排,需要的小伙伴可以參考一下2022-03-03Python結(jié)合Sprak實(shí)現(xiàn)計(jì)算曲線與X軸上方的面積
這篇文章主要介紹了Python結(jié)合Sprak實(shí)現(xiàn)計(jì)算曲線與X軸上方的面積,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02python PaddleOCR庫用法及知識(shí)點(diǎn)詳解
在本篇內(nèi)容里小編給大家分享的是一篇關(guān)于python PaddleOCR庫用法及知識(shí)點(diǎn)詳解內(nèi)容,對(duì)此有需要的朋友們可以學(xué)習(xí)參考下。2021-07-07