Python庫如何打包到PyPI
Python庫打包到PyPI
打開pypi官網(wǎng), 并注冊賬號
創(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. 要打包的代碼必須是一個包(package)
2. 代碼是開源的
包(package) 創(chuàng)建包
右鍵 / New / Python Package / 輸入包名 / OK
創(chuàng)建成功后,查看目錄結(jié)構(gòu)
tianshl@tianshl wechat $ tree
.
└── wxReply
└── __init__.py實際上就是文件夾中包含__init__.py文件
編寫要打包的源代碼
tianshl@tianshl wechat $ tree
.
└── wxReply
├── __init__.py
└── wxReply.py
1 directory, 2 files
創(chuàng)建README.rst和setup.py文件
- README.rst為說明文檔
- setup.py為安裝腳本 (核心)
此時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', # 版本號
description='wxReply', # 簡單描述
long_description=long_description, # 詳細描述
classifiers=[
'License :: OSI Approved :: MIT License',
'Programming Language :: Python',
'Intended Audience :: Developers',
'Operating System :: OS Independent',
],
keywords='wechat robot weixin wxReply', # 關鍵字
author='tianshl', # 作者
author_email='xiyuan91@126.com', # 郵箱
url='https://my.oschina.net/tianshl/blog', # 包含包的項目地址
license='MIT', # 授權(quán)方式
packages=find_packages(), # 包列表
install_requires=['requests', 'itchat'],
include_package_data=True,
zip_safe=True,
)
校驗 (python setup.py check)
tianshl@tianshl wxReply $ python3 setup.py check running check
注: running check 表示沒問題, 其他情況對照提示修改即可
打包 (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)
此時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
測試
注意:
如果本地修改過pip的源, 執(zhí)行search 或 install 時, 請修改為官方源或臨時使用官方源(如: pip3 install wxReply -i https://pypi.python.org/simple), 否則有可能提示包不存在, 原因是官方的pip源尚未同步到其他的源, 等一段時間就可以了(具體多久沒研究過)
測試是否上傳成功
tianshl@tianshl wechat $ pip3 search wxReply wxReply (1.0.6) - wxReply
測試能否安裝成功
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
測試能否正常引入
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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python如何使用print()函數(shù)輸出格式化字符串
Python中內(nèi)置的%操作符和format函數(shù),都可以用于格式化字符串,下面這篇文章主要給大家介紹了關于Python如何使用print()函數(shù)輸出格式化字符串的相關資料,需要的朋友可以參考下2021-08-08
Jupyter Lab設置切換虛擬環(huán)境的實現(xiàn)步驟
本文主要介紹了Jupyter Lab設置切換虛擬環(huán)境的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-02-02
詳解Python數(shù)據(jù)可視化編程 - 詞云生成并保存(jieba+WordCloud)
這篇文章主要介紹了Python數(shù)據(jù)可視化編程 - 詞云生成并保存(jieba+WordCloud),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-03-03
Python調(diào)用ollama本地大模型進行批量識別PDF
現(xiàn)在市場上有很多PDF文件的識別,然而隨著AI的興起,本地大模型的部署,這些成為一種很方便的方法,本文我們就來看看Python如何調(diào)用ollama本地大模型進行PDF相關操作吧2025-03-03
Python實現(xiàn)12306火車票搶票系統(tǒng)
這篇文章主要介紹了Python實現(xiàn)12306火車票搶票系統(tǒng),本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-07-07
總結(jié)用Pdb庫調(diào)試Python的方式及常用的命令
大家都知道Python是自帶Pdb庫,使用Pdb調(diào)試Python程序還是很方便的。但是遠程調(diào)試、多線程,Pdb是搞不定的,下面一起來看看用Pdb庫調(diào)試Python的方式及常用的命令。2016-08-08
python使用requests模塊實現(xiàn)爬取電影天堂最新電影信息
這篇文章主要介紹了python使用requests模塊實現(xiàn)爬取電影天堂最新電影信息,本文通過實例代碼給大家介紹了str/list/tuple三者之間怎么相互轉(zhuǎn)換,需要的朋友可以參考下2019-04-04

