PyPI教程

0x01、简介

Wiki PyPI

The Python Package Index, abbreviated as PyPI and also known as the Cheese Shop (a reference to the Monty Python's Flying Circus sketch Cheese Shop),[1][2] is the official third-party software repository for Python.[3] It is analogous to CPAN, the repository for Perl.[4] Some package managers, including pip, use PyPI as the default source for packages and their dependencies.[5][6] Over 113,000 Python packages can be accessed through PyPI.[7]

就是说PyPI(Python Package Index)是Python官方的第三方库,所有人都可以下载或上传Python库到PyPI。

0x02、流程

1、注册账号

想要上传自己的Python包没PyPI账号怎么行!官网地址

2、项目架构

1、项目环境:

  • python3
  • pip
  • setuptools
  • wheel
  • twine

2、项目文件

如果想上传一个名为 "debug_world" 的Python包,那么她的项目文件如下:

-- packaging_tutorial      # 项目名字
    -- debug_world         # 包名字
        -- __init__.py     # 一定要有init文件
        -- print_str.py    # 功能实现
    -- __init__.py         # 一定要有init文件
    -- README.md           # 一般记录具体使用方法
    -- setup.py            # 打包程序
复制代码

print_str.py文件实现三个功能,代码如下:

def debug_world():
    print('世界很美好,我去如此暴躁,这样不好不好')


def hello_world():
    print('Hello, world')


def hello_python():
    print('Hello Python\nPython 大法好')
复制代码

setup.py文件代码如下:

import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="debug-world",                                     # 包的分发名称,使用字母、数字、_、-
    version="0.0.1",                                        # 版本号, 版本号规范:https://www.python.org/dev/peps/pep-0440/
    author="liheyou",                                       # 作者名字
    author_email="author@example.com",                      # 作者邮箱
    description="PyPI Tutorial",                            # 包的简介描述
    long_description=long_description,                      # 包的详细介绍(一般通过加载README.md)
    long_description_content_type="text/markdown",          # 和上条命令配合使用,声明加载的是markdown文件
    url="https://github.com/",                              # 项目开源地址,我这里写的是同性交友官网,大家可以写自己真实的开源网址
    packages=setuptools.find_packages(),                    # 如果项目由多个文件组成,我们可以使用find_packages()自动发现所有包和子包,而不是手动列出每个包,在这种情况下,包列表将是example_pkg
    classifiers=[                                           # 关于包的其他元数据(metadata)
        "Programming Language :: Python :: 3",              # 该软件包仅与Python3兼容
        "License :: OSI Approved :: MIT License",           # 根据MIT许可证开源
        "Operating System :: OS Independent",               # 与操作系统无关
    ],
)

# 这是最简单的配置
# 有关详细信息,请参阅(https://packaging.python.org/guides/distributing-packages-using-setuptools/)
复制代码

这里是最简单的配置,有关详细信息,请参阅打包和分发项目

3、本地打包

确保pip,setuptools和wheel是最新的。
虽然pip就能够保证安装成功,但是最新的setuptools和wheel对安装有益无害。

While pip alone is sufficient to install from pre-built binary archives, up to date copies of the setuptools and wheel projects are useful to ensure you can also install from source archives:

python -m pip install --upgrade pip setuptools wheel
复制代码

进入setup.py同级目录,然后运行打包程序

# 运行setup.py
python setup.py sdist
# 或者
python setup.py sdist bdist_wheel
复制代码

此时项目会出现两个新文件:

4、上传PyPI

运行把Python包上传到PyPI的命令

pip install twine     # 如果已经安装twine,跳过次步骤
python -m twine upload dist/*

# 接着会让你输入PyPI用户名和密码,注意不是邮箱和密码
# 出现下面信息说明成功,如有错误信息,检查setup.py配置信息
Uploading distributions to https://upload.pypi.org/legacy/
Uploading debug-world-0.0.1.tar.gz 
100%█████████████████████████| 5.56k/5.56k [00:01<00:00, 3.98kB/s]

如果不想每次上传都输入账号和密码,可以创建用户验证文件 ** ~/.pypirc**
# 而且此方法不安全,容易泄露密码, 因为密码是明文
[distutils]
index-servers =
    pypi

[pypi]
repository: https://upload.pypi.org/legacy/
username: <username>
password: <password>
复制代码

然后就可以去PyPI官网查看你的包是否成功上传了

3、验证

PyPI推荐通过pip使用Python包

pip install debug-world
复制代码

新建验证文件 verify_pypi.py

from debug_world import print_str

print(print_str.hello_world())
print(print_str.debug_world())
print(print_str.hello_python())
复制代码

查看运行结果,说明成功了

Hello, world
世界很美好,我去如此暴躁,这样不好不好
Hello Python	Python 大法好
复制代码

4、删除版本

当想要删除某一版本的时候,只需在官网项目管理页面进行删除即可。 输入相对应的版本号。

0x03、注意事项

  • 包名一定是别人没用过的
  • 项目文件一定要有** init.py**
  • 运行setup.py文件一定要同级目录
  • 在上传PyPI的是时候输入的是用户名和密码,不是邮箱和密码
  • 上传之后需要等一段时间,才能下载最新版本的包
  • 更改包的时候一定要修改版本号
  • pip 按照版本号安装,==前后没有空格

0x04、报错

HTTPError: 400 Client Error: File already exists: 版本号错误
HTTPError: 403 Client Error: Invalid or non-existent authentication information: 密码错误
error: invalid command 'bdist_wheel':

# 升级pip的安装工具setuptools
sudo pip install --upgrade setuptools
# 然后更新包
python -m pip install --upgrade pip setuptools wheel
复制代码

0x05、参考

Wiki PyPI
Python Packaging User Guide

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值