84
72

uvが良いので今日から暫く使ってみます

Last updated at Posted at 2024-08-21

たまたまこちらのブログが目に入りました。

uvというPythonのパッケージ管理を行うツールです。

もともとはpipやpipxの代替として2月にリリースされました。(ブログ

今回の発表では、PythonプロジェクトやPython自体の管理もできるようになりましたとのことです。

pip、pip-tools、pipx、poetry、pyenv、virtualenvの機能が単一バイナリで提供されているよ!というウリ!!

代替品というだけではなく、Rustで記述されているため処理速度が早いという特徴があります。(10-100x fasterらしい)


公式ドキュメントのIntroductionをやってみました。

検証環境

Pythonが古い環境で試そうと思いまして、devcontainerでUbuntu 20.04.6 LTS (Focal Fossa)の環境を作りました。

Shell
cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.6 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.6 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

導入されているPythonのバージョンは3.8.10でした。

Shell
python3 --version
Python 3.8.10

Ubuntu 20.04.6 LTSは、aptでインストールできるPythonは3.9までで、3.10以降はインストールできませんが、uvを使うとインストールできました

uvをインストール

コマンド一発です

Shell
curl -LsSf https://astral.sh/uv/install.sh | sh

パスに追加し、シェルに反映

Shell
echo 'source $HOME/.cargo/env' >> ~/.bashrc
source ~/.bashrc

コマンド確認

Shell
uv --version
uv 0.3.0

機能紹介

プロジェクトマネジメント

Poerty的な機能です

プロジェクトを新規作成します。

Shell
uv init example
Initialized project `example` at `/workspaces/uv/example`

以下のファイルが生成されます。

example
├── pyproject.toml
├── README.md
└── src
    └── example
        └── __init__.py

2 directories, 3 files
pyproject.toml
[project]
name = "example"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = []

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

ライブラリーを追加する場合はuv addコマンドです。

Shell
cd example
uv add langchain langchain-aws

ライブラリーを追加すると、pyproject.tomlのdependencies に追加されます。

pyproject.toml
  [project]
  name = "example"
  version = "0.1.0"
  description = "Add your description here"
  readme = "README.md"
  requires-python = ">=3.12"
+ dependencies = [
+     "langchain>=0.2.14",
+     "langchain-aws>=0.1.16",
+ ]
  
  [build-system]
  requires = ["hatchling"]
  build-backend = "hatchling.build"

ツールマネジメント

pipx的な機能です

コマンドラインツールを直接実行できる機能です。pycowsayを呼び出す例です。

Shell
uvx pycowsay "hello world!"

  ------------
< hello world! >
  ------------
   \   ^__^
    \  (oo)\_______
       (__)\       )\/\
           ||----w |
           ||     ||

Pythonマネジメント

pyenv的な機能です

指定したバージョンのPythonをインストールします。

Shell
uv python install 3.10 3.11 3.12
Searching for Python versions matching: Python 3.10
Searching for Python versions matching: Python 3.11
Searching for Python versions matching: Python 3.12
Installed 3 versions in 6.99s
 + cpython-3.10.14-linux-x86_64-gnu
 + cpython-3.11.9-linux-x86_64-gnu
 + cpython-3.12.5-linux-x86_64-gnu

指定したバージョンのPythonを起動するにはuv runコマンドのパラメーターでPythonのバージョンを指定します。

  • Python 3.10を実行

    Shell
    uv run --python 3.10 -- python --version
    
    Python 3.10.14
    
  • Python 3.11を実行

    Shell
    uv run --python 3.11 -- python --version
    
    Python 3.11.9
    
  • Python 3.12を実行

    Shell
    uv run --python 3.12 -- python --version
    
    Python 3.12.5
    

ただ、プロジェクトの場合は、自動で必要なバージョンのPythonが検知してインストールされます。

仮想環境を構築するuv venvコマンドを実行すると、pyproject.tomlのrequires-pythonを指定に従って自動でインストールされます。

Shell
uv venv
Using Python 3.12.5
Creating virtualenv at: .venv
Activate with: source .venv/bin/activate
Shell
source .venv/bin/activate
python --version
Python 3.12.5

インストールされるPythonはコミュニティでメンテナンスされているPython Standalone Buildsとのことです。(参考

簡単にインストールされますが、出どころはしっかり気にしておくと良いと思います。

Pyenvの場合

Pyenvの場合、ソースコードからビルドしてインストールを行うので、ビルドのための環境構築が必要、ビルドに時間がかかるという問題がありました。uvの場合は、数秒で導入が完了します。

Shell
## pyenvをインストール
curl https://pyenv.run | bash

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"

## Pythonのビルドのための環境構築
sudo apt update
sudo apt install -y \
  build-essential \
  libssl-dev \
  zlib1g-dev \
  libbz2-dev \
  libreadline-dev \
  libsqlite3-dev \
  curl \
  git \
  libncursesw5-dev \
  xz-utils \
  tk-dev \
  libxml2-dev \
  libxmlsec1-dev \
  libffi-dev \
  liblzma-dev

## Pythonをインストール(ビルドを行うので時間がかかる)
pyenv install 3.12

## Pythonを利用する
pyenv shell 3.12.5
python --version

まとめ

とても使いやすいと感じました。今日から暫く使ってみようと思います。

CLIコマンドも豊富ですし、ドキュメントが整理されていてとても見やすいです。

84
72
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
84
72