Skip to content
閲覧中:
2.uv

2.uv

  • uv, 2026-05-02

1. uv インストール

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

2. 実際に仮想プロジェクトを作成

1. プロジェクトの作成と初期化

Bash
uv init my-project

2. ディレクトリへ移動

Bash
cd my-project
  • ファイルの一覧
    Bash
    ls
    # README.md  main.py  pyproject.toml
    

3. pythonのバージョン指定

Text Only
vim  pyproject.toml
requires-python = ">=3.14"
Bash
uv python pin 3.13
  • バージョンの確認
    Bash
    uv run python --version
    # Using CPython 3.13.13
    # Creating virtual environment at: .venv
    # Python 3.13.13
    

4. 仮想環境の作成

Bash
uv venv
  • ファイルの一覧
    Bash
    ls -a
    .git/  .gitignore  .python-version  README.md  main.py  pyproject.toml
    

5. 仮想環境の有効化

Bash
$ source .venv/bin/activate
# (my-project) yossym@yossym-NucBox-EVO-X2: ~/tmp/my-project

uvのコマンド

uv自体のバージョンアップ


  • curlでインストールした場合
Bash
uv self update

パッケージのインストール

Bash
uv add パッケージ名
  • requirements.txtに記載しているパッケージをインストール
Bash
uv add install -r requirements.txt
Bash
uv add install "flask[dotenv]"
uv add install flask ruff
uv add install 'ruff>=0.2.0'
uv add install 'ruff==0.3.0'
uv add install "ruff @ ./projects/ruff"
uv add install "git+https://github.com/astral-sh/ruff"

アップデート可能なライブラリを表示

Bash
uv sync -U --dry-run

ライブラリをアップデート

Bash
uv sync --upgrade

パッケージのアンインストール

Bash
uv uninstall

インストールしてしまったpythonのアンインストール


Bash
uv python uninstall 3.14

pinしようとしてエラー発生


Bash
$ uv python pin 3.13
error: The requested Python version `3.13` is incompatible with the project `requires-python` value of `>=3.14`.
  • 解決策:プロジェクトの要求仕様を変更する
Bash
uv init --requires-python ">=3.13" --force
Bash
cat pyproject.toml
[project]
name = "pandas-vs-polars"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.14"
dependencies = []
  • 修正案
    TOML
    # pyproject.toml
    [project]
    requires-python = ">=3.13"
    

requirements.txtを読み込む

Bash
uv add -r requirements.txt

requirements.txtを書き込む

Bash
uv pip freeze > requirements.txt