はじめに
※個人用のメモになります。(諸々説明不足ですがご容赦ください。)
環境
- windows 11 home 24H2
uv インストール
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
Python インストール
利用できるPythonバージョンを確認
※インストール済みの場合はパスが表示される
uv python list
Pythonインストール(今回は3.12)
uv python install 3.12
プロジェクト作成
プロジェクト作成(今回は、プロジェクト名はexampleとする)
※-pオプションでPythonバージョンを指定する(今回は3.12)
uv init example -p 3.12
作成されたディレクトリに移動
cd example
インストール済みのパッケージ確認
uv pip list
Ruff インストール
Ruff:Linter & Formtter
公式ドキュメント
uv add --dev ruff
taskipy インストール
taskipy:タスクランナー
GitHub
uv add --dev taskipy
Jupyter Notebook インストール
Jupyter Notebookを使うため以下パッケージの追加
uv add --dev ipykernel
VScode拡張機能整備
およそ以下目的のため
- Jupyter Notebookを開く
- ファイル保存時の自動フォーマット
- docstringを楽に書く
以下のファイルを配置し、Recommendされた拡張機能をインストール
.vscode/extensions.json
{
"recommendations": [
"ms-toolsai.jupyter",
"ms-toolsai.vscode-jupyter-cell-tags",
"ms-toolsai.jupyter-keymap",
"ms-toolsai.jupyter-renderers",
"ms-toolsai.vscode-jupyter-slideshow",
"ms-python.vscode-pylance",
"ms-python.python",
"ms-python.debugpy",
"charliermarsh.ruff",
"njpwerner.autodocstring"
]
}
.vscode/settings.json
{
"[python]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
"source.organizeImports": "explicit",
},
"editor.defaultFormatter": "charliermarsh.ruff",
},
"notebook.formatOnSave.enabled": true,
"notebook.codeActionsOnSave": {
"notebook.source.fixAll": "explicit",
"notebook.source.organizeImports": "explicit",
},
}
FormatterやLinterの設定
pyproject.tomlが以下の内容となるように適宜追記する
※srcディレクトリを作ってその中にソースコードは配置する前提
pyproject.toml
[project]
name = "hoge-fuga"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = []
[dependency-groups]
dev = [
"ipykernel>=6.29.5",
"ruff>=0.9.10",
"taskipy>=1.14.1",
]
[tool.taskipy.tasks]
lint = "uv run ruff check src"
format = "uv run ruff format src"
[tool.ruff.lint]
select = ["ALL"]
ignore = [
"T201", # print
"COM812", # missing-trailing-comma
]
unfixable = [
"F401", # unused-import
"F841", # unused-variable
]
pydocstyle.convention = "google"
[tool.ruff.lint.pylint]
max-args = 6
Linterの手動実行
uv run task lint
Formatterの手動実行
uv run task format
Jupyter Notebook を開く
拡張子.ipynb
のファイルを作成
touch src/test.ipynb
作成したファイルを開き、右上のSelect Kernel
をクリック
-> Python Environments
-> .venv (Python 3.X.X)
の順に選択
おわりに
uv + ruff の環境を今更ながら知り、すごく使いやすかったのでメモを残しました。
参考文献
大変参考になりました。ありがとうございました。