未分类

使用PyPI发布自己的轮子

PyPI打包上传全教程

官方文档

准备

目录结构

1
2
3
4
5
6
/example_pkg
/example_pkg
__init__.py
setup.py
LICENSE
README.md

setup.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="example_pkg",
version="0.0.1",
author="Example Author",
author_email="[email protected]",
description="A small example package",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/pypa/sampleproject",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
)

README.md

1
2
3
4
5
# Example Package
This is a simple example package. You can use
[Github-flavored Markdown](https://guides.github.com/features/mastering-markdown/)
to write your content.

LICENSE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Copyright (c) 2018 The Python Packaging Authority
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

PyPI帐号注册

点击这里

打包&上传

安装打包依赖

python -m pip install --user --upgrade setuptools wheel twine

打包

python setup.py sdist bdist_wheel

此时会生成如下目录及文件:

1
2
3
dist/
example_pkg-0.0.1-py3-none-any.whl
example_pkg-0.0.1.tar.gz

上传

twine upload --repository-url https://test.pypi.org/legacy/ dist/*

会提示你输入用户名密码,即最上方你注册的PyPI的帐户

成功

会提示上传成功,这时去帐户里查看就可以看到你上传的轮子了。

可能碰到的异常

403

Upload failed (403): Invalid or non-existent authentication information.

错误的用户验证信息,你需要创建一个用户验证文件 ~/.pypirc。请参阅上文。

Upload failed (403): You are not allowed to edit ‘xxx’ package information

你需要先注册你的包才可以开始上传,运行注册命令:python setup.py register

401

Server response (401): Incomplete registration; check your email

你的PyPI账户还没完成邮箱验证,你需要去注册邮箱找到一封验证邮件完成验证后再重试失败的步骤。

400

Server response (400): Invalid classifier “Topic :: Software Development :: Utilities”

你的setup.py文件中的classifier信息有误,请按官网的正确分类书写classifier.

Upload failed (400): File already exists

文件已经存在了,你每一次上次都应该更新版本号。

410

HTTPError: 410 Client Error: Project pre-registration is no longer required or supported, so continue directly to uploading files. for url: https://upload.pypi.org/legacy/

以前的时候需要注册后再上传,现在不再需要或不支持项目预注册,因此请继续直接上传文件。

No dist

error: No dist file created in earlier command

你还没打包就开始了上传命令,建议打包和上传的操作放在一起做,比如:

499

python setup sdist upload

error: Upload failed (499): Client Disconnected

这应该是网络问题,多重试几次。