0
0

python: UV いいねって聞いたので、やってみた際のメモ for windows

Posted at

背景

とりあえずやってみた際の自分用メモ

uv install

pwsh -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

or

pwsh -c "irm https://astral.sh/uv/install.ps1 | iex"

powershell 7.x 系は、pwsh.exe になっているので注意。
とはいえ、install 結果は変わらない気もする・・

以下、公式は、powershell.exe (5.x以前) を想定?

version 確認

uv --version

エラー対処

uv: The term 'uv' is not recognized as a name of a cmdlet, function, script file, or executable program.

Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

  • %userprofile%.cargo\bin がinstall 先なので、ここにパスを通せばよい

現在のPathの確認

Get-ChildItem -Path Env:PATH

pwsh とする理由について

公式によると、以下だが

powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

powershell は、7.0 以降、起動ファイルが変わっている。故に、今どきの powershell を使ってる人は pwsh で install したほうが良いと思う。

python 準備

使える一覧

自分の環境に入っているものを見られて便利。
これまでのぐちゃぐちゃさがよくわかる瞬間

uv python list

version install

適当に好きなバージョンを入れるだけ

uv python install 3.10 3.11 3.12

python project 作成

開発はプロジェクト単位で、npm init みたいな感じ

uv init {project name}

python 実行

この際、.venv が作成されていなければ、自動的に作成され、その仮想環境で実行される。
up pip install などは、その環境に行われる。
※Activate しなくても。明示的に Activate は可能

uv run {***.py}

uv run hello.py を実行すると、以下 .venv が勝手に出来る

image.png
image.png

実行時エラー

error: The requested Python interpreter (3.11.9) is incompatible with the project Python requirement: >=3.12

pyproject.toml 内の以下確認

requires-python = ">=3.12"


requirements.in / requirements./txt

依存関係の install

超高速になってありがたいやつ

uv pip install -r requirements.txt

依存関係解決 by compiling .in

で、requirements.txt を自動で生成する方法もほぼ困ることはない

pip-tools install

uv pip install pip-tools
uv pip install --upgrade pip-tools

compile

  • compile 確認
    pip compile の場合は default で txt 吐いてたけど、uv だと確認だけになってる。設定次第かも?
uv pip compile .\requirements.in
  • txt 作成
uv pip compile .\requirements.in --output-file requirements.txt

仮想環境

簡単に作成

  • カレントに、.venv を、カレント python で作成
    とはいえ、uv run {*.py} で自動生成されるので・・
uv venv

activate

{venv folder}/Scripts/activate

作成時に、以下のように表示されているので、迷うことは無い筈

Activate with: venv311\Scripts\activate

version と 仮想環境フォルダを指定して作成

  • version & folder 指定で作成
uv venv -p 3.11 venv311

あとがき

うん、確かに便利。
python やりはじめて浅いわけだけど、使う機会増えたから、アンテナ張っておかないと :sweat:

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0