Open7

pdm いい感じかもしれない

zztkmzztkm

概要

最近、個人的には rye を推しているが、PDM というツールが気になっており、ちょっと使ってみたのでメモ的なものを書く

zztkmzztkm

Introduction

PDMは説明したように、最新のPEP標準をサポートする最新のPythonパッケージと依存性マネージャです。しかし、単なるパッケージマネージャではありません。様々な面であなたの開発ワークフローを向上させます。
DeepL 翻訳

https://pdm-project.org/latest/

Feature highlights

  • 主に大規模なバイナリディストリビューション向けの、シンプルで高速な依存性解決ツール。
    • 大規模プロジェクトで使っていないので、比較できていないが、速いとは思う(思う
  • PEP 517 ビルドバックエンド。
    • ビルドバックエンドに依存しない
  • PEP 621 プロジェクトメタデータ
    • Python 標準のメタデータをサポートしているの嬉しい
  • 柔軟で強力なプラグインシステム
    • 使ったことなし
  • 多彩なユーザースクリプト
    • これは rye もサポートしており、同じような使用感だった(詳しく見ていないけど設定方法は一緒なのかな?
  • pnpm のようなオプトインの集中インストールキャッシュ。
    • デフォルトではないが、これはうれし
zztkmzztkm

Installation

自分は Python インタプリタを rye で管理しているので、rye install 経由でインストールした。
rye install 便利。

rye install pdm
zztkmzztkm

Project initialize

プロジェクトの初期化(pyproject.toml の生成)には pdm init を使う。

プロジェクトバージョンの次に「配布用にプロジェクトをビルドするか?」と聞かれる、ここで y を選択するとビルドバックエンドを選択できる。n の場合は特に設定がなく、最終的な pyproject.toml では tool.pdm.distribution = false になる。

個人的に、インタプリタを選ばせてくれるところと、配布用プロジェクトかどうかを確認してくれるところが嬉しい。

pdm init

Creating a pyproject.toml for PDM...
Please enter the Python interpreter to use
0. /opt/homebrew/bin/python3.12 (3.12)
1. /Users/zztkm/.rye/py/cpython@3.12.0/install/bin/python3 (3.12)
2. /Users/zztkm/.rye/py/cpython@3.11.6/install/bin/python3 (3.11)
3. /Users/zztkm/.rye/shims/python3 (3.11)
Please select (0): 2
Would you like to create a virtualenv with /Users/zzztkm/.rye/py/cpython@3.11.6/install/bin/python3? [y/n] (y):
Virtualenv is created successfully at /Users/zztkm/dev/sandbox/pydev/rye-init-test/.venv
Project name (hello-pdm):
Project version (0.1.0):
Do you want to build this project for distribution(such as wheel)?
If yes, it will be installed by default when running `pdm install`. [y/n] (n): y
Project description (): hello pdm
Which build backend to use?
0. pdm-backend
1. setuptools
2. flit-core
3. hatchling
Please select (0): 3
License(SPDX name) (MIT):
Author name (zztkm):
Author email (zztkm@tsurutatakumi.info):
Python requires('*' to allow any) (>=3.11):
Project is initialized successfully

生成された pyproject

[project]
name = "hello-pdm"
version = "0.1.0"
description = "hello pdm"
authors = [
    {name = "zztkm", email = "zztkm@tsurutatakumi.info"},
]
dependencies = []
requires-python = ">=3.11"
readme = "README.md"
license = {text = "MIT"}

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"


[tool.pdm]
distribution = true
zztkmzztkm

pdm の lock ファイルについて少しふかぼりたい

zztkmzztkm

Python 3.8 以上をサポートしたいときに、opencv-python をインストールしようとしたらこのエラーがでた。

pdm use で選択したのは Python3.12 だったので、これを 3.11 に戻して distutils が存在するバージョンにしないといけなそう。
サポートしてるすべてのバージョンに対応するモジュールをインストールしようとしてくれてるのかな...

❯ pdm add opencv-python
Adding packages to default dependencies: opencv-python
/home/zztkm/.rye/tools/pdm/lib/python3.12/site-packages/pdm/resolver/providers.py:202: PackageWarning: Skipping numpy@1.26.4 because it requires Python>=3.9 but the
project claims to work with Python>=3.8. Instead, another version of numpy that supports Python>=3.8 will be used.
If you want to install numpy@1.26.4, narrow down the `requires-python` range to include this version. For example, ">=3.9" should work.
  return self.repository.find_candidates(
/home/zztkm/.rye/tools/pdm/lib/python3.12/site-packages/pdm/resolver/providers.py:202: PackageWarning: Skipping numpy@1.26.3 because it requires Python>=3.9 but the
project claims to work with Python>=3.8. Instead, another version of numpy that supports Python>=3.8 will be used.
If you want to install numpy@1.26.3, narrow down the `requires-python` range to include this version. For example, ">=3.9" should work.
  return self.repository.find_candidates(
/home/zztkm/.rye/tools/pdm/lib/python3.12/site-packages/pdm/resolver/providers.py:202: PackageWarning: Skipping numpy@1.26.2 because it requires Python>=3.9 but the
project claims to work with Python>=3.8. Instead, another version of numpy that supports Python>=3.8 will be used.
If you want to install numpy@1.26.2, narrow down the `requires-python` range to include this version. For example, ">=3.9" should work.
  return self.repository.find_candidates(
/home/zztkm/.rye/tools/pdm/lib/python3.12/site-packages/pdm/resolver/providers.py:202: PackageWarning: Skipping numpy@1.26.1 because it requires Python<3.13,>=3.9 but
the project claims to work with Python>=3.8. Instead, another version of numpy that supports Python>=3.8 will be used.
If you want to install numpy@1.26.1, narrow down the `requires-python` range to include this version. For example, ">=3.9,<3.13" should work.
  return self.repository.find_candidates(
/home/zztkm/.rye/tools/pdm/lib/python3.12/site-packages/pdm/resolver/providers.py:202: PackageWarning: Skipping numpy@1.26.0 because it requires Python<3.13,>=3.9 but
the project claims to work with Python>=3.8. Instead, another version of numpy that supports Python>=3.8 will be used.
If you want to install numpy@1.26.0, narrow down the `requires-python` range to include this version. For example, ">=3.9,<3.13" should work.
  return self.repository.find_candidates(
/home/zztkm/.rye/tools/pdm/lib/python3.12/site-packages/pdm/resolver/providers.py:202: PackageWarning: Skipping numpy@1.25.2 because it requires Python>=3.9 but the
project claims to work with Python>=3.8. Instead, another version of numpy that supports Python>=3.8 will be used.
If you want to install numpy@1.25.2, narrow down the `requires-python` range to include this version. For example, ">=3.9" should work.
  return self.repository.find_candidates(
/home/zztkm/.rye/tools/pdm/lib/python3.12/site-packages/pdm/resolver/providers.py:202: PackageWarning: Skipping numpy@1.25.1 because it requires Python>=3.9 but the
project claims to work with Python>=3.8. Instead, another version of numpy that supports Python>=3.8 will be used.
If you want to install numpy@1.25.1, narrow down the `requires-python` range to include this version. For example, ">=3.9" should work.
  return self.repository.find_candidates(
/home/zztkm/.rye/tools/pdm/lib/python3.12/site-packages/pdm/resolver/providers.py:202: PackageWarning: Skipping numpy@1.25.0 because it requires Python>=3.9 but the
project claims to work with Python>=3.8. Instead, another version of numpy that supports Python>=3.8 will be used.
If you want to install numpy@1.25.0, narrow down the `requires-python` range to include this version. For example, ">=3.9" should work.
  return self.repository.find_candidates(
INFO: Use `-q/--quiet` to suppress these warnings, or ignore them per-package with `ignore_package_warnings` config in [tool.pdm] table.
🔒 Lock successful
Changes are written to pyproject.toml.
Synchronizing working set with resolved packages: 2 to add, 0 to update, 0 to remove

  ✖ Install numpy 1.24.4 failed
  ✔ Install opencv-python 4.8.1.78 successful
Retry failed jobs
  ✖ Install numpy 1.24.4 failed

ERRORS:
add numpy failed:
Traceback (most recent call last):
  File "/home/zztkm/.rye/py/cpython@3.12.1/install/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/zztkm/.rye/tools/pdm/lib/python3.12/site-packages/pdm/installers/synchronizers.py", line 286, in install_candidate
    self.manager.install(can)
  File "/home/zztkm/.rye/tools/pdm/lib/python3.12/site-packages/pdm/installers/manager.py", line 34, in install
    dist_info = installer(str(prepared.build()), self.environment, prepared.direct_url())
                              ^^^^^^^^^^^^^^^^
  File "/home/zztkm/.rye/tools/pdm/lib/python3.12/site-packages/pdm/models/candidates.py", line 418, in build
    self.wheel = Path(builder.build(build_dir, metadata_directory=self._metadata_dir))
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/zztkm/.rye/tools/pdm/lib/python3.12/site-packages/pdm/builders/wheel.py", line 26, in build
    requires = self._hook.get_requires_for_build_wheel(config_settings)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/zztkm/.rye/tools/pdm/lib/python3.12/site-packages/pyproject_hooks/_impl.py", line 166, in get_requires_for_build_wheel
    return self._call_hook('get_requires_for_build_wheel', {
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/zztkm/.rye/tools/pdm/lib/python3.12/site-packages/pyproject_hooks/_impl.py", line 321, in _call_hook
    raise BackendUnavailable(data.get('traceback', ''))
pyproject_hooks._impl.BackendUnavailable: Traceback (most recent call last):
  File "/home/zztkm/.rye/tools/pdm/lib/python3.12/site-packages/pyproject_hooks/_in_process/_in_process.py", line 77, in _build_backend
    obj = import_module(mod_path)
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/zztkm/.rye/py/cpython@3.12.1/install/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name, package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1310, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 994, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/tmp/pdm-build-env-3x4_vnfi-shared/lib/python3.12/site-packages/setuptools/__init__.py", line 10, in <module>
    import distutils.core
ModuleNotFoundError: No module named 'distutils'


See /tmp/pdm-install-85k1yvno.log for detailed debug log.
[InstallationError]: Some package operations are not complete yet
WARNING: Add '-v' to see the detailed traceback