Skip to content

Dependency configuration依存関係の設定


Project dependencies are defined with PEP 508 strings using optional PEP 440 version specifiers.プロジェクトの依存関係は、オプションのPEP 440 バージョン指定子を使用して、PEP 508 文字列で定義されます。

Version specifiersバージョン指定子

A version specifier consists of a series of version clauses, separated by commas. For example:バージョン指定子は、カンマで区切られた一連のバージョン条項で構成されます。例えば:

[project]
...
dependencies = [
  "cryptography",
  "click>=7, <9, != 8.0.0",
  "python-dateutil==2.8.*",
  "numpy~=1.21.4",
]

The comma is equivalent to a logical AND operator: a candidate version must match all given version clauses in order to match the specifier as a whole.カンマは論理AND演算子に相当します: 候補バージョンは、指定子全体に一致するために、すべての指定されたバージョン条項に一致する必要があります。

Operators演算子

Operators演算子 Function関数
~= Compatible release互換性のあるリリース
== Version matchingバージョンの一致
!= Version exclusionバージョンの除外
<=, >=<=, >= Inclusive ordered comparison包括的な順序比較
<, ><, > Exclusive ordered comparison排他的な順序比較
====== Arbitrary equality任意の等価性

Version matchingバージョンマッチング

A version matching clause includes the version matching operator == and a version identifier.バージョンマッチング句には、バージョンマッチングオペレーター==とバージョン識別子が含まれます。

By default, the version matching operator is based on a strict equality comparison: the specified version must be exactly the same as the requested version.デフォルトでは、バージョンマッチングオペレーターは厳密な等価比較に基づいています:指定されたバージョンは要求されたバージョンと完全に同じでなければなりません。

Clause Allowed versions許可されたバージョン
==1==1 1.0.0
==1.2==1.2 1.2.0

Prefix matching may be requested instead of strict comparison, by appending a trailing .* to the version identifier in the version matching clause. This means that additional trailing segments will be ignored when determining whether or not a version identifier matches the clause.厳密な比較の代わりにプレフィックスマッチングを要求することができ、バージョンマッチング句のバージョン識別子に末尾の.*を追加します。これは、バージョン識別子が句に一致するかどうかを判断する際に、追加の末尾セグメントが無視されることを意味します。

Clause Allowed versions許可されたバージョン
==1.*==1.* >=1.0.0, <2.0.0>=1.0.0, <2.0.0
==1.2.*==1.2.* >=1.2.0, <1.3.0>=1.2.0, <1.3.0

Compatible release互換性のあるリリース

A compatible release clause consists of the compatible release operator ~= and a version identifier. It matches any candidate version that is expected to be compatible with the specified version.互換性のあるリリース条項は、互換性のあるリリース演算子~=とバージョン識別子で構成されています。これは、指定されたバージョンと互換性があると予想される任意の候補バージョンに一致します。

For a given release identifier V.N, the compatible release clause is approximately equivalent to the following pair of comparison clauses:特定のリリース識別子V.Nに対して、互換性のあるリリース条項は、次の比較条項のペアにほぼ相当します:

>= V.N, == V.*

This operator cannot be used with a single segment version number such as ~=1.この演算子は、~=1のような単一セグメントのバージョン番号では使用できません。

Clause条項 Allowed versions許可されたバージョン
~=1.2~=1.2 >=1.2.0, <2.0.0>=1.2.0, <2.0.0
~=1.2.3~=1.2.3 >=1.2.3, <1.3.0>=1.2.3, <1.3.0

Version exclusionバージョン除外

A version exclusion clause includes the version exclusion operator != and a version identifier.バージョン除外条項には、バージョン除外演算子 != とバージョン識別子が含まれます。

The allowed version identifiers and comparison semantics are the same as those of the Version matching operator, except that the sense of any match is inverted.許可されるバージョン識別子と比較の意味は、バージョンマッチング 演算子のものと同じですが、マッチの意味が反転します。

Ordered comparison順序付き比較

Inclusive comparisons allow for the version identifier part of clauses whereas exclusive comparisons do not. For example, >=1.2 allows for version 1.2.0 while >1.2 does not.包括的比較は、条項のバージョン識別子部分を許可しますが、排他的比較は許可しません。例えば、>=1.2 はバージョン 1.2.0 を許可しますが、>1.2 は許可しません。

Unlike the inclusive ordered comparisons <= and >=, the exclusive ordered comparisons < and > specifically exclude pre-releases, post-releases, and local versions of the specified version.包括的な順序付き比較 <=>= とは異なり、排他的な順序付き比較 <> は、指定されたバージョンのプレリリース、ポストリリース、およびローカルバージョンを特に除外します。

Arbitrary equality任意の等価性

Though heavily discouraged, arbitrary equality comparisons allow for simple string matching without any version semantics, for example ===foobar.強く推奨されてはいませんが、任意の等価性比較は、バージョンセマンティクスなしで単純な文字列マッチングを可能にします。例えば、===foobar

Environment markers環境マーカー

Environment markers allow for dependencies to only be installed when certain conditions are met.環境マーカーは、特定の条件が満たされたときにのみ依存関係をインストールできるようにします。

For example, if you need to install the latest version of cryptography that is available for a given Python major version you could define the following:例えば、特定のPythonメジャーバージョンで利用可能な最新のcryptographyをインストールする必要がある場合、次のように定義できます:

cryptography==3.3.2; python_version < "3"
cryptography>=35.0; python_version > "3"

Alternatively, if you only need it on Python 3 when running on Windows you could do:または、Windows上で実行しているときにPython 3でのみ必要な場合は、次のようにできます:

cryptography; python_version ~= "3.0" and platform_system == "Windows"

The available environment markers are as follows.利用可能な環境マーカーは次のとおりです。

Markerマーカー Python equivalentPythonの同等物 Examples
os_nameos_name import os
os.name
import os
os.name
  • posixposix
  • javajava
sys_platformsys_platform import sys
sys.platform
import sys
sys.platform
  • linuxlinux
  • win32win32
  • darwindarwin
platform_machineplatform_machine import platform
platform.machine()
import platform
platform.machine()
  • x86_64x86_64
platform_python_implementationplatform_python_implementation import platform
platform.python_implementation()
import platform
platform.python_implementation()
  • CPythonCPython
  • JythonJython
platform_releaseplatform_release import platform
platform.release()
import platform
platform.release()
  • 1.8.0_511.8.0_51
  • 3.14.1-x86_64-linode393.14.1-x86_64-linode39
platform_systemplatform_system import platform
platform.system()
import platform
platform.system()
  • LinuxLinux
  • WindowsWindows
  • Darwinダーウィン
platform_versionplatform_version import platform
platform.version()
import platform
platform.version()
  • 10.0.19041
  • #1 SMP Fri Apr 2 22:23:49 UTC 2021#1 SMP 2021年4月2日金曜日 22:23:49 UTC
python_versionpython_version import platform
'.'.join(platform.python_version_tuple()[:2])
import platform
'.'.join(platform.python_version_tuple()[:2])
  • 2.7
  • 3.10
python_full_versionpython_full_version import platform
platform.python_version()
import platform
platform.python_version()
  • 2.7.18
  • 3.11.0b13.11.0b1
implementation_nameimplementation_name import sys
sys.implementation.name
import sys
sys.implementation.name
  • cpythoncpython
implementation_versionimplementation_version See hereこちらを参照してください here
  • 2.7.18
  • 3.11.0b13.11.0b1

Features機能

You can select groups of optional dependencies to install using the extras syntax. For example, if a dependency named foo defined the following:オプションの依存関係のグループを選択してインストールするには、extras構文を使用できます。たとえば、fooという名前の依存関係が次のように定義されている場合:

[project.optional-dependencies]
crypto = [
  "PyJWT",
  "cryptography",
]
fastjson = [
  "orjson",
]
cli = [
  "prompt-toolkit",
  "colorama; platform_system == 'Windows'",
]

You can select the cli and crypto features like so:次のようにclicryptoの機能を選択できます:

foo[cli,crypto]==1.*

Note that the features come immediately after the package name, before any version specifiers.機能はパッケージ名のすぐ後に来ることに注意してください。バージョン指定子の前に。

Self-referential自己参照

Feature groups can self-referentially extend others. For example, for a project called awesome-project, the dev feature group in the following pyproject.toml file would select everything in the crypto feature group, plus black:フィーチャーグループは他のグループを自己参照的に拡張できます。例えば、awesome-projectというプロジェクトの場合、以下のpyproject.tomlファイルのdevフィーチャーグループは、cryptoフィーチャーグループ内のすべてのものと、blackを選択します。

[project]
name = "awesome-project"

[project.optional-dependencies]
crypto = [
  "PyJWT",
  "cryptography",
]
dev = [
  "awesome-project[crypto]",
  "black",
]

Direct references直接参照

Instead of using normal version specifiers and fetching packages from an index like PyPI, you can define exact sources using direct references with an explicit URI.通常のバージョン指定子を使用して、PyPIのようなインデックスからパッケージを取得する代わりに、直接参照を使用して、明示的なURIを定義できます。

Direct references are usually not meant to be used for dependencies of a published project but rather are used for defining dependencies for an environment.直接参照は、公開プロジェクトの依存関係として使用することを意図していないことが多く、むしろ環境の依存関係を定義するために使用されます。

All direct reference types are prefixed by the package name like:すべての直接参照タイプは、パッケージ名でプレフィックスされます:

<NAME> @ <REFERENCE>

Version control systemsバージョン管理システム

Various version control systems (VCS) are supported as long as the associated executable is available along your PATH.さまざまなバージョン管理システム(VCS)は、関連する実行可能ファイルがPATHに沿って利用可能である限り、サポートされています

VCS direct references are defined using one of the following formats:VCSの直接参照は、以下のいずれかの形式を使用して定義されます:

<NAME> @ <SCHEME>://<PATH>
<NAME> @ <SCHEME>://<PATH>@<REVISION>

You may also append a #subdirectory=<PATH> component for specifying the relative path to the Python package when it is not located at the root e.g. #subdirectory=lib/foo.ルートにないPythonパッケージへの相対パスを指定するために、#subdirectory=<PATH>コンポーネントを追加することもできます。例えば、#subdirectory=lib/fooのように。

For more information, refer to this.詳細については、こちらを参照してください。

Supported VCSサポートされているVCS

Executable実行可能 Schemesスキーム Revisionsリビジョン Example
gitgit
  • git+filegit+file
  • git+httpsgit+https
  • git+sshgit+ssh
  • git+http ⚠git+http ⚠
  • git+git ⚠git+git ⚠
  • git ⚠git ⚠
  • Commit hashコミットハッシュ
  • Tag nameタグ名
  • Branch nameブランチ名
proj @ git+https://github.com/org/proj.git@v1proj @ git+https://github.com/org/proj.git@v1
Executable実行可能ファイル Schemesスキーム Revisionsリビジョン Example
hg
  • hg+filehg+file
  • hg+httpshg+https
  • hg+sshhg+ssh
  • hg+http ⚠hg+http ⚠
  • hg+static-http ⚠hg+static-http ⚠
  • Revision hashリビジョンハッシュ
  • Revision numberリビジョン番号
  • Tag nameタグ名
  • Branch nameブランチ名
proj @ hg+file:///path/to/proj@v1proj @ hg+file:///path/to/proj@v1
Executable実行可能ファイル Schemesスキーム Revisionsリビジョン Example
svnsvn
  • svn+httpssvn+https
  • svn+sshsvn+ssh
  • svn+http ⚠svn+http ⚠
  • svn+svn ⚠svn+svn ⚠
  • svn ⚠svn ⚠
  • Revision numberリビジョン番号
proj @ svn+file:///path/to/projproj @ svn+file:///path/to/proj
Executable実行可能 Schemesスキーム Revisionsリビジョン Example
bzrbzr
  • bzr+httpsbzr+https
  • bzr+sshbzr+ssh
  • bzr+sftpbzr+sftp
  • bzr+lpbzr+lp
  • bzr+http ⚠bzr+http ⚠
  • bzr+ftp ⚠bzr+ftp ⚠
  • Revision numberリビジョン番号
  • Tag nameタグ名
proj @ bzr+lp:proj@v1proj @ bzr+lp:proj@v1

Localローカル

You can install local packages with the file scheme in the following format:次の形式でfileスキームを使用してローカルパッケージをインストールできます:

<NAME> @ file://<HOST>/<PATH>

The <HOST> is only used on Windows systems, where it can refer to a network share. If omitted it is assumed to be localhost and the third slash must still be present.<HOST>はWindowsシステムでのみ使用され、ネットワーク共有を指すことがあります。省略した場合はlocalhostと見なされ、3つ目のスラッシュは依然として必要です。

The <PATH> can refer to a source archive, a wheel, or a directory containing a Python package.<PATH>はソースアーカイブ、ホイール、またはPythonパッケージを含むディレクトリを指すことができます。

Type UnixUnix WindowsWindows
Source archiveソースアーカイブ proj @ file:///path/to/pkg.tar.gzproj @ file:///path/to/pkg.tar.gz proj @ file:///c:/path/to/pkg.tar.gzproj @ file:///c:/path/to/pkg.tar.gz
Wheel車輪 proj @ file:///path/to/pkg.whlproj @ file:///path/to/pkg.whl proj @ file:///c:/path/to/pkg.whlproj @ file:///c:/path/to/pkg.whl
Directoryディレクトリ proj @ file:///path/to/pkgproj @ file:///path/to/pkg proj @ file:///c:/path/to/pkgproj @ file:///c:/path/to/pkg

Tipヒント

You may also specify paths relative to your project's root directory on all platforms by using context formatting:すべてのプラットフォームでプロジェクトのルートディレクトリに対して相対パスを指定することもできます。コンテキストフォーマットを使用してください:

<NAME> @ {root:uri}/pkg_inside_project
<NAME> @ {root:parent:uri}/pkg_alongside_project

Remoteリモート

You can install source archives and wheels by simply referring to a URL:URLを参照するだけでソースアーカイブとホイールをインストールできます:

black @ https://github.com/psf/black/archive/refs/tags/21.10b0.zip
pytorch @ https://download.pytorch.org/whl/cu102/torch-1.10.0%2Bcu102-cp39-cp39-linux_x86_64.whl

An expected hash value may be specified by appending a #<HASH_ALGORITHM>=<EXPECTED_HASH> component:期待されるハッシュ値は、#<HASH_ALGORITHM>=<EXPECTED_HASH>コンポーネントを追加することで指定できます:

requests @ https://github.com/psf/requests/archive/refs/tags/v2.26.0.zip#sha256=eb729a757f01c10546ebd179ae2aec852dd0d7f8ada2328ccf4558909d859985

If the hash differs from the expected hash, the installation will fail.ハッシュが期待されるハッシュと異なる場合、インストールは失敗します。

It is recommended that only hashes which are unconditionally provided by the latest version of the standard library's hashlib module be used for hashes. As of Python 3.10, that list consists of:ハッシュには、標準ライブラリの最新バージョンのhashlibモジュールによって無条件に提供されるハッシュのみを使用することを推奨します。Python 3.10の時点で、そのリストは次の通りです:

  • md5md5
  • sha1sha1
  • sha224sha224
  • sha256sha256
  • sha384sha384
  • sha512sha512
  • blake2bblake2b
  • blake2sblake2s

Complex syntax複雑な構文

The following is an example that uses features and environment markers:以下は、featuresenvironment markers を使用した例です:

pkg[feature1,feature2] @ <REFERENCE> ; python_version < "3.7"

Note that the space before the semicolon is required.セミコロンの前のスペースは必須です。