Open10
RyeによるPython環境の管理
Ryeの導入
cargo install --git https://github.com/mitsuhiko/rye rye
または
curl -sSf https://rye-up.com/get | bash
Windowsの場合は開発者モードを有効にしてシンボリックリンクを有効にする。
初回実行
rye
Welcome to Rye!
Rye has detected that it's not installed on this computer yet and
automatically started the installer for you. For more information
read https://rye-up.com/guide/installation/
This installer will install rye to C:\Users\xxxxx\.rye
This path can be changed by exporting the RYE_HOME environment variable.
Details:
Rye Version: 0.4.0
Platform: windows (x86_64)
Continue?
yesで続行。~/.rye/が作られる。
Rye自身のアップデートは以下のコマンドで行う。
rye self update
ShimsをPathに追加
Ryeプロジェクトの作成
rye init my-project
cd my-project
このような感じで作成される。
.
├── .git
├── .gitignore
├── .python-version
├── README.md
├── pyproject.toml
└── src
└── my_project
└── __init__.py
現在のディレクトリをプロジェクトのルートにして作成する場合は以下のコマンド。
rye init
Pythonのバージョン変更は以下のコマンド。
rye pin バージョン
以下のコマンドで.venv/とrequirements.lockを作成する。
rye sync
依存関係の追加
rye addで追加する。以下はFlaskの2.0以上の場合の例。
rye add "flask>=2.0"
rye addした後は忘れずにrye syncする。
開発環境のみの依存関係は--devをつける。
グローバルツール
以下のどちらかのコマンドでインストール。
rye tools install パッケージ名
rye install パッケージ名
一覧表示は以下のコマンド。
rye tools list
tool.rye.scripts
Node.jsのpackage.jsonのscriptsみたいなやつ。
pyproject.toml
[tool.rye.scripts]
# These three options are equivalent:
devserver = "flask run --app ./hello.py --debug"
devserver-alt = ["flask", "run", "--app", "./hello.py", "--debug"]
devserver-explicit = { cmd = "flask run --app ./hello.py --debug" }
環境変数はenvで渡せる。
pyproject.toml
[tool.rye.scripts]
devserver = { cmd = "flask run --debug", env = { FLASK_APP = "./hello.py" } }
以下はdevserverを実行する例。
rye run devserver
仮想環境の有効化
venv(virtualenv)そのものなのでこれらと同様。
Unix
. .venv/bin/activate
Windows
.venv\Scripts\activate
依存関係ソースの追加