💡

ディファクトになりつつあるRyeでPython開発環境を整える

2023/08/24に公開

TL;DR

Pythonの開発環境を整える際、選択肢がありすぎて困る。
その困りごとを一掃してくれるのが、Ryeである。
Pythonの開発環境を整えるのであれば、Ryeを使うべし。

Ryeとは

Pythonにおけるone-stop-shop、すなはち、これ1つですべての用事が足りるものらしいです。
以下は原文。

Rye is Armin's personal one-stop-shop for all his Python needs. It installs and manages Python installations, manages pyproject.toml files, installs and uninstalls dependencies, manages virtualenvs behind the scenes. It supports monorepos and global tool installations.

https://github.com/mitsuhiko/rye

Ryeのインストール

当方は、Mac(zsh)を利用している。
ターミナルから以下のコマンドを叩く。

zsh
curl -sSf https://rye-up.com/get | bash
echo 'source "$HOME/.rye/env"' >> ~/.zshrc
source ~/.zshrc

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

GitHubのセットアップ

まずは作業用のGitHubのリポジトリ作成する

brew upgrade gh # or brew install gh && gh auth login
gh repo create fastapi-sandbox --public
gh repo clone fastapi-sandbox 

Ryeのセットアップ

リポジトリ直下にプロジェクトを作成する。
最新安定版のPython3.11をインストールする。

rye init .
rye pin 3.11
rye sync

動作確認

Python環境でバージョン表示してみる

. .venv/bin/activate
python -V

すげー、一瞬でPythonの環境できた。

おわりに

Python 3.11の環境が一瞬で構築できました。
この後、FastAPIというライブラリを活用してRESTful APIでも構築してみようと思います。

以上。

Discussion