11# Usage
22
3- ## at build time
3+ ## At build time
44
55The preferred way to configure ` setuptools-scm ` is to author
66settings in the ` tool.setuptools_scm ` section of ` pyproject.toml ` .
@@ -25,7 +25,9 @@ that support PEP 518 ([pip](https://pypi.org/project/pip) and
2525[ pep517] ( https://pypi.org/project/pep517/ ) ).
2626Tools that still invoke ` setup.py ` must ensure build requirements are installed
2727
28- ### version files
28+ ### Version files
29+
30+ Version files can be created with the `` version_file `` directive.
2931
3032``` toml title="pyproject.toml"
3133...
@@ -34,15 +36,13 @@ version_file = "pkg/_version.py"
3436```
3537Where `` pkg `` is the name of your package.
3638
39+ Unless the small overhead of introspecting the version at runtime via
40+ ` importlib.metadata ` is a concern or you need a version file in an
41+ alternative format such as plain-text (see `` version_file_template `` )
42+ you most likely do _ not_ need to write a separate version file; see
43+ the runtime discussion below for more details.
3744
38- ``` commandline
39- $ python -m setuptools_scm
40-
41- # To explore other options, try:
42- $ python -m setuptools_scm --help
43- ```
44-
45- ## as cli tool
45+ ## As cli tool
4646
4747If you need to confirm which version string is being generated
4848or debug the configuration, you can install
@@ -65,46 +65,31 @@ $ python -m setuptools_scm ls # output trimmed for brevity
6565...
6666```
6767
68- !!! note "committed files only"
68+ !!! note "Committed files only"
6969
7070 currently only committed files are listed, this might change in the future
7171
7272!!! warning "sdists/archives don't provide file lists"
7373
74- currently there is no builtin mechanism
75- to safely transfer the file lists to sdists or obtaining them from archives
76- coordination for setuptools and hatch is ongoing
77-
78- ## at runtime (strongly discouraged)
79-
80- the most simple ** looking** way to use ` setuptools-scm ` at runtime is:
81-
82- ``` python
83- from setuptools_scm import get_version
84- version = get_version()
85- ```
86-
87-
88- In order to use ` setuptools-scm ` from code that is one directory deeper
89- than the project's root, you can use:
90-
91- ``` python
92- from setuptools_scm import get_version
93- version = get_version(root = ' ..' , relative_to = __file__ )
94- ```
95-
96-
97- ## Python package metadata
74+ Currently there is no builtin mechanism
75+ to safely transfer the file lists to sdists or obtaining them from archives.
76+ Coordination for setuptools and hatch is ongoing.
9877
78+ To explore other options, try
9979
80+ ``` commandline
81+ $ python -m setuptools_scm --help
10082
83+ ## At runtime
10184
102- ### version at runtime
85+ ### Python Metadata
10386
104- If you have opted not to hardcode the version number inside the package,
105- you can retrieve it at runtime from [ PEP-0566] ( https://www.python.org/dev/peps/pep-0566/ ) metadata using
87+ The standard method to retrieve the version number at runtime is via
88+ [PEP-0566](https://www.python.org/dev/peps/pep-0566/) metadata using
10689``importlib.metadata`` from the standard library (added in Python 3.8)
107- or the [ ` importlib_metadata ` ] ( https://pypi.org/project/importlib-metadata/ ) backport:
90+ or the
91+ [`importlib_metadata`](https://pypi.org/project/importlib-metadata/)
92+ backport for earlier versions:
10893
10994```python title="package_name/__init__.py"
11095from importlib.metadata import version, PackageNotFoundError
@@ -116,6 +101,40 @@ except PackageNotFoundError:
116101 pass
117102```
118103
104+ ### Via your version file
105+
106+ If you have opted to create a Python version file via the standard
107+ template, you can import that file, where you will have a `` version ``
108+ string and a `` version_tuple `` tuple with elements corresponding to
109+ the version tags.
110+
111+ ``` python title="Using package_name/_version.py"
112+ import package_name._version as v
113+
114+ print (v.version)
115+ print (v.version_tuple)
116+ ```
117+
118+ ### Via setuptools_scm (strongly discouraged)
119+
120+ While the most simple ** looking** way to use ` setuptools_scm ` at
121+ runtime is:
122+
123+ ``` python
124+ from setuptools_scm import get_version
125+ version = get_version()
126+ ```
127+
128+ it is strongly discouraged to call directly into ` setuptools_scm ` over
129+ the standard Python ` importlib.metadata ` .
130+
131+ In order to use ` setuptools_scm ` from code that is one directory deeper
132+ than the project's root, you can use:
133+
134+ ``` python
135+ from setuptools_scm import get_version
136+ version = get_version(root = ' ..' , relative_to = __file__ )
137+ ```
119138
120139### Usage from Sphinx
121140
@@ -132,7 +151,7 @@ the working directory for good reasons and using the installed metadata
132151prevents using needless volatile data there.
133152
134153
135- ## with Docker/Podman
154+ ### With Docker/Podman
136155
137156
138157In some situations, Docker may not copy the ` .git ` into the container when
0 commit comments