发布 Python 包到 Pypi

发布时间: 更新时间: 总字数:545 阅读时间:2m 作者: 分享 复制网址

学会如何制作 python 包后,如果你觉得自己开发的模块需要共享给其他人,可以把 Python 包发布到 Pypi(Python Package Index)。Pypi 类似于 Java 的 maven 仓库。

注册账号

到下面两个地址注册账户:

配置

分别获取 PyPI token 参考 https://pypi.org/help/#apitoken

创建 ~/.pypirc 文件,此文件中配置 PyPI 访问地址和账号。配置示例:

[server-login]
  username = xiexianbin
  password = password

[pypi]
  username = __token__  # 必须配置为 __token__,参考 https://pypi.org/help/#invalid-auth
  password = <user password>  # 官方说要使用 token,实际使用密码成功

通过 setup.py 上传

Pecan 使用介绍 为例,修改 setup.py

setup(
    ...
    author='xiexianbin',
    author_email='me@xiexianbin.cn',
    url='https://www.xiexianbin.cn',
    ...

注册项目

到项目根目录下,执行如下命令进行项目信息注册:

$ python3 setup.py check
$ python3 setup.py register
running register
running check
We need to know who you are, so please choose either:
 1. use your existing login,
 2. register as a new user,
 3. have the server generate a new password for you (and email it to you), or
 4. quit
Your selection [default 1]:
1
Username: xiexianbin
Password:
Registering test_pecan to https://upload.pypi.org/legacy/
Server response (410): Project pre-registration is no longer required or supported, upload your files instead.

上传源码

$ python3 setup.py sdist upload

通过 twine 上传

twine 是一个专门用于与 pypi 进行交互的工具。

安装

pip3 install twine

注册项目

到项目根目录下,执行如下命令进行项目信息注册:

twine register dist/xxx.whl

检查

twine check dist/*

上传源码

twine upload dist/*

至此,源码包已经上传完毕。

示例

FAQ

Invalid or non-existent authentication

$ python3 setup.py sdist upload
...
running upload
Submitting dist/xxx-0.1.tar.gz to https://upload.pypi.org/legacy/
Upload failed (403): Invalid or non-existent authentication information. See https://pypi.org/help/#invalid-auth for more information.
error: Upload failed (403): Invalid or non-existent authentication information. See https://pypi.org/help/#invalid-auth for more information.

修复方式:

~/.pypirc 文件,添加如下配置:

[server-login]
  username = xiexianbin
  password = password

Invalid value for blake2_256_digest

$ python3 setup.py sdist upload
...
running upload
Submitting dist/test_pecan-0.1.tar.gz to https://upload.pypi.org/legacy/
Upload failed (400): Invalid value for blake2_256_digest. Error: Use a valid, hex-encoded, BLAKE2 message digest.
error: Upload failed (400): Invalid value for blake2_256_digest. Error: Use a valid, hex-encoded, BLAKE2 message digest.

参考

  1. https://pypi.org/
  2. https://packaging.python.org/en/latest/tutorials/packaging-projects/
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数