【python】ryeにuvをインストールしてみる

概要

github.com

以前にPythonの開発環境にryeを乗り換えたところ、uvに対応していたのでインストールしてみました。

環境

用語

rye

rye-up.com

公式の文章になりますが、Pythonでパッケージ管理だけでなく、プロジェクト管理も行うためのツールになります。

github.com

元々はRustにおけるcargoのように、プロジェクト毎にインストールされているパッケージをPythonでも管理したいという思いから作られています。

pyenvとの違い

例えば、pyenvと比較するとpyenvでは先に仮想環境を作成して、次にプロジェクトごと(フォルダごと)にプログラムを実行します。
そのために該当のプロジェクトでプログラムを実行する場合は

  1. 該当の仮想環境に切り替える(例:pythonのバージョンは3.10、パッケージモジュールは⚪︎⚪︎など)

  2. プロジェクト(フォルダ)に移動

  3. プログラムを実行

という手順になります。
そのために該当のプロジェクトでプログラムを実行しようとすると仮想環境を切り替えて実行する必要があります。
一方で、Ryeでプロジェクト管理するとプロジェクトごと(フォルダごと)に必要なパッケージやプログラムのバージョンなども管理できるため、

  1. プロジェクト(フォルダ)に移動

  2. プログラムを実行

で実行することができます。

uv

github.com

こちらも、公式の文章になりますが、Rustで実行されているPythonモジュールのインストーラであり、pipでパッケージをインストールするよりも高速です。

方法

Ryeのバージョンをチェック

下記より、0.11.0であることが分かります。
対応しているバージョンは0.24.0以上なので、アップデートする必要があります。

% rye --version
rye 0.11.0
commit: 0.11.0 (f6f63d6c1 2023-07-18)
platform: macos (x86_64)
self-python: cpython@3.11
symlink support: true

Ryeのバージョンのアップデート

下記のrye self updateコマンドを実行して、バージョンをアップデート。
ただし、このままだとuv enabled: falseの通り、uvは使えていないので、設定でtrueに変更します。

% rye self update
Updating to latest
Checking checksum
Updated!

rye 0.33.0
commit: 0.33.0 (58523f69f 2024-04-24)
platform: macos (x86_64)
self-python: not bootstrapped (target: cpython@3.12)
symlink support: true
uv enabled: false

uvを使えるように変更

rye-up.com

rye-up.com

公式を参考に下記のコマンドを実行。

% rye config --set-bool behavior.use-uv=true

次にホームディレクトリにあるconfig.tomlに設定されているか確認します。

% rye config --show-path
ホームディレクトリ/.rye/config.toml
% cat ホームディレクトリ/.rye/config.toml

[behavior]
use-uv = true

下記のようにuse-uv = trueとなっていれば、OKです。

[behavior]
use-uv = true

再度、rye self updateを実行します。

% rye self update
Updating to latest
Checking checksum
Validate updated installation
Detected outdated rye internals. Refreshing
Bootstrapping rye internals
Found a compatible Python version: cpython@3.11.3
Initializing new virtualenv in /private/var/folders/gf/5ntllx1s5wjdw2945t44f45w0000gn/T/.tmpopChOT/.venv
Python version: cpython@3.11.3
Updated!

rye 0.33.0
commit: 0.33.0 (58523f69f 2024-04-24)
platform: macos (x86_64)
self-python: cpython@3.11.3
symlink support: true
uv enabled: true

uv enabled: trueとなっていれば、uvを利用できるようになります。

動作確認

uvが使えるようになっているか確認します。

% rye --version
rye 0.33.0
commit: 0.33.0 (58523f69f 2024-04-24)
platform: macos (aarch64)
self-python: cpython@3.11.3
symlink support: true
uv enabled: true
% rye init test-uv-enable
success: Initialized project in /test-uv-enable
  Run `rye sync` to get started
% cd test-uv-enable
% rye add flask
Added flask>=3.0.3 as regular dependency
% rye add pandas
Added pandas>=2.0.3 as regular dependency
% time rye sync
Reusing already existing virtualenv
Generating production lockfile: /test-uv-enable/requirements.lock
Generating dev lockfile: /test-uv-enable/requirements-dev.lock
Installing dependencies
Installed 16 packages in 26ms
 + blinker==1.8.1
 + click==8.1.7
 + flask==3.0.3
 + importlib-metadata==7.1.0
 + itsdangerous==2.2.0
 + jinja2==3.1.3
 + markupsafe==2.1.5
 + numpy==1.24.4
 + pandas==2.0.3
 + python-dateutil==2.9.0.post0
 + pytz==2024.1
 + six==1.16.0
 + tzdata==2024.1
 + werkzeug==3.0.2
 + zipp==3.18.1
Done!
rye sync  0.43s user 0.21s system 101% cpu 0.629 total