Python Project Setup
なんだかんだ毎回忘れるのでメモ
uv を使って管理する場合を対象としています
uv でプロジェクト管理を行う際の一般的な操作については、以下の記事がおすすめ
Environment
Python 3.11 (Microsoft Store Version)
uv 0.4.27 (36b729e92 2024-10-25)
UV のセットアップ
前提
code:shell
pipx install uv
pipx upgrade uv
UV で Python Projectを作成
code:setup.sh
uv init sample-project
cd sample-project
使用するPythonバージョンを指定
code:setup.sh
uv python pin 3.11
Project の設定
uv で Project 管理ができるようにする
uv 0.4 以降は不要っぽい
詳しい設定項目については以下を参照のこと
code:setup.sh
echo -e "tool.uv\nmanaged = true\n" >> pyproject.toml Windows の場合
code:setup.bat
(echo;&echo tool.uv&echo managed = true) >> pyproject.toml 以下のようになるはず
code:toml
name = "sample-project"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.11"
dependencies = []
+ managed = true
+
開発用パッケージの追加
code:setup.sh
uv add --dev ruff pre-commit pytest
設定
ruff や pre-commit の設定を行う
ruff
デフォルトの設定については以下参照
ruff に関しては target-version = "py38" 以外はdefaultでいいと思っている
pre-commit
コマンドでサンプル設定が出せる
code:setup.sh
uv run pre-commit sample-config >> .pre-commit-config.yaml
サンプルはこんな感じの設定になっている
code:sample.yaml
repos:
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
ruff や ignore の設定を追加
そのほかはお好みで
code:.pre-commit-config.yaml
exclude: "\
.*pyi$|\
.vscode\/.*json$|\
.*(ps1|bat|cmd|sh|txt|log|md|gitignore|gitattributes|gitkeep)$|\
(README|LICENSE)$\
"
repos:
rev: v3.2.0
hooks:
# Removes trailing whitespaces.
- id: trailing-whitespace
# Adds an empty line if missing at the end of a file.
- id: end-of-file-fixer
# Ensures a yaml file is properly formatted
- id: check-yaml
# Avoid committing large files
- id: check-added-large-files
# Ensures the code is syntaxically correct
- id: check-ast
language_version: python3
# Ensures a file name will resolve on all platform
- id: check-case-conflict
# Checks files with the execute bit set have shebangs
- id: check-executables-have-shebangs
# Ensure there's no incomplete merges
- id: check-merge-conflict
# Makes sure requirements.txt is properly formatted
- id: requirements-txt-fixer
# Use Ruff instead of Black to format the code.
# Ruff version.
rev: v0.6.2
hooks:
# Run the linter.
- id: ruff
# Run the formatter.
- id: ruff-format
設定を配置したらインストールを忘れずに
code:setup.sh
uv run pre-commit install
その他
pyproject.toml
ビルド設定やエントリポイント、スクリプト等は適宜設定すること
.gitignore
Project 作成時点で uv が最低限は作ってくれている
VS Code
僕は VSCode で開発をするので、code のワークスペースも作る
code:sample-project.code-workspace.json
{
"folders": [
{
"path": "."
}
],
"settings": {
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
"files.trimTrailingWhitespace": true,
"yaml.format.enable": true,
"yaml.completion": true,
"yaml.validate": true,
"yaml.hover": true,
"editor.insertSpaces": true,
"editor.tabSize": 2,
"editor.autoIndent": "keep",
"diffEditor.ignoreTrimWhitespace": false,
"editor.quickSuggestions": {
"other": true,
"comments": false,
"strings": true
},
"editor.defaultFormatter": "redhat.vscode-yaml",
},
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.ruff": "explicit",
"source.organizeImports.ruff": "explicit"
}
},
"python.analysis.extraPaths": [],
"python.autoComplete.extraPaths": [],
"python.analysis.stubPath": "typings",
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true,
"python.testing.autoTestDiscoverOnSaveEnabled": true,
"python.testing.unittestArgs": [
"-v",
"-s",
"./tests",
"-p",
"test_*.py"
],
},
"extensions": {
"recommendations": [
"redhat.vscode-yaml",
"ms-python.vscode-pylance",
"ms-python.python",
"ms-python.debugpy",
"charliermarsh.ruff",
"tamasfe.even-better-toml",
"aaron-bond.better-comments"
]
},
"launch": {
"version": "0.2.0",
"configurations": [],
"compounds": []
},
"tasks": {
"version": "2.0.0",
"tasks": [
{
"label": "Sync dependencies",
"command": "uv",
"type": "shell",
"args": [
"sync"
],
"problemMatcher": [
"$tsc"
],
"presentation": {
"reveal": "always"
}
},
{
"label": "Build package",
"command": "uv",
"type": "shell",
"args": [
"build"
],
"problemMatcher": [
"$tsc"
],
"presentation": {
"reveal": "always"
},
"dependsOn": "Sync dependencies",
"group": "build"
},
]
}
}