🕌

[memo] pythonのパッケージ管理ryeを使ってみた(スクラップの転記)

2024/10/02に公開

ライと読むらしい(たぶん)

インストール

curl -sSf https://rye-up.com/get | bash

homebrewもありました。
https://formulae.brew.sh/formula/rye

パスを通す

echo 'source "$HOME/.rye/env"' >> ~/.zshrc
source ~/.zshrc

新規プロジェクトの作成

rye init test-project

プロジェクトはこんな感じ

$ tree test-project 
test-project
├── README.md
├── pyproject.toml
└── src
    └── test_project
        └── __init__.py

3 directories, 3 files

pyproject.tomlでPythonプロジェクトのビルドシステムの要件や依存関係を定義します。

pythonのバージョンを指定する

rye pin 3.11

バージョンファイルが作成されます。

仮想環境を作成

rye sync
ここでエラー発生
$ rye sync
Initializing new virtualenv in /Users/motonarikurita/tech/test/test-project/.venv
Python version: cpython@3.11.6
Generating production lockfile: /Users/motonarikurita/tech/test/test-project/requirements.lock
Generating dev lockfile: /Users/motonarikurita/tech/test/test-project/requirements-dev.lock
Installing dependencies
Looking in indexes: https://pypi.org/simple/
Obtaining file:///. (from -r /var/folders/4v/81cp8z0s061f1n1347n0nq_h0000gn/T/tmp0cw9ttou (line 1))
  Installing build dependencies ... done
  Checking if build backend supports build_editable ... done
  Getting requirements to build editable ... done
  Preparing editable metadata (pyproject.toml) ... done
Building wheels for collected packages: test-project
  Building editable for test-project (pyproject.toml) ... done
  Created wheel for test-project: filename=test_project-0.1.0-py3-none-any.whl size=1337 sha256=f8621fdb4d98376a2a4c7ce7eed689fee0f19684a64e9ae4dac6bf604c1d3fbf
  Stored in directory: /private/var/folders/4v/81cp8z0s061f1n1347n0nq_h0000gn/T/pip-ephem-wheel-cache-schj49wr/wheels/97/54/f5/d849319cdfa096e074df352654ee2e7c919da8951f090690c6
Successfully built test-project
Installing collected packages: test-project
ERROR: For req: test-project==0.1.0. Invalid script entry point: <ExportEntry hello = test:None []> - A callable suffix is required. Cf https://packaging.python.org/specifications/entry-points/#use-for-scripts for more information.
Traceback (most recent call last):
  File "/Users/motonarikurita/.rye/pip-tools/cpython@3.11/bin/pip-sync", line 8, in <module>
    sys.exit(cli())
             ^^^^^
  File "/Users/motonarikurita/.rye/pip-tools/cpython@3.11/lib/python3.11/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/motonarikurita/.rye/pip-tools/cpython@3.11/lib/python3.11/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/Users/motonarikurita/.rye/pip-tools/cpython@3.11/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/motonarikurita/.rye/pip-tools/cpython@3.11/lib/python3.11/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/motonarikurita/.rye/pip-tools/cpython@3.11/lib/python3.11/site-packages/piptools/scripts/sync.py", line 174, in cli
    sync.sync(
  File "/Users/motonarikurita/.rye/pip-tools/cpython@3.11/lib/python3.11/site-packages/piptools/sync.py", line 244, in sync
    run(  # nosec
  File "/Users/motonarikurita/.rye/py/cpython@3.11.6/install/lib/python3.11/subprocess.py", line 571, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['/Users/motonarikurita/tech/test/test-project/.venv/bin/python', '-m', 'pip', 'install', '-r', '/var/folders/4v/81cp8z0s061f1n1347n0nq_h0000gn/T/tmp0cw9ttou', '--index-url', 'https://pypi.org/simple/', '--python=/Users/motonarikurita/tech/test/test-project/.venv/bin/python', '--no-deps']' returned non-zero exit status 1.
error: Installation of dependencies failed

ここ <ExportEntry hello = test:None []> helloというエントリーポイントがないよと言っている。

pyproject.toml

[project.scripts]
hello = "test-project:hello"

ディレクトリ

$ tree src 
src
└── test_project
    └── __init__.py

ディレクトリ名が違うせいですね。(なんでや)

pyproject.tomlを修正

[project.scripts]
hello = "test_project:hello"

試してみる

$ rye run hello
Hello from test-project!

無事動きました。

ライブラリの追加

rye add openai

で同期する

rye sync

おまけ

Dockerなどでryeは入れたくないけど、pip installしたい場合は以下のような形で requirements.lock を書き換えてあげましょう。

FROM python:3.12.0-slim as builder

WORKDIR /build

COPY requirements.lock ./

RUN sed '/-e/d' requirements.lock > requirements.txt && \
    pip install --user -r requirements.txt

Discussion