使用说明Document
注册
基本信息
可以注册的地址有两个,如下。
- PyPi注册地址
- TestPyPi注册地址
本文以注册PyPi为例,注册TestPyPi的步骤相同。
填写信息
-
进入注册地址填写信息注册账号
-
邮件确认:点击确认后Pypi会向注册邮箱发送邮件,通过邮件确认注册
双重校验
-
选择Account Setting
-
滑动到双重校验部分:Two factor authentication (2FA)
-
点击
Generate recovery codes
按钮,按要求走下去。会得到多个recovery code
,建议保存下来。这里需要复制其中一个recovery code
,下一步就要校验
-
下面就进入了双重校验设置页面。PyPi提供了通过应用校验和通过设备校验两种方法。
-
笔者使用通过应用校验的方法,即
Add 2FA with authentication application
。使用的软件是微软推出的Authenticator
-
打开
Add 2FA with authentication application
,会出现一个二维码 -
打开
Authenticator
后点击+号按钮,选择账户类型,选择扫描QR码。扫描上一步出现的二维码。 -
扫描完成后,
Authenticator
主界面上会多出一个元素,点击进去看到一个六位码,将其作为校验码填入双重校验设置页面,并Set up Application
-
至此,已完成双重校验设置。
打包
官网提供了一个打包Python工程的文档,下面成为文档
本文按照文档的内容创建并上传Python工程到PyPi
创建远程仓库
创建一个远程仓库。笔者选择了Gitee
平台创建了一个开源项目。刚创建好的目录如下
py-pi-study /
├── .gitee/
├── .gitignore/
├── LICENSE
├── README.en.md
└── README.md
按照结构要求创建
文档
要求的结构目录如下
packaging_tutorial/
├── LICENSE
├── pyproject.toml
├── README.md
├── src/
│ └── example_package_YOUR_USERNAME_HERE/
│ ├── __init__.py
│ └── example.py
└── tests/
我们向工程中添加一些文件,使得目录结构如下:
py-pi-study/
├── .git/
├── .gitee/
├── .gitignore/
├── LICENSE
├── README.en.md
├── README.md
├── src/
└── pypistudy_auspiciouschan/
├── __init__.py
└── main.py
└── test/
其中main.py
文件中实现了print_hi
方法,能打印文本到控制台。
pyproject.toml
在工程第一级目录下添加pyproject.toml文件,用于配置。添加后,目录结构如下
py-pi-study/
├── .git/
├── .gitee/
├── .gitignore/
├── LICENSE
├── pyproject.toml
├── README.en.md
├── README.md
├── src/
└── pypistudy_auspiciouschan/
├── __init__.py
└── main.py
└── test/
配置构建后端
文档使用hatchling
来讲解,笔者也使用hatchling
。添加下面内容即可指定使用hatchling
。
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
后面python -m build
的时候可能有报错如下
At least one file selection option must be defined in the `tool.hatch.build.targets.wheel` table, see: https://hatch.pypa.io/latest/config/build/
As an example, if you intend to ship a directory named `foo` that resides within a `src` directory located at the root of your project, you can define the following:
[tool.hatch.build.targets.wheel]
packages = ["src/foo"]
只要按照提示添加代码即可,当前工程的包名是pypistudy_auspiciouschan
。可添加如下内容。
[tool.hatch.build.targets.wheel]
packages = ["src/pypistudy_auspiciouschan"]
配置包信息
包信息包括包名
,版本号
、作者名
、邮箱
、最低Python版本
、远程仓库地址
等
文档给出的示例如下:
[project]
name = "example_package_YOUR_USERNAME_HERE"
version = "0.0.1"
authors = [
{ name="Example Author", email="author@example.com" },
]
description = "A small example package"
readme = "README.md"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
[project.urls]
Homepage = "https://github.com/pypa/sampleproject"
Issues = "https://github.com/pypa/sampleproject/issues"
其中,example_package_YOUR_USERNAME_HERE
在这里就是pypistudy_auspiciouschan
使用说明和证书
文件结构里面的README.md
和LICENSE
分别为使用说明和证书。使用Gitee
创建工程时,已经生成。
如果没有README.md
文件,可以创建桶名文件;如果没有LICENSE
文件,也可以创建出来,然后参考文档
生成。
发布
构建可发布包
- 先切到
pyproject
文件的同级目录下。 - 更新build
python3 -m pip install --upgrade build
- 执行build
python3 -m build
完成后会新生成一个目录,目录中有两个压缩文件
dist/
├── pypistudy_auspiciouschan-0.0.1-py3-none-any.whl
└── pypistudy_auspiciouschan-0.0.1.tar.gz
执行上传包
- 文档中使用
twine
上传包到TestPyPI
。 如果没有twine
,先安装。
python3 -m pip install --upgrade twine
- 上传
dist
目录下面的文件
上传文件需要用到API Token
,在官网的AccountSetting目录下,可以找到API Token
的区域。
如果有,可以直接获取;如果没有则根据提示创建即可。然后运行下面命令。
python3 -m twine upload --repository pypi dist/*
如果想先发布在TestPyPi平台上,也可以运行下面命令。
python3 -m twine upload --repository testpypi dist/*
执行上面命令时,可能需要填写username和tokne,分别填写__token__
和上面创建的API Token
即可。
注意:API Token是pypi-
开头的一串字符串,只会展示一次。退出创建页面后,在Account Settings页面只能看到它的ID。
- 上传成功。
注意:
如果有如下报错,可能是用错了API
ERROR HTTPError: 403 Forbidden from https://test.pypi.org/legacy/
Invalid or non-existent authentication information. See https://test.pypi.org/help/#invalid-auth for more information.
如果有如下报错,可能是pkginfo
不识别的metadata版本
,升级pkginfo
即可
ERROR InvalidDistribution: Metadata is missing required fields: Name, Version.
Make sure the distribution includes the files where those fields are specified, and is using a supported Metadata-Version: 1.0, 1.1, 1.2, 2.0, 2.1, 2.2.
笔者遇到的问题是由1.10.0
以下版本的pkginfo
无法识别Metadata-Version: 2.3
导致的,通过下面命令升级pkginfo
即可
pip install pkginfo==1.10.0
两个构建出的压缩包都记录了Metadata-Version
,查看方法如下
- 解压
pypistudy_auspiciouschan-0.0.1.tar.gz
,打开PKG-INFO
文件。 - 解压
pypistudy_auspiciouschan-0.0.1-py3-none-any.whl
,打开pypistudy_auspiciouschan-0.0.1.disk-info
文件夹下的METADATA
文件。
引用包
上传成功后,从头像进入Your Projects
页面就能看到包列表了,选择一个包,就能看到通过pip install
命令引用包的具体方法了。