Open1
Pythonの新しい開発環境について学ぶ
Rye + uv + ruff
Rye
- Ryeはもともとより良いパッケージツールがどうあるべきかの実証のために作られていて、中身は既存のツールのツギハギだった
- いつかuvがRyeの機能に追いつくまでRyeの開発は当面続く
- RyeはAstralが引き継ぎ、Arminも当面Ryeの開発に参加する
uvと役割が同じなので、一旦Ryeの学習は後回しに
uv
astral-sh/uv
uv document
uvの紹介記事
Cargo for Pythonを目指しているらしい
Installation
curl -LsSf https://astral.sh/uv/install.sh | sh
downloading uv 0.3.4 aarch64-apple-darwin
installing to /Users/masahero/.cargo/bin
uv
uvx
everything's installed!
To add $HOME/.cargo/bin to your PATH, either restart your shell or run:
source $HOME/.cargo/env (sh, bash, zsh)
source $HOME/.cargo/env.fish (fish)source $HOME/.cargo/env
source $HOME/.cargo/env
簡単な操作
uv --version
uv 0.3.4 (39f3cd2a9 2024-08-26)
uv init example
Initialized project `example` at `/Users/masahero/Practice/newEnvPython/example`
exa -l
drwxr-xr-x - masahero 27 8 22:06 example
cd example/
uv add ruff
Using Python 3.11.4 interpreter at: /Library/Frameworks/Python.framework/Versions/3.11/bin/python3
Creating virtualenv at: .venv
Resolved 2 packages in 131ms
Built example @ file:///Users/masahero/Practice/newEnvPython/example
Prepared 2 packages in 693ms
Installed 2 packages in 1ms
+ example==0.1.0 (from file:///Users/masahero/Practice/newEnvPython/example)
+ ruff==0.6.2
uv run ruff check
All checks passed!
pipx代わりのuvx
uv tool run
のalias
環境を汚さずにPythonライブラリを入れたい場合に使う。
(無知ゆえ、pipx自体初めて知った。)
uvx pycowsay 'hello world!'
Installed 1 package in 5ms
------------
< hello world! >
------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
uv tool install ruff
Resolved 1 package in 66ms
Prepared 1 package in 902ms
Installed 1 package in 2ms
+ ruff==0.6.2
Installed 1 executable: ruff
ruff --version
ruff 0.6.2
Pythonバージョン管理
uv python install 3.12
Searching for Python versions matching: Python 3.12
Installed Python 3.12.5 in 2.80s
+ cpython-3.12.5-macos-aarch64-none
uv venv --python 3.12.5
Using Python 3.12.5
Creating virtualenv at: .venv
Activate with: source .venv/bin/activate
source .venv/bin/activate
python --version
Python 3.12.5
deactivate
ライブラリインストール
uv add hoge
でインストール可能。インストールの速さに驚く。
uv add numpy polars
Resolved 4 packages in 84ms
Built example @ file:///Users/masahero/Practice/newEnvPython/example
Prepared 4 packages in 2.98s
Installed 4 packages in 23ms
+ example==0.1.0 (from file:///Users/masahero/Practice/newEnvPython/example)
+ numpy==2.1.0
+ polars==1.5.0
+ ruff==0.6.2
bat pyproject.toml
───────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────
│ File: pyproject.toml
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ [project]
2 │ name = "example"
3 │ version = "0.1.0"
4 │ description = "Add your description here"
5 │ readme = "README.md"
6 │ requires-python = ">=3.11"
7 │ dependencies = [
8 │ "ruff>=0.6.2",
9 │ "numpy>=2.1.0",
10 │ "polars>=1.5.0",
11 │ ]
12 │
13 │ [build-system]
14 │ requires = ["hatchling"]
15 │ build-backend = "hatchling.build"
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────
スクリプト実行
$ echo 'import requests; print(requests.get("https://astral.sh"))' > example.py
$ uv add --script example.py requests
Updated `example.py`
example.py
の中身
example.py
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "requests",
# ]
# ///
import requests; print(requests.get("https://astral.sh"))
uv run scriptFile
で実行
uv run example.py
Reading inline script metadata from: example.py
Installed 5 packages in 3ms
<Response [200]>