This PEP specifies how to write a project’s dependencies in apyproject.toml file for packaging-related tools to consumeusing thefields defined in PEP 621.
Note
This PEP has been accepted and was merged intoPEP 621.
All dependency entries MUST be validPEP 508 strings.
Build backends SHOULD abort at load time for any parsing errors.
frompackaging.requirementsimportInvalidRequirement,Requirement...try:Requirement(entry)exceptInvalidRequirement:# exit
Every element MUST be anentry.
[project]dependencies=['PyYAML ~= 5.0','requests[security] < 3','subprocess32; python_version < "3.2"',]
Each key is the name of the provided option, with each value being the same type asthedependencies field i.e. an array of strings.
[project.optional-dependencies]tests=['coverage>=5.0.3','pytest','pytest-benchmark[histogram]>=3.2.1',]
This is a real-world example port of whatdocker-compose defines.
[project]dependencies=['cached-property >= 1.2.0, < 2','distro >= 1.5.0, < 2','docker[ssh] >= 4.2.2, < 5','dockerpty >= 0.4.1, < 1','docopt >= 0.6.1, < 1','jsonschema >= 2.5.1, < 4','PyYAML >= 3.10, < 6','python-dotenv >= 0.13.0, < 1','requests >= 2.20.0, < 3','texttable >= 0.9.0, < 2','websocket-client >= 0.32.0, < 1',# Conditional'backports.shutil_get_terminal_size == 1.0.0; python_version < "3.3"','backports.ssl_match_hostname >= 3.5, < 4; python_version < "3.5"','colorama >= 0.4, < 1; sys_platform == "win32"','enum34 >= 1.0.4, < 2; python_version < "3.4"','ipaddress >= 1.0.16, < 2; python_version < "3.3"','subprocess32 >= 3.5.4, < 4; python_version < "3.2"',][project.optional-dependencies]socks=['PySocks >= 1.5.6, != 1.5.7, < 2']tests=['ddt >= 1.2.2, < 2','pytest < 6','mock >= 1.0.1, < 4; python_version < "3.4"',]
frompackaging.requirementsimportInvalidRequirement,Requirementdefparse_dependencies(config):dependencies=config.get('dependencies',[])ifnotisinstance(dependencies,list):raiseTypeError('Field `project.dependencies` must be an array')fori,entryinenumerate(dependencies,1):ifnotisinstance(entry,str):raiseTypeError(f'Dependency #{i} of field `project.dependencies` must be a string')try:Requirement(entry)exceptInvalidRequirementase:raiseValueError(f'Dependency #{i} of field `project.dependencies` is invalid:{e}')returndependenciesdefparse_optional_dependencies(config):optional_dependencies=config.get('optional-dependencies',{})ifnotisinstance(optional_dependencies,dict):raiseTypeError('Field `project.optional-dependencies` must be a table')optional_dependency_entries={}foroption,dependenciesinoptional_dependencies.items():ifnotisinstance(dependencies,list):raiseTypeError(f'Dependencies for option `{option}` of field ''`project.optional-dependencies` must be an array')entries=[]fori,entryinenumerate(dependencies,1):ifnotisinstance(entry,str):raiseTypeError(f'Dependency #{i} of option `{option}` of field ''`project.optional-dependencies` must be a string')try:Requirement(entry)exceptInvalidRequirementase:raiseValueError(f'Dependency #{i} of option `{option}` of field 'f'`project.optional-dependencies` is invalid:{e}')else:entries.append(entry)optional_dependency_entries[option]=entriesreturnoptional_dependency_entries
defconstruct_metadata_file(metadata_object):""" https://packaging.python.org/specifications/core-metadata/ """metadata_file='Metadata-Version: 2.1\n'...ifmetadata_object.dependencies:# Sort dependencies to ensure reproducible buildsfordependencyinsorted(metadata_object.dependencies):metadata_file+=f'Requires-Dist:{dependency}\n'ifmetadata_object.optional_dependencies:# Sort extras and dependencies to ensure reproducible buildsforoption,dependenciesinsorted(metadata_object.optional_dependencies.items()):metadata_file+=f'Provides-Extra:{option}\n'fordependencyinsorted(dependencies):if';'independency:metadata_file+=f'Requires-Dist:{dependency} and extra == "{option}"\n'else:metadata_file+=f'Requires-Dist:{dependency}; extra == "{option}"\n'...returnmetadata_file
This document is placed in the public domain or under theCC0-1.0-Universal license, whichever is more permissive.
Source:https://github.com/python/peps/blob/main/peps/pep-0631.rst
Last modified:2025-02-01 08:55:40 GMT