Python库如何打包到PyPI

 更新时间:2023年11月29日 08:36:28   作者:tian_shl  
这篇文章主要介绍了Python库如何打包到PyPI问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

Python库打包到PyPI

打开pypi官网, 并注册账号

https://pypi.python.org/

创建并编辑.pypirc (注: 家目录下创建)

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) 创建包

右键 / New / Python Package / 输入包名 / OK

创建成功后,查看目录结构

tianshl@tianshl wechat $ tree
.
└── wxReply
    └── __init__.py

实际上就是文件夹中包含__init__.py文件 

编写要打包的源代码

tianshl@tianshl wechat $ tree
.
└── wxReply
    ├── __init__.py
    └── wxReply.py

1 directory, 2 files

创建README.rst和setup.py文件

  • README.rst为说明文档
  • setup.py为安装脚本 (核心)

此时wechat包的目录结构

tianshl@tianshl wechat $ tree
.
├── README.rst
├── setup.py
└── wxReply
    ├── __init__.py
    └── wxReply.py

1 directory, 4 files

编辑setup.py内容

# -*- 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',                                  # 授权方式
    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包的目录结构

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的源, 执行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
>>> 

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • python实现五子棋算法

    python实现五子棋算法

    这篇文章主要为大家详细介绍了python实现五子棋算法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • Python学习之直方图均衡化原理详解

    Python学习之直方图均衡化原理详解

    直方图均衡化是以累计分布函数为核心,将原始图像灰度直方图从比较集中的某个灰度区间,非线性地映射为在全部灰度范围内的较均匀分布,从而增强对比度。本文将为大家详细讲解直方图均衡化的原理,需要的可以参考一下
    2022-03-03
  • 利用Python实现斐波那契数列的方法实例

    利用Python实现斐波那契数列的方法实例

    这篇文章主要给大家介绍了关于如何利用Python实现斐波那契数列的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用Python具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2020-07-07
  • python解释器pycharm安装及环境变量配置教程图文详解

    python解释器pycharm安装及环境变量配置教程图文详解

    这篇文章主要介绍了python解释器pycharm安装及环境变量配置教程图文详解,本文图文并茂给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-02-02
  • 带你彻底搞懂python操作mysql数据库(cursor游标讲解)

    带你彻底搞懂python操作mysql数据库(cursor游标讲解)

    这篇文章主要介绍了带你彻底搞懂python操作mysql数据库(cursor游标讲解),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • 简单文件操作python 修改文件指定行的方法

    简单文件操作python 修改文件指定行的方法

    使用python进行简略的文件读写
    2013-05-05
  • python入门语句基础之if语句、while语句

    python入门语句基础之if语句、while语句

    本文介绍了python入门语句基础之if语句、while语句,if 语句让你能够检查程序的当前状态,并据此采取相应的措施,而for 循环用于针对集合中的每个元素都一个代码块,而 while 循环不断地运行,直到指定的条件不满足为止,本文通过示例代码详解介绍,需要的朋友参考下吧
    2022-04-04
  • Python使用crontab模块设置和清除定时任务操作详解

    Python使用crontab模块设置和清除定时任务操作详解

    这篇文章主要介绍了Python使用crontab模块设置和清除定时任务操作,结合实例形式分析了centos7平台上Python安装、python-crontab模块安装,以及基于python-crontab模块的定时任务相关操作技巧,需要的朋友可以参考下
    2019-04-04
  • 安装python时MySQLdb报错的问题描述及解决方法

    安装python时MySQLdb报错的问题描述及解决方法

    这篇文章主要介绍了安装python时MySQLdb报错的问题描述及解决方法,需要的朋友可以参考下
    2018-03-03
  • python通过微信发送邮件实现电脑关机

    python通过微信发送邮件实现电脑关机

    这篇文章主要为大家详细介绍了python通过微信发送邮件实现电脑关机,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06

最新评论