python-pyproject.toml打包

1. pyproject.toml 说明

pyproject.toml 是目前官方推荐的打包方式。

官网写的比较抽象。
这里记录下一个简单的使用方法。

2. 包的文件夹结构

建议的包文件结构如下,假设要创建一个名为snake的包:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
snake_proj
├──src/
└──snake/
├──__init__.py
├──AA.py
├──BB.py
└──snake_data/
├──xx.txt
└──yy.txt
├──tests/
└──snake/
├──.gitignore
├──LICENSE
├──pyproject.toml
└──README.md

3. 编写 pyproject.toml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[project]
name = 'snake'
version = '0.0.1'

description = "snake package"

authors = [{ name = "123", email = "abc@xyz.com" }]

[build-system]
requires = ["setuptools >= 61.0"]
build-backend = "setuptools.build_meta"

[tool.setuptools.package-data]
"snake_data" = ["*.txt"]

4. 打包

1
2
3
4
5
6
7
8
# 安装 build 工具
python -m pip install build

# 打包
python -m build -w -v

# 安装 whl
python -m pip install dist\snake-0.0.1-py3-none-any.whl --force-reinstall