Closed5

Python実行環境をRyeで構築する

kobayashi-m42kobayashi-m42

インストール

curl -sSf https://rye-up.com/get | bash
This script will automatically download and install rye (latest) for you.
######################################################################## 100.0%
Welcome to Rye!

This installer will install rye to /Users/xxxx/.rye
This path can be changed by exporting the RYE_HOME environment variable.

Details:
  Rye Version: 0.32.0
  Platform: macos (aarch64)

✔ Continue? · yes
✔ Select the preferred package installer · uv (fast, recommended)
✔ What should running `python` or `python3` do when you are not inside a Rye managed project? · Run a Python installed and managed by Rye
✔ Which version of Python should be used as default toolchain? · cpython@3.12
Installed binary to /Users/xxxx/.rye/shims/rye
Bootstrapping rye internals
Downloading cpython@3.12.2
Checking checksum
Unpacking
Downloaded cpython@3.12.2
Updated self-python installation at /Users/xxxx/.rye/self

The rye directory /Users/xxxx/.rye/shims was not detected on PATH.
It is highly recommended that you add it.
✔ Should the installer add Rye to PATH via .profile? · yes
Added to PATH.
note: for this to take effect you will need to restart your shell or run this manually:

    source "$HOME/.rye/env"

To make it work with zsh, you might need to add this to your .zprofile:

    source "$HOME/.rye/env"

For more information read https://rye-up.com/guide/installation/

All done!

zshを使っているので、以下の手順を実行。

To make it work with zsh, you might need to add this to your .zprofile:

echo 'source "$HOME/.rye/env"' >> ~/.zprofile
kobayashi-m42kobayashi-m42

プロジェクトの作成

以下を参考にプロジェクトを作成する。
https://rye-up.com/guide/basics/

rye init my-project

以下の構成のプロジェクトが作成される。

.
├── .git
├── .gitignore
├── .python-version
├── README.md
├── pyproject.toml
└── src
    └── my_project
        └── __init__.py

rye syncを実行する。
.venvディレクトリとrequirements.lockrequirements-dev.lockファイルが作成される。

ライブラリの追加

以下のコマンドでライブラリを追加。

rye add flask

pyproject.tomlrequirements.lockrequirements-dev.lockにflaskが追加される。

kobayashi-m42kobayashi-m42

Dockerでの実行

ドキュメントを参考に、Dockerで動作させる。

https://rye-up.com/guide/docker/

pyproject.toml[tool.rye]のセクションの下にvirtual = trueを追加。
rye syncを実行。
requirements.lockrequirements-dev.lockに以下の差分が発生する。
これでビルド時にパッケージのインストールが可能となる。

requirements.lock
--e file:.
 blinker==1.7.0
     # via flask
 click==8.1.7
     # via flask
 flask==3.0.3
-    # via my-project
 itsdangerous==2.2.0
     # via flask
 jinja2==3.1.3

kobayashi-m42kobayashi-m42

Pythonのバージョンを変更

Ryeで管理しているバージョンの確認

rye toolchain list 

3.12.4をダウンロードしようとしたところ下記のエラーになった

rye fetch 3.12.4
error: error while fetching Python installation

Caused by:
    unknown version 3.12.4

rye自体のバージョンをアップデート

rye self update

3.12.4を再度ダウンロードし成功

rye fetch 3.12.4

.python-version を変更する

.python-version
3.12.4

以下のコマンド実行

rye sync

バージョン確認

rye run python --version
Python 3.12.4
このスクラップは2024/09/26にクローズされました