当前位置: 首页 > backend >正文

使用pdm+uv替换poetry

用了好几年poetry了,各方面都还挺满意,就是lock实在太慢;

已经试用pdm+uv一段时间了,确实是快,也基本能覆盖poetry的功能。

至于为什么用pdm+uv,而不是只用uv,原因很多,有兴趣的可以去看这个https://github.com/pdm-project/pdm/discussions/3388

1. 安装pdm、uv并换源

pip install --user --upgrade pipx
pipx install pdm uv
pdm config pypi.url https://mirrors.cloud.tencent.com/pypi/simple/
mkdir -p ~/.config/uv && echo -e '[[index]]\nurl="https://pypi.tuna.tsinghua.edu.cn/simple/"\ndefault = true' > ~/.config/uv/uv.toml

 2. 配置pdm全局使用uv

pdm config use_uv true

注:对于个别不想使用uv加速的项目,可以单独这样配置:pdm config --local use_uv true

3. 迁移poetry的pyproject.toml到pdm

- Old

[project]
name = "asynctor"
description = "Async functions to compare with anyio and asyncio, and toolkit to read excel with async/await."
authors = [{ name = "Waket Zheng", email = "waketzheng@gmail.com" }]
readme = "README.md"
license = { text = "MIT" }
requires-python = ">=3.9"
dynamic = [ "version" ]
dependencies = ["anyio>=3.7.1","eval-type-backport (>=0.2.2,<1.0.0); python_version < '3.10'",
]
# requires by anyio:
#    "exceptiongroup >= 1.0.2; python_version < '3.11'",
#    "typing_extensions >= 4.1; python_version < '3.11'",[project.optional-dependencies]
xls = ["pandas>=2.2.0", "openpyxl>=3.1.0"]
fastapi = ["redis", "fastapi>=0.115.0", "httpx>=0.28.1", "asgi-lifespan>=2.1.0"]
redis = ["redis>=5.0.0"][project.urls]
homepage = "https://github.com/waketzheng/asynctor"
repository = "https://github.com/waketzheng/asynctor.git"
"Bug Tracker" = "https://github.com/waketzheng/asynctor/issues"[tool.poetry]
version = "0"  # Managed by poetry-plugin-version[tool.poetry.group.dev.dependencies]
fast-dev-cli = "^0.15.1"
types-redis = "^4.6.0.20241004"
pandas-stubs = {version=">=2.2.0", python="^3.10"}
bandit = "^1.8.3"
pytest-mock = "^3.14.1"
fastapi-cdn-host = "^0.9.1"
uvicorn = "^0.34.3"[tool.poetry.group.ci.dependencies]
coveralls = {git = "https://github.com/waketzheng/coveralls-python", rev = "4.1.1", python="^3.9"}[tool.ruff]
line-length = 100[tool.ruff.lint]
extend-select = ["E",  # pycodestyle errors"W",  # pycodestyle warnings"F",  # pyflakes"I",  # isort"B",  # flake8-bugbear"C4", # flake8-comprehensions"UP", # https://docs.astral.sh/ruff/rules/#pyupgrade-up
][tool.ruff.lint.per-file-ignores]
"test_*.py" = ["E501"]
"scripts/test.py" = ["E501"]
"scripts/*.py" = ["UP009","UP032"][tool.mypy]
pretty = true
python_version = "3.9"
ignore_missing_imports = true
check_untyped_defs = true[tool.coverage.report]
omit = ["*/tests/*", "test_*"]
exclude_lines = ["pragma: no cover","@overload",'if __name__ == "__main__":',"if TYPE_CHECKING:",
][build-system]
requires = ["poetry-plugin-version"]
build-backend = "poetry_plugin_version.api"

- Diff

-[tool.poetry]
-version = "0"  # Managed by poetry-plugin-version
+[tool.pdm]
+version = {source="file", path="asynctor/__init__.py"}-[tool.poetry.group.dev.dependencies]
-fast-dev-cli = "^0.15.0"
-types-redis = "^4.6.0.20241004"
-pandas-stubs = {version=">=2.2.0", python="^3.10"}
-bandit = "^1.8.3"
-pytest-mock = "^3.14.0"
-fastapi-cdn-host = "^0.9.1"
-uvicorn = "^0.34.2"
-
-[tool.poetry.group.ci.dependencies]
-coveralls = {git = "https://github.com/waketzheng/coveralls-python", rev = "4.1.1", python="^3.9"}
+[dependency-groups]
+dev = [
+    "fast-dev-cli>=0.15.1",
+    "types-redis>=4.6.0.20241004",
+    "pandas-stubs",
+    "bandit>=1.8.3",
+    "pytest-mock>=3.14.1",
+    "fastapi-cdn-host>=0.9.1",
+    "uvicorn>=0.34.3",
+]
+ci = ["coveralls @ git+https://github.com/waketzheng/coveralls-python@4.1.1"][build-system]
-requires = ["poetry-plugin-version"]
-build-backend = "poetry_plugin_version.api"
+requires = ["pdm-backend"]
+build-backend = "pdm.backend"

- New

[project]
name = "asynctor"
description = "Async functions to compare with anyio and asyncio, and toolkit to read excel with async/await."
authors = [{ name = "Waket Zheng", email = "waketzheng@gmail.com" }]
readme = "README.md"
license = { text = "MIT" }
requires-python = ">=3.9"
dynamic = [ "version" ]
dependencies = ["anyio>=3.7.1","eval-type-backport (>=0.2.2,<1.0.0); python_version < '3.10'",
]
# requires by anyio:
#    "exceptiongroup >= 1.0.2; python_version < '3.11'",
#    "typing_extensions >= 4.1; python_version < '3.11'",[project.optional-dependencies]
xls = ["pandas>=2.2.0", "openpyxl>=3.1.0"]
fastapi = ["redis", "fastapi>=0.115.0", "httpx>=0.28.1", "asgi-lifespan>=2.1.0"]
redis = ["redis>=5.0.0"][project.urls]
homepage = "https://github.com/waketzheng/asynctor"
repository = "https://github.com/waketzheng/asynctor.git"
"Bug Tracker" = "https://github.com/waketzheng/asynctor/issues"[tool.pdm]
version = {source="file", path="asynctor/__init__.py"}[dependency-groups]
dev = ["fast-dev-cli>=0.15.1","types-redis>=4.6.0.20241004","pandas-stubs","bandit>=1.8.3","pytest-mock>=3.14.1","fastapi-cdn-host>=0.9.1","uvicorn>=0.34.3",
]
ci = ["coveralls @ git+https://github.com/waketzheng/coveralls-python@4.1.1"][tool.ruff]
line-length = 100[tool.ruff.lint]
extend-select = ["E",  # pycodestyle errors"W",  # pycodestyle warnings"F",  # pyflakes"I",  # isort"B",  # flake8-bugbear"C4", # flake8-comprehensions"UP", # https://docs.astral.sh/ruff/rules/#pyupgrade-up
][tool.ruff.lint.per-file-ignores]
"test_*.py" = ["E501"]
"scripts/test.py" = ["E501"]
"scripts/*.py" = ["UP009","UP032"][tool.mypy]
pretty = true
python_version = "3.9"
ignore_missing_imports = true
check_untyped_defs = true[tool.coverage.report]
omit = ["*/tests/*", "test_*"]
exclude_lines = ["pragma: no cover","@overload",'if __name__ == "__main__":',"if TYPE_CHECKING:",
][build-system]
requires = ["pdm-backend"]
build-backend = "pdm.backend"

4. 修改Makefile为使用pdm

- diff

--- a/Makefile
+++ b/Makefile
@@ -11,17 +11,17 @@ help:@echo  "    lint    Auto-formats the code and check type hints"up:
-       poetry run fast upgrade
+       pdm run fast upgradedeps:
-       poetry install --all-extras
+       pdm install --verbose --group :all --without=ci --frozen_check:./scripts/check.pycheck: deps _build _check_lint:
-       poetry run fast lint
+       pdm run fast lint
lint: deps _build _lint_test:
@@ -33,7 +33,8 @@ _style:style: deps _style_build:
-       poetry build --clean
+       rm -fR dist/
+       pdm buildbuild: deps _buildci: check _build _test

5. 更新CI文件

--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -38,49 +38,26 @@ jobs:with:python-version: ${{ matrix.python-version }}allow-prereleases: true
-      - name: Load cached Poetry installation
-        id: cached-poetry
-        uses: actions/cache@v4
+      - uses: actions/cache@v4
+        id: cachewith:
-          path: ~/.local  # the path depends on the OS
-          key: poetry-0  # increment to reset cache
-      - name: Install Poetry
-        uses: snok/install-poetry@v1
+          path: ~/.cache/pip
+          key: ${{ runner.os }}-python-${{ env.pythonLocation }}-${{ hashFiles('**/pdm.lock') }}
+      - name: Set up PDM
+        uses: pdm-project/setup-pdm@v4with:
-          virtualenvs-in-project: true
-          plugins: poetry-plugin-version
-      - name: Load cached venv
-        id: cached-poetry-dependencies
-        uses: actions/cache@v4
-        with:
-          path: .venv
-          key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('
**/poetry.lock') }}
-      #----------------------------------------------
-      # install dependencies if cache does not exist
-      #----------------------------------------------
-      - name: Install dependencies
-        if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
-        run: poetry install --no-interaction --no-root --all-extras --all-groups
-        env:
-          PYO3_USE_ABI3_FORWARD_COMPATIBILITY: 1
-      #----------------------------------------------
-      # install your root project, if package-mode is true
-      #----------------------------------------------
-      - name: Install library
-        run: poetry install --no-interaction
+          python-version: ${{ matrix.python-version }}- uses: astral-sh/ruff-action@v3
-      - name: Check code style and Type Hint
-        run: ./scripts/check.py
-      - name: build
-        run: poetry build
-      - name: Test with pytest
-        run: poetry run fast test
+      - name: Check code style and Type Hint then Test with pytest
+        run: make cienv:# The hostname used to communicate with the Redis service containerREDIS_HOST: localhost
+      - name: Install library
+        run: pdm sync -d -G :all- name: Upload Coveragerun: |
-          poetry run coveralls --service=github
+          pdm run coveralls --service=githubenv:GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

http://www.xdnf.cn/news/10664.html

相关文章:

  • 20250602在荣品的PRO-RK3566开发板的Android13下的uboot启动阶段配置BOOTDELAY为10s
  • 安装 Hugo
  • Flask + Celery 应用
  • 【C++】23. unordered_map和unordered_set的使用
  • Qt OpenGL 光照实现
  • JAVA-springboot整合Mybatis
  • Linux 系统 Docker Compose 安装
  • Spring Cloud 2025 正式发布啦
  • Vue基础(12)_Vue.js循环语句用法:列表渲染
  • 超声波测距三大算法实测对比
  • 字节跳动开源图标库:2000+图标一键换肤的魔法
  • 深度剖析:AI 建站的现状、局限与未来展望-AI编程建站实战系列预告优雅草卓伊凡
  • 5.RV1126-OPENCV 图形计算面积
  • Ubuntu22.04 安装 CUDA12.8
  • SQL Transactions(事务)、隔离机制
  • Python发送天气预报到企业微信解决方案
  • A. We Need the Zero
  • LangGraph framework
  • 《Linux 包管理实战手册:RPM 精准操作与 YUM 自动化部署从入门到精通》
  • 软件测评师 第9章 基于质量特性的测试与评价 笔记
  • SQL 中的 `CASE WHEN` 如何使用?
  • CUDA与OpenGL混合编程图形渲染
  • 【Python 算法零基础 4.排序 ⑦ 桶排序】
  • 【算法训练营Day05】哈希表part1
  • 《软件项目管理》第二章(项目准备与启动)期末周复习总结笔记
  • uniapp中view标签使用范围
  • Kotlin 中companion object {} 什么时候触发
  • C语言:内存函数
  • 前端面试高频问题通关指南--通用性问题
  • Python----目标检测(使用YOLO 模型进行线程安全推理和流媒体源)