Open6

Rye メモ

mitsuhiko/rye: An Experimental Package Management Solution for Python

Pythonのインストールと管理、pyproject.tomlファイルの管理、依存関係のインストールとアンインストール、virtualenvsの裏側での管理などを行います。monoreposやグローバルツールのインストールにも対応しています。

Rye は mitsuhiko (Flask や Jinja2 の開発者で現在 Sentry を開発している) が開発している Python パッケージマネージャーです。Rust で書かれています。

今までは pipenv を使っていたのですが、恐ろしく使いづらかったので Rye に切り替えました。切り替えて今のところ困っていません。

pyenv + pipenv というツールだと思ってもらうのがよさそうです。

pyproject.toml

PEP 518 – Specifying Minimum Build System Requirements for Python Projects | peps.python.org

Rye が利用している pyproject.toml はちゃんと PEP になってるようです。

インストール

Installation - Rye

macOS であれば curl -sSf https://rye-up.com/get | bash で入ります。

Rye は Rust で書かれており、Python に依存しません。

セットアップ

  • rye initpyproject.toml を用意してくれます
    • rye add pytest のように追加していってください。
  • パッケージのインストールは rye sync すれば終わります
  • 利用したい Python のバージョンの固定は rye pin 3.10 でできます

.venv がプロジェクト毎に作られます。もし Python 3.8 を使いたい場合は rye pin 3.8 と打ってその後 rye sync すればそのプロジェクトで 3.8 が使われるようになります。pyproject の requires-python = ">= 3.8" も勝手に更新されます。

rye run

  • rye run pythonrye pin した Pythonが呼び出されます

rye shll

  • rye shell を呼ぶとそのプロジェクトの .venv が有効になり。rye run を経由しなくて良くなる

GitHub Actions

- run: curl -sSf https://rye-up.com/get | RYE_INSTALL_OPTION="--yes" bash
- run: echo "$HOME/.rye/shims" >> $GITHUB_PATH

キャッシュはまぁイイかということで設定していませんが、設定した方が良いと思います。

そもそも CI であれば pip install でもいい

rye sync の動作を確認したいというのがあり、rye を使ってますが、ライブラリ自体のテストだけなら pip install -r requirements.lock だけでいい気がします。

依存関連

requirements-dev.lock にいれる場合は --dev を利用する。

$ rye add --dev pytest

グローバルに入れて呼ぶツール

rye install を使えば グローバル ~/.rye/tools にインストールされます。

Optuna

$ rye install optuna
Collecting optuna
  Downloading optuna-3.1.1-py3-none-any.whl (365 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 365.7/365.7 kB 1.7 MB/s eta 0:00:00
Collecting alembic>=1.5.0 (from optuna)
  Downloading alembic-1.11.1-py3-none-any.whl (224 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 224.5/224.5 kB 2.3 MB/s eta 0:00:00
Collecting cmaes>=0.9.1 (from optuna)
  Downloading cmaes-0.9.1-py3-none-any.whl (21 kB)
Collecting colorlog (from optuna)
  Downloading colorlog-6.7.0-py2.py3-none-any.whl (11 kB)
Collecting numpy (from optuna)
  Downloading numpy-1.24.3-cp311-cp311-macosx_11_0_arm64.whl (13.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 13.8/13.8 MB 21.0 MB/s eta 0:00:00
Collecting packaging>=20.0 (from optuna)
  Using cached packaging-23.1-py3-none-any.whl (48 kB)
Collecting sqlalchemy>=1.3.0 (from optuna)
  Downloading SQLAlchemy-2.0.15-cp311-cp311-macosx_11_0_arm64.whl (2.0 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.0/2.0 MB 21.3 MB/s eta 0:00:00
Collecting tqdm (from optuna)
  Using cached tqdm-4.65.0-py3-none-any.whl (77 kB)
Collecting PyYAML (from optuna)
  Downloading PyYAML-6.0-cp311-cp311-macosx_11_0_arm64.whl (167 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 167.5/167.5 kB 12.6 MB/s eta 0:00:00
Collecting Mako (from alembic>=1.5.0->optuna)
  Downloading Mako-1.2.4-py3-none-any.whl (78 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 78.7/78.7 kB 8.1 MB/s eta 0:00:00
Collecting typing-extensions>=4 (from alembic>=1.5.0->optuna)
  Using cached typing_extensions-4.6.2-py3-none-any.whl (31 kB)
Collecting MarkupSafe>=0.9.2 (from Mako->alembic>=1.5.0->optuna)
  Downloading MarkupSafe-2.1.2-cp311-cp311-macosx_10_9_universal2.whl (17 kB)
Installing collected packages: typing-extensions, tqdm, PyYAML, packaging, numpy, MarkupSafe, colorlog, sqlalchemy, Mako, cmaes, alembic, optuna
Successfully installed Mako-1.2.4 MarkupSafe-2.1.2 PyYAML-6.0 alembic-1.11.1 cmaes-0.9.1 colorlog-6.7.0 numpy-1.24.3 optuna-3.1.1 packaging-23.1 sqlalchemy-2.0.15 tqdm-4.65.0 typing-extensions-4.6.2

Installed scripts:
  - optuna
$ optuna
usage: optuna [-h] [--storage STORAGE] [-v | -q] [--log-file LOG_FILE] [--debug]
              [--version]
              {create-study,delete-study,study
              set-user-attr,studies,trials,best-trial,best-trials,study
              optimize,storage upgrade,ask,tell,help} ...

positional arguments:
  {create-study,delete-study,study set-user-attr,studies,trials,best-trial,best-trials,study optimize,storage upgrade,ask,tell,help}
    create-study        Create a new study.
    delete-study        Delete a specified study.
    study set-user-attr
                        Set a user attribute to a study.
    studies             Show a list of studies.
    trials              Show a list of trials.
    best-trial          Show the best trial.
    best-trials         Show a list of trials located at the Pareto front.
    study optimize      Start optimization of a study. Deprecated since version
                        2.0.0.
    storage upgrade     Upgrade the schema of a storage.
    ask                 Create a new trial and suggest parameters.
    tell                Finish a trial, which was created by the ask command.
    help                Show help message and exit.

options:
  -h, --help            show this help message and exit
  --storage STORAGE     DB URL. (e.g. sqlite:///example.db) Also can be specified
                        via OPTUNA_STORAGE environment variable.
  -v, --verbose         Increase verbosity of output. Can be repeated.
  -q, --quiet           Suppress output except warnings and errors.
  --log-file LOG_FILE   Specify a file to log output. Disabled by default.
  --debug               Show tracebacks on errors.
  --version             show program's version number and exit
作成者以外のコメントは許可されていません