昔はpoetryを用いていたのですが、今はuvが便利と聞いたのでそのメモです。
uvのwindowsへのインストール
下記の方法でインストールできます。 [Python]Windows環境でuvを使った効率的な仮想環境構築!インストールからVS Codeでのデバッグまで|こはた
プロジェクトの作成
uv init project-name
venvの構築(python 3.11をインストールする例)
2026年7月時点、python 3.12以上を使用するとOpemMMLabのインストールで嵌りやすいそうです。
cd project-name uv venv --python 3.11 uv python pin 3.11
pytorchのインストール(CUDA12.1の例)
pyproject.tomlのdependencies以降に以下を追記する
[project]
dependencies = [
"torch",
"torchvision"
]
[[tool.uv.index]]
name = "pytorch-cu121"
url = "https://download.pytorch.org/whl/cu121"
explicit = true
[tool.uv.sources]
torch = [{ index = "pytorch-cu121" }]
torchvision = [{ index = "pytorch-cu121" }]
[tool.ruff]
line-length = 120
[tool.ruff.lint]
select = ["E", "F", "W", "I", "B", "N", "UP", "SIM", "RUF", "PLR0915"]
[tool.pytest.ini_options]
testpaths = ["tests"]
[tool.pyright]
typeCheckingMode = "basic"
[dependency-groups]
dev = [
"mlflow>=3.14.0",
"pre-commit>=4.6.0",
"pyright>=1.1.411",
"pytest>=9.1.1",
"pytest-watch>=4.2.0",
"ruff>=0.15.21",
"ruff-format>=0.5.2",
]
編集後にuv syncを実行する
設定を変更する
Ctrl + Shift + PでVSCodeのsettings.jsonを開き以下を加える。これによりRuffがリアルタイムにコードを解析してくれる。
{ "editor.formatOnSave": true, "editor.codeActionsOnSave": { "source.fixAll": true }, "python.linting.enabled": false, "ruff.lint.run": "onType" }
.precommit-config.yamlを作成し、以下を記載する。これによりgitにcommitする際にRuff, Pyright, 単体テストが走り、失敗するとcommitできないようになる。
repos: - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.13.0 hooks: - id: ruff - id: ruff-format - repo: local hooks: - id: pyright name: run pyright entry: uv run pyright language: system pass_filenames: false - id: pytest name: run pytest entry: uv run pytest tests/ language: system pass_filenames: false
その後に以下を実行する。
uv run pre-commit install
git commitを通すためのダミー単体テストを作成し、initial commitする
mkdir tests
test_dummy.pyを作成する
def test_dummy() -> None: assert True
