プロジェクト管理したい(pyproject.toml

 1[project]
 2name = "PyPIに公開するプロジェクト名"
 3version = "..."
 4description = "..."
 5readme = "README.md"
 6requires-python = ">=3.10"
 7authors = [
 8    {name = "qumasan", email = "..."}
 9]
10license = { text = "MIT" }
11keywords = ["...", "..."]
12dependencies = [
13    "pydantic",
14    "typer",
15    "loguru",
16]
17
18[project.optional-dependencies]
19docs = [
20    "mkdocs",
21    "mkdocs-material",
22    "mkdocstrings[python]",
23]
24
25dev = [
26    "pytest",
27    "pytest-cov",
28    "ruff",
29    "commitizen",
30]
31
32[project.scripts]
33script_name = "..."
34
35[project.urls]
36Repository = "..."
37Documentation = "..."
38Issues = "..."
39
40[build-system]
41requires = ["hatchling"]
42build-backend = "hatchling.build"
43
44[tool.uv]
45managed = true
46
47[tool.ruff]
48line-length = 100
49target-version = "py312"
50
51[tool.commitizen]
52name = "cz_conventional_commits"
53tag_format = "$version"
54version_scheme = "semver2"
55version_provider = "uv"
56update_changelog_on_bump = true
57major_version_zero = true
58version_files = [
59    "..."
60]

pyproject.tomlは、Pythonプロジェクトの設定を一元管理するための標準ファイルです。 Python3.7以降からデファクトスタンダードになっており、ビルド・依存関係・パッケージ情報・ツール設定をまとめて書くことができます。

注釈

これまで、Pythonのプロジェクト設定は、 setup.pysetup.cfgrequirements.txtなどに分散していました。

PEP518、PEP621、PEP517などにより標準化されました。

プロジェクト設定([project]

依存パッケージ([project.dependencies]

依存パッケージ(オプション)([project.optional-dependencies]

ビルド環境([build-system]

1[build-system]
2requires = ["hatchling"]
3build-backend = "hatchling.build"

[build-system]で、パッケージをビルドするためのツールを指定できます。

パッケージ名とディレクトリ構造を変えたい

1[project]
2name = "osechi-kazunoko"
3
4[tool.hatch.build.targets.wheel]
5packages = ["src/kazunoko"]
6
7[build-system]
8requires = ["hatchling"]
9build-backend = "hatchling.build"

pyserialパッケージをimport serialでインポートできるように、 Pythonのパッケージ名とモジュール名は別々に設定できます。 しかし、いざ、自分で設定してみようとしたら、少し躓いたので紹介します。

[build-system]hatchlingを指定し、 [tool.hatch.build.targets.wheel.packages]を設定する必要がありました。