Skip to content

Declaring dependencies依存関係の宣言

It is best practice to declare dependencies in a static file instead of modifying environments with ad-hoc installations. Once dependencies are defined, they can be locked to create a consistent, reproducible environment.依存関係は、アドホックインストールで環境を変更するのではなく、静的ファイルに宣言するのがベストプラクティスです。依存関係が定義されると、それらはロックされ、一貫性のある再現可能な環境を作成できます。

Using pyproject.toml pyproject.tomlの使用

The pyproject.toml file is the Python standard for defining configuration for a project.pyproject.tomlファイルは、プロジェクトの設定を定義するためのPythonの標準です。

To define project dependencies in a pyproject.toml file:pyproject.tomlファイルにプロジェクトの依存関係を定義するには:

pyproject.toml
[project]
dependencies = [
  "httpx",
  "ruff>=0.3.0"
]

To define optional dependencies in a pyproject.toml file:pyproject.tomlファイルにオプションの依存関係を定義するには:

pyproject.toml
[project.optional-dependencies]
cli = [
  "rich",
  "click",
]

Each of the keys defines an "extra", which can be installed using the --extra and --all-extras flags or package[<extra>] syntax. See the documentation on installing packages for more details.各キーは「extra」を定義しており、--extraおよび--all-extrasフラグまたはpackage[<extra>]構文を使用してインストールできます。詳細については、パッケージのインストールに関するドキュメントを参照してください。

See the official pyproject.toml guide for more details on getting started with a pyproject.toml.公式のpyproject.tomlガイドを参照して、pyproject.tomlの使い始めに関する詳細を確認してください。

Using requirements.inrequirements.inの使用

It is also common to use a lightweight requirements.txt format to declare the dependencies for the project. Each requirement is defined on its own line. Commonly, this file is called requirements.in to distinguish it from requirements.txt which is used for the locked dependencies.プロジェクトの依存関係を宣言するために、軽量のrequirements.txt形式を使用することも一般的です。各要件は独自の行で定義されます。一般的に、このファイルはロックされた依存関係に使用されるrequirements.txtと区別するためにrequirements.inと呼ばれます。

To define dependencies in a requirements.in file:requirements.inファイルで依存関係を定義するには:

requirements.in
httpx
ruff>=0.3.0

Optional dependencies groups are not supported in this format.この形式ではオプションの依存関係グループはサポートされていません。