使用poetry工具设置pyproject.toml将python项目进行打包分发和上传到pypi。
具体步骤如下: 1. 安装 poetry 工具:可以使用 pip 安装,命令为 pip install poetry。
- 在项目根目录下创建 pyproject.toml 文件,定义项目依赖和版本等信息。例如:
[tool.poetry]
name = "FastText_Shop"
version = "0.0.3"
description = "FastText_Shop是一个基于FastText和结巴分词的短文本分类工具,特点是高效易用,同时支持中文和英文语料。基本使用方法、灵感来自TextGrocery,并且和TextGrocery基本相同。"
authors = ["ranvane <[email protected]>"]
license = "MIT"
readme = "README.md"
homepage = "https://gitee.com/ranvane/fasttext_shop"
repository = "https://gitee.com/ranvane/fasttext_shop"
[tool.poetry.dependencies]
python = "^3.7"
fasttext = "^0.9.2"
jieba = "^0.42.1"
loguru = "^0.7.2"
numpy = "^1.20.1"
[tool.poetry.dev-dependencies]
pytest = "^5.2"
[build-system]
requires = ["poetry>=1.0.0"]
build-backend = "poetry.masonry.api"
#设置poetry包管理工具的自定义pypi镜像源配置
#[[tool.poetry.source]]
#name = "aliyun"
#url = "http://mirrors.aliyun.com/pypi/simple"
#default = true
-
在终端进入项目根目录,执行 poetry install 命令,安装项目依赖。
-
使用 poetry build 命令进行打包,例如 poetry build --format=wheel 生成 wheel 文件格式。
-
上传前需要在 PyPI 上注册账号并认证,具体方法可以参考 PyPI 官方文档,这里进行简单说明: PyPI默认开启Two factor authentication (2FA),上传方式和以往网上常见资料有所不同, 在PyPI 的
Account settings
中找到API tokens
,点击Add API token
,填上Token name
和范围Scope
,点击Create token
,复制保存好取得的token。 - 使用
poetry publish -u __token__ -p pypi-xxxxxxxxxxxx
命令进行上传,xxxxxxxxxxxx即为`Token。 - 注意,在执行 poetry build 命令之前,应该先执行 poetry check 命令检查项目的依赖、版本、LICENSE 等信息是否符合要求。另外,执行 poetry install --no-dev 命令可以安装只包含生产依赖的项目版本。