Python庫(kù)如何打包到PyPI
Python庫(kù)打包到PyPI
打開(kāi)pypi官網(wǎng), 并注冊(cè)賬號(hào)
創(chuàng)建并編輯.pypirc (注: 家目錄下創(chuàng)建)
tianshl@tianshl ~ $ vim .pypirc [pypirc] index-servers = pypi pypitest [pypi] repository=https://pypi.python.org/pypi [pypitest] repository=https://testpypi.python.org/pypi [server-login] username:tianshl password:******
前提
1. 要打包的代碼必須是一個(gè)包(package)
2. 代碼是開(kāi)源的
包(package) 創(chuàng)建包
右鍵 / New / Python Package / 輸入包名 / OK
創(chuàng)建成功后,查看目錄結(jié)構(gòu)
tianshl@tianshl wechat $ tree . └── wxReply └── __init__.py
實(shí)際上就是文件夾中包含__init__.py文件
編寫要打包的源代碼
tianshl@tianshl wechat $ tree . └── wxReply ├── __init__.py └── wxReply.py 1 directory, 2 files
創(chuàng)建README.rst和setup.py文件
- README.rst為說(shuō)明文檔
- setup.py為安裝腳本 (核心)
此時(shí)wechat包的目錄結(jié)構(gòu)
tianshl@tianshl wechat $ tree . ├── README.rst ├── setup.py └── wxReply ├── __init__.py └── wxReply.py 1 directory, 4 files
編輯setup.py內(nèi)容
# -*- coding: utf-8 -*- from setuptools import setup, find_packages from codecs import open from os import path here = path.abspath(path.dirname(__file__)) with open(path.join(here, 'README.rst'), encoding='utf-8') as f: long_description = f.read() __author__ = 'tianshl' __date__ = '2017/01/26' setup( name='wxReply', # 名稱 version='1.0.6', # 版本號(hào) description='wxReply', # 簡(jiǎn)單描述 long_description=long_description, # 詳細(xì)描述 classifiers=[ 'License :: OSI Approved :: MIT License', 'Programming Language :: Python', 'Intended Audience :: Developers', 'Operating System :: OS Independent', ], keywords='wechat robot weixin wxReply', # 關(guān)鍵字 author='tianshl', # 作者 author_email='xiyuan91@126.com', # 郵箱 url='https://my.oschina.net/tianshl/blog', # 包含包的項(xiàng)目地址 license='MIT', # 授權(quán)方式 packages=find_packages(), # 包列表 install_requires=['requests', 'itchat'], include_package_data=True, zip_safe=True, )
校驗(yàn) (python setup.py check)
tianshl@tianshl wxReply $ python3 setup.py check running check
注: running check 表示沒(méi)問(wèn)題, 其他情況對(duì)照提示修改即可
打包 (python setup.py sdist)
tianshl@tianshl wechat $ python3 setup.py sdist running sdist running egg_info creating wxReply.egg-info writing wxReply.egg-info/PKG-INFO writing dependency_links to wxReply.egg-info/dependency_links.txt writing requirements to wxReply.egg-info/requires.txt writing top-level names to wxReply.egg-info/top_level.txt writing manifest file 'wxReply.egg-info/SOURCES.txt' reading manifest file 'wxReply.egg-info/SOURCES.txt' writing manifest file 'wxReply.egg-info/SOURCES.txt' running check creating wxReply-1.0.6 creating wxReply-1.0.6/wxReply creating wxReply-1.0.6/wxReply.egg-info copying files to wxReply-1.0.6... copying README.rst -> wxReply-1.0.6 copying setup.py -> wxReply-1.0.6 copying wxReply/__init__.py -> wxReply-1.0.6/wxReply copying wxReply/wxReply.py -> wxReply-1.0.6/wxReply copying wxReply.egg-info/PKG-INFO -> wxReply-1.0.6/wxReply.egg-info copying wxReply.egg-info/SOURCES.txt -> wxReply-1.0.6/wxReply.egg-info copying wxReply.egg-info/dependency_links.txt -> wxReply-1.0.6/wxReply.egg-info copying wxReply.egg-info/requires.txt -> wxReply-1.0.6/wxReply.egg-info copying wxReply.egg-info/top_level.txt -> wxReply-1.0.6/wxReply.egg-info copying wxReply.egg-info/zip-safe -> wxReply-1.0.6/wxReply.egg-info Writing wxReply-1.0.6/setup.cfg creating dist Creating tar archive removing 'wxReply-1.0.6' (and everything under it)
此時(shí)wechat包的目錄結(jié)構(gòu)
tianshl@tianshl wechat $ tree . ├── README.rst ├── dist │ └── wxReply-1.0.6.tar.gz ├── setup.py ├── wxReply │ ├── __init__.py │ └── wxReply.py └── wxReply.egg-info ├── PKG-INFO ├── SOURCES.txt ├── dependency_links.txt ├── requires.txt ├── top_level.txt └── zip-safe 3 directories, 11 files
上傳
tianshl@tianshl wxReply $ python3 setup.py sdist register upload running sdist running egg_info writing wxReply.egg-info/PKG-INFO writing dependency_links to wxReply.egg-info/dependency_links.txt writing requirements to wxReply.egg-info/requires.txt writing top-level names to wxReply.egg-info/top_level.txt reading manifest file 'wxReply.egg-info/SOURCES.txt' writing manifest file 'wxReply.egg-info/SOURCES.txt' running check creating wxReply-1.0.6 creating wxReply-1.0.6/wxReply creating wxReply-1.0.6/wxReply.egg-info copying files to wxReply-1.0.6... copying README.rst -> wxReply-1.0.6 copying setup.py -> wxReply-1.0.6 copying wxReply/__init__.py -> wxReply-1.0.6/wxReply copying wxReply/wxReply.py -> wxReply-1.0.6/wxReply copying wxReply.egg-info/PKG-INFO -> wxReply-1.0.6/wxReply.egg-info copying wxReply.egg-info/SOURCES.txt -> wxReply-1.0.6/wxReply.egg-info copying wxReply.egg-info/dependency_links.txt -> wxReply-1.0.6/wxReply.egg-info copying wxReply.egg-info/requires.txt -> wxReply-1.0.6/wxReply.egg-info copying wxReply.egg-info/top_level.txt -> wxReply-1.0.6/wxReply.egg-info copying wxReply.egg-info/zip-safe -> wxReply-1.0.6/wxReply.egg-info Writing wxReply-1.0.6/setup.cfg Creating tar archive removing 'wxReply-1.0.6' (and everything under it) running register Registering wxReply to https://upload.pypi.org/legacy/ Server response (410): Project pre-registration is no longer required or supported, so continue directly to uploading files. running upload Submitting dist/wxReply-1.0.6.tar.gz to https://upload.pypi.org/legacy/ Server response (200): OK
測(cè)試
注意:
如果本地修改過(guò)pip的源, 執(zhí)行search 或 install 時(shí), 請(qǐng)修改為官方源或臨時(shí)使用官方源(如: pip3 install wxReply -i https://pypi.python.org/simple), 否則有可能提示包不存在, 原因是官方的pip源尚未同步到其他的源, 等一段時(shí)間就可以了(具體多久沒(méi)研究過(guò))
測(cè)試是否上傳成功
tianshl@tianshl wechat $ pip3 search wxReply wxReply (1.0.6) - wxReply
測(cè)試能否安裝成功
tianshl@tianshl ~ $ pip3 install wxReply Collecting wxReply Downloading wxReply-1.0.6.tar.gz Requirement already satisfied: requests in /usr/local/lib/python3.6/site-packages (from wxReply) Requirement already satisfied: itchat in /usr/local/lib/python3.6/site-packages (from wxReply) Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python3.6/site-packages (from requests->wxReply) Requirement already satisfied: urllib3<1.23,>=1.21.1 in /usr/local/lib/python3.6/site-packages (from requests->wxReply) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/site-packages (from requests->wxReply) Requirement already satisfied: idna<2.7,>=2.5 in /usr/local/lib/python3.6/site-packages (from requests->wxReply) Requirement already satisfied: pyqrcode in /usr/local/lib/python3.6/site-packages (from itchat->wxReply) Requirement already satisfied: pypng in /usr/local/lib/python3.6/site-packages (from itchat->wxReply) Building wheels for collected packages: wxReply Running setup.py bdist_wheel for wxReply ... done Stored in directory: /Users/tianshl/Library/Caches/pip/wheels/49/26/9e/883fd73919e7c2cce794b0acc212216441ced601d6062e2941 Successfully built wxReply Installing collected packages: wxReply Successfully installed wxReply-1.0.6
測(cè)試能否正常引入
tianshl@tianshl ~ $ python3 Python 3.6.2 (default, Jul 17 2017, 16:44:45) [GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import wxReply >>>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
利用Python實(shí)現(xiàn)斐波那契數(shù)列的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于如何利用Python實(shí)現(xiàn)斐波那契數(shù)列的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07python解釋器pycharm安裝及環(huán)境變量配置教程圖文詳解
這篇文章主要介紹了python解釋器pycharm安裝及環(huán)境變量配置教程圖文詳解,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02帶你徹底搞懂python操作mysql數(shù)據(jù)庫(kù)(cursor游標(biāo)講解)
這篇文章主要介紹了帶你徹底搞懂python操作mysql數(shù)據(jù)庫(kù)(cursor游標(biāo)講解),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01python入門語(yǔ)句基礎(chǔ)之if語(yǔ)句、while語(yǔ)句
本文介紹了python入門語(yǔ)句基礎(chǔ)之if語(yǔ)句、while語(yǔ)句,if?語(yǔ)句讓你能夠檢查程序的當(dāng)前狀態(tài),并據(jù)此采取相應(yīng)的措施,而for?循環(huán)用于針對(duì)集合中的每個(gè)元素都一個(gè)代碼塊,而?while?循環(huán)不斷地運(yùn)行,直到指定的條件不滿足為止,本文通過(guò)示例代碼詳解介紹,需要的朋友參考下吧2022-04-04Python使用crontab模塊設(shè)置和清除定時(shí)任務(wù)操作詳解
這篇文章主要介紹了Python使用crontab模塊設(shè)置和清除定時(shí)任務(wù)操作,結(jié)合實(shí)例形式分析了centos7平臺(tái)上Python安裝、python-crontab模塊安裝,以及基于python-crontab模塊的定時(shí)任務(wù)相關(guān)操作技巧,需要的朋友可以參考下2019-04-04安裝python時(shí)MySQLdb報(bào)錯(cuò)的問(wèn)題描述及解決方法
這篇文章主要介紹了安裝python時(shí)MySQLdb報(bào)錯(cuò)的問(wèn)題描述及解決方法,需要的朋友可以參考下2018-03-03python通過(guò)微信發(fā)送郵件實(shí)現(xiàn)電腦關(guān)機(jī)
這篇文章主要為大家詳細(xì)介紹了python通過(guò)微信發(fā)送郵件實(shí)現(xiàn)電腦關(guān)機(jī),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06