Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark, in light modeAuto light/dark, in dark modeSkip to content
setuptools 82.0.0 documentation
Logo

Links

Project

Back to top

Configuring setuptools usingpyproject.toml files

Note

New in 61.0.0

Important

If compatibility with legacy builds or versions of tools that don’t supportcertain packaging standards (e.g.PEP 517 orPEP 660), a simplesetup.pyscript can be added to your project[1](while keeping the configuration inpyproject.toml):

fromsetuptoolsimportsetupsetup()

Starting withPEP 621, the Python community selectedpyproject.toml asa standard way of specifyingproject metadata.Setuptools has adopted this standard and will use the information containedin this file as an input in the build process.

The example below illustrates how to write apyproject.toml file that canbe used withsetuptools. It contains two TOML tables (identified by the[table-header] syntax):build-system andproject.Thebuild-system table is used to tell the build frontend (e.g.build orpip) to usesetuptools and any other plugins (e.g.setuptools-scm) to build the package.Theproject table contains metadata fields as described by theWriting your pyproject.toml guide.

[build-system]requires=["setuptools","setuptools-scm"]build-backend="setuptools.build_meta"[project]name="my_package"authors=[{name="Josiah Carberry",email="josiah_carberry@brown.edu"},]description="My package description"readme="README.rst"requires-python=">=3.8"keywords=["one","two"]license="BSD-3-Clause"classifiers=["Framework :: Django","Programming Language :: Python :: 3",]dependencies=["requests",'importlib-metadata; python_version<"3.10"',]dynamic=["version"][project.optional-dependencies]pdf=["ReportLab>=1.2","RXP"]rest=["docutils>=0.3","pack ==1.1, ==1.3"][project.scripts]my-script="my_package.module:function"# ... other project metadata fields as listed in:#     https://packaging.python.org/en/latest/guides/writing-pyproject-toml/

Important

Support forproject.license-filesand SPDX license expressions inproject.license (PEP 639)were introduced in version 77.0.0.

Setuptools-specific configuration

While the standardproject table in thepyproject.toml file covers mostof the metadata used during the packaging process, there are still somesetuptools-specific configurations that can be set by users that requirecustomization.These configurations are completely optional and probably can be skipped whencreating simple packages.They are equivalent to theKeywords used by thesetup.pyfile, and can be set via thetool.setuptools table:

Key

Value Type (TOML)

Notes

py-modules

array

See tip below.

ext-modules

array oftables/inline-tables

Experimental - Each item corresponds to asetuptools.Extension object and may definethe associated parameters inkebab-case.SeeBuilding Extension Modules.

packages

array orfind directive

See tip below.

package-dir

table/inline-table

Used when explicitly/manually listingpackages.

package-data

table/inline-table

SeeData Files Support.

include-package-data

boolean

True by default (only when usingpyproject.toml project metadata/config).SeeData Files Support.

exclude-package-data

table/inline-table

Empty by default. SeeData Files Support.

license-files

array of glob patterns

Deprecated - useproject.license-files instead. SeeWriting your pyproject.toml(by default:['LICEN[CS]E*','COPYING*','NOTICE*','AUTHORS*'])

data-files

table/inline-table

Discouraged - checkData Files Support.Whenever possible, consider using data files inside the package directories.

script-files

array

Discouraged - equivalent to thescript keyword insetup.py.Whenever possible, please useproject.scripts instead.

provides

array

ignored by pip when installing packages

obsoletes

array

ignored by pip when installing packages

platforms

array

Sets thePlatformcore-metadata field(ignored by pip when installing packages).

zip-safe

boolean

Obsolete - only relevant forpkg_resources,easy_install andsetup.pyinstallin the context ofeggs (deprecated).

eager-resources

array

Obsolete - only relevant forpkg_resources,easy_install andsetup.pyinstallin the context ofeggs (deprecated).

namespace-packages

array

Deprecated - use implicit namespaces instead (PEP 420).

Note

TheTOML value typesarray andtable/inline-table are roughlyequivalent to the Python’slist anddict data types, respectively.

Please note that some of these configurations are deprecated, obsolete or at leastdiscouraged, but they are made available to ensure portability.Deprecated and obsolete configurations may be removed in future versions ofsetuptools.New packages should avoid relying on discouraged fields if possible, andexisting packages should consider migrating to alternatives.

Tip

When bothpy-modules andpackages are left unspecified,setuptools will attempt to performAutomatic discovery, which shouldcover most popular project directory organization techniques, such as thesrc-layout and theflat-layout.

However if your project does not follow these conventional layouts(e.g. you want to use aflat-layout but at the same time have customdirectories at the root of your project), you might need to use thefinddirective[3] as shown below:

[tool.setuptools.packages.find]where=["src"]# list of folders that contain the packages (["."] by default)include=["my_package*"]# package names should match these glob patterns (["*"] by default)exclude=["my_package.tests*"]# exclude packages matching these glob patterns (empty by default)namespaces=false# to disable scanning PEP 420 namespaces (true by default)

Note that the glob patterns in the example above need to be matchedby theentire package name. This means that if you specifyexclude=["tests"],modules liketests.my_package.test1 will still be included in the distribution(to remove them, add a wildcard to the end of the pattern:"tests*").

Alternatively, you can explicitly list the packages in modules:

[tool.setuptools]packages=["my_package"]

If you want to publish a distribution that does not include any Python module(e.g. a “meta-distribution” that just aggregate dependencies), pleaseconsider something like the following:

[tool.setuptools]packages=[]

Dynamic Metadata

Note that in the first example of this page we usedynamic to identifywhich metadata fields are dynamically computed during the build by eithersetuptools itself or the plugins installed viabuild-system.requires(e.g.setuptools-scm is capable of deriving the current project versiondirectly from thegitversion control system).

Currently the following fields can be listed as dynamic:version,classifiers,description,entry-points,scripts,gui-scripts andreadme.When these fields are expected to be provided bysetuptools acorresponding entry is required in thetool.setuptools.dynamic table[2]. For example:

# ...[project]name="my_package"dynamic=["version","readme"]# ...[tool.setuptools.dynamic]version={attr="my_package.__version__"}# any module attribute compatible with ast.literal_evalreadme={file=["README.rst","USAGE.rst"]}

In thedynamic table, theattr directive[3] will read anattribute from the given module[4], whilefile will read the contentsof all given files and concatenate them in a single string.

Key

Directive

Notes

version

attr,file

readme

file

Here you can also set"content-type":

readme={file=["README.txt","USAGE.txt"],content-type="text/plain"}

Ifcontent-type is not given,"text/x-rst" is used by default.

description

file

One-line text (no line breaks)

classifiers

file

Multi-line text with one classifier per line

entry-points

file

INI format followingEntry points specification(console_scripts andgui_scripts can be included)

dependencies

file

subset of therequirements.txt format(# comments and blank lines excluded)BETA

optional-dependencies

file

subset of therequirements.txt format per group(# comments and blank lines excluded)BETA

Supportingfile for dependencies is meant for a convenience for packagingapplications with possibly strictly versioned dependencies.

Library packagers are discouraged from using overly strict (or “locked”)dependency versions in theirdependencies andoptional-dependencies.

Currently, when specifyingoptional-dependencies dynamically, all of the groupsmust be specified dynamically; one can not specify some of them statically andsome of them dynamically.

Also note that the file format for specifying dependencies resembles arequirements.txt file,however please keep in mind that all non-comment lines must conform withPEP 508(pip specific syntaxes, e.g.-c/-r/-e and other flags, are not supported).

Note

If you are using an old version ofsetuptools, you might need to ensurethat all files referenced by thefile directive are included in thesdist(you can do that viaMANIFEST.in or using plugins such assetuptools-scm,please have a look onControlling files in the distribution for more information).

Changed in version 66.1.0:Newer versions ofsetuptools will automatically add these files to thesdist.

It is advisable to use literal values together withattr (e.g.str,tuple[str], seeast.literal_eval()). This is recommendin order to support the common case of a literal value assigned to a variablein a module containing (directly or indirectly) third-party imports.

attr first tries to read the value from the module by examining themodule’s AST. If that fails,attr falls back to importing the module,usingimportlib.util.spec_from_file_location() recommended recipe(seeexample on Python docsabout “Importing a source file directly”).Note however that importing the module is error prone since your package isnot installed yet. You may also need to manually add the project directory tosys.path (viasetup.py) in order to be able to do that.


Notes

[1]

pip may allow editable install only withpyproject.tomlandsetup.cfg. However, this behavior may not be consistent over variouspipversions and other packaging-related tools(setup.py is more reliable on those scenarios).

[2]

Dynamicscripts andgui-scripts are a special case.When resolving these metadata keys,setuptools will look fortool.setuptools.dynamic.entry-points, and use the values of theconsole_scripts andgui_scriptsentry-point groups.

[3](1,2)

In the context of this document,directives are special TOMLvalues that are interpreted differently bysetuptools (usually triggering anassociated function). Most of the times they correspond to a special TOML table(or inline-table) with a single top-level key.For example, you can have the{find={where=["src"],exclude=["tests*"]}}directive fortool.setuptools.packages, or{attr="mymodule.attr"}directive fortool.setuptools.dynamic.version.

[4]

attr is meant to be used when the module attribute is staticallyspecified (e.g. as a string). As a rule of thumb, theattribute should be able to be parsed withast.literal_eval(), andshould not be modified or re-assigned.

On this page

[8]ページ先頭

©2009-2026 Movatter.jp