//python/extensions:python.bzl
Python toolchain module extensions for use with bzlmod.
Basic usage
The simplest way to configure the toolchain withrules_python is as follows.
python=use_extension("@rules_python//python/extensions:python.bzl","python")python.defaults(python_version="3.11")python.toolchain(python_version="3.11")use_repo(python,"python_3_11")
See also
For more in-depth documentation see thepython.toolchain.
Overrides
Overrides can be done at 3 different levels:
Overrides affecting all python toolchain versions on all platforms -
python.override.Overrides affecting a single toolchain versions on all platforms -
python.single_version_override.Overrides affecting a single toolchain versions on a single platforms -
python.single_version_platform_override.
See also
The main documentation page on registeringtoolchains.
- moduleextpython
Bzlmod extension that is used to register Python toolchains.
- python.defaults(python_version='',python_version_env='',python_version_file=None)
Tag class to specify the default Python version.
- Attributes:
python_version–(str)(default“”)String saying what the default Python version should be. If the stringmatches the
python_versionattribute of a toolchain, thistoolchain is the default version. If this attribute is set, theis_defaultattribute of the toolchain is ignored.Added in version 1.4.0.
optional
python_version_env–(str)(default“”)Environment variable saying what the default Python version should be.If the string matches the
python_versionattribute of atoolchain, this toolchain is the default version. If this attribute isset, theis_defaultattribute of the toolchain is ignored.Added in version 1.4.0.
optional
python_version_file–(label)(defaultNone)File saying what the default Python version should be. If the contentsof the file match the
python_versionattribute of a toolchain,this toolchain is the default version. If this attribute is set, theis_defaultattribute of the toolchain is ignored.Added in version 1.4.0.
optional
- python.override(auth_patterns={},available_python_versions=[],base_url='https://github.com/astral-sh/python-build-standalone/releases/download',ignore_root_user_error=True,minor_mapping={},netrc='',register_all_versions=False)
Tag class used to override defaults and behaviour of the module extension.
Added in version 0.36.0.
- Attributes:
auth_patterns–(dict[str,str])(default{})An optional dict mapping host names to custom authorization patterns.
If a URL’s host name is present in this dict the value will be used as a pattern whengenerating the authorization header for the http request. This enables the use of customauthorization schemes used in a lot of common cloud storage providers.
The pattern currently supports 2 tokens:
<login>and<password>, which are replaced with their equivalent valuein the netrc file for the same host name. After formatting, the result is setas the value for theAuthorizationfield of the HTTP request.Example attribute and netrc for a http download to an oauth2 enabled API using a bearer token:
auth_patterns = { "storage.cloudprovider.com": "Bearer <password>"}netrc:
machine storage.cloudprovider.com password RANDOM-TOKEN
The final HTTP request would have the following header:
Authorization: Bearer RANDOM-TOKEN
optional
available_python_versions–(list[str])(default[])The list of available python tool versions to use. Must be in
X.Y.Zformat.If the unknown version given the processing of the extension will fail - all ofthe versions in the list have to be defined withpython.single_version_overrideorpython.single_version_platform_overridebefore they are used in thislist.This attribute is usually used in order to ensure that no unexpected transitivedependencies are introduced.
optional
base_url–(str)(default“https://github.com/astral-sh/python-build-standalone/releases/download”)The base URL to be used when downloading toolchains.
optional
ignore_root_user_error–(bool)(defaultTrue)Deprecated; do not use. This attribute has no effect.
optional
minor_mapping–(dict[str,str])(default{})The mapping between
X.YtoX.Y.Zversions to be used when setting uptoolchains. It defaults to the interpreter with the highest available patchversion for each minor version. For example if one registers3.10.3,3.10.4and3.11.4then the default for theminor_mappingdict will be:{"3.10":"3.10.4","3.11":"3.11.4",}
Changed in version 0.37.0:The values in this mapping override the default values and do not replace them.
optional
Location of the .netrc file to use for authentication
optional
register_all_versions–(bool)(defaultFalse)Add all versions
optional
- python.single_version_override(python_version,distutils=None,distutils_content='',patch_strip=0,patches=[],sha256={},strip_prefix='python',urls=[])
Override single python version URLs and patches for all platforms.
Note
This will replace any existing configuration for the given python version.
Tip
If you would like to modify the configuration for a specific
(version,platform), please use thesingle_version_platform_overridetagclass.Added in version 0.36.0.
- Attributes:
The python version to override URLs for. Must be in
X.Y.Zformat.mandatory
distutils–(label)(defaultNone)A distutils.cfg file to be included in the Python installation. Either
distutilsordistutils_contentcan be specified, but not both.optional
distutils_content–(str)(default“”)A distutils.cfg file content to be included in the Python installation. Either
distutilsordistutils_contentcan be specified, but not both.optional
patch_strip–(int)(default0)Same as the –strip argument of Unix patch.
optional
patches–(list[label])(default[])A list of labels pointing to patch files to apply for the interpreter repository. They are applied in the list order and are applied before any platform-specific patches are applied.
optional
sha256–(dict[str,str])(default{})The python platform to sha256 dict. See
python.single_version_platform_override.platformfor allowed key values.optional
strip_prefix–(str)(default“python”)The ‘strip_prefix’ for the archive, defaults to ‘python’.
optional
The URL template to fetch releases for this Python version. See
python.single_version_platform_override.urlsfor documentation.optional
- python.single_version_platform_override(platform,python_version,arch='',coverage_tool=None,os_name='',patch_strip=0,patches=[],sha256='',strip_prefix='python',target_compatible_with=[],target_settings=[],urls=[])
Override single python version for a single existing platform.
If the
(version,platform)is new, we will add it to the existing versions and willuse the sameurltemplate.Tip
If you would like to add or remove platforms to a single python version toolchainconfiguration, please use
single_version_override.Added in version 0.36.0.
- Attributes:
The platform to override the values for, typically one of:
aarch64-apple-darwin-freethreadedaarch64-apple-darwinaarch64-pc-windows-msvc-freethreadedaarch64-pc-windows-msvcaarch64-unknown-linux-gnu-freethreadedaarch64-unknown-linux-gnuarmv7-unknown-linux-gnu-freethreadedarmv7-unknown-linux-gnui386-unknown-linux-gnu-freethreadedi386-unknown-linux-gnuppc64le-unknown-linux-gnu-freethreadedppc64le-unknown-linux-gnuriscv64-unknown-linux-gnu-freethreadedriscv64-unknown-linux-gnus390x-unknown-linux-gnu-freethreadeds390x-unknown-linux-gnux86_64-apple-darwin-freethreadedx86_64-apple-darwinx86_64-pc-windows-msvc-freethreadedx86_64-pc-windows-msvcx86_64-unknown-linux-gnu-freethreadedx86_64-unknown-linux-gnux86_64-unknown-linux-musl-freethreadedx86_64-unknown-linux-musl
Other values are allowed, in which case,
target_compatible_with,target_settings,os_name, andarchshould be specified so the toolchain isonly used when appropriate.Changed in version 1.5.0:Arbitrary platform strings allowed.
mandatory
The python version to override URLs for. Must be in
X.Y.Zformat.mandatory
The arch (cpu) the runtime is compatible with.
If not set, then the runtime cannot be used as a
python_X_Y_hostruntime.If set, the
os_name,target_compatible_withandtarget_settingsattributesshould also be set.The values should be one of the values in
@platforms//cpuSee also
Docs for [Registering custom runtimes]
optional
coverage_tool–(label)(defaultNone)The coverage tool to be used for a particular Python interpreter. This can override
rules_pythondefaults.optional
The host OS the runtime is compatible with.
If not set, then the runtime cannot be used as a
python_X_Y_hostruntime.If set, the
os_name,target_compatible_withandtarget_settingsattributesshould also be set.The values should be one of the values in
@platforms//osSee also
Docs for [Registering custom runtimes]
optional
patch_strip–(int)(default0)Same as the –strip argument of Unix patch.
optional
patches–(list[label])(default[])A list of labels pointing to patch files to apply for the interpreter repository. They are applied in the list order and are applied after the common patches are applied.
optional
The sha256 for the archive
optional
strip_prefix–(str)(default“python”)The ‘strip_prefix’ for the archive, defaults to ‘python’.
optional
target_compatible_with–(list[str])(default[])The
target_compatible_withvalues to use for the toolchain definition.If not set, then
os_nameandarchwill be used to populate it.If set,
target_settings,os_name, andarchshould also be set.See also
Docs for [Registering custom runtimes]
optional
target_settings–(list[str])(default[])The
target_setingsvalues to use for the toolchain definition.If set,
target_compatible_with,os_name, andarchshould also be set.See also
Docs for [Registering custom runtimes]
optional
The URL template to fetch releases for this Python version. If the URL template results in a relative fragment, default base URL is going to be used. Occurrences of
{python_version},{platform}and{build}will be interpolated based on the contents in the override and the knownplatformvalues.optional
- python.toolchain(python_version,configure_coverage_tool=False,ignore_root_user_error=True,is_default=False)
Tag class used to register Python toolchains.Use this tag class to register one or more Python toolchains. This classis also potentially called by sub modules. The following covers differentbusiness rules and use cases.
Toolchains in the Root Module
This class registers all toolchains in the root module.
Toolchains in Sub Modules
It will create a toolchain that is in a sub module, if the toolchainof the same name does not exist in the root module. The extension stops nameclashing between toolchains in the root module and toolchains in sub modules.You cannot configure more than one toolchain as the default toolchain.
Toolchain set as the default version
This extension will not create a toolchain that exists in a sub module,if the sub module toolchain is marked as the default version. If you havemore than one toolchain in your root module, you need to set one of thetoolchains as the default version. If there is only one toolchain itis set as the default toolchain.
Toolchain repository name
A toolchain’s repository name uses the format
python_{major}_{minor}, e.g.python_3_10. Themajorandminorcomponents aremajorandminorare the Python version from thepython_versionattribute.If a toolchain is registered in
X.Y.Z, then similarly the toolchain name willbepython_{major}_{minor}_{patch}, e.g.python_3_10_19.Toolchain detection
The definition of the first toolchain wins, which means that the root modulecan override settings for any python toolchain available. This relies on thedocumented module traversal from the
module_ctx.modules.Tip
In order to use a different name than the above, you can use the following
MODULE.bazelsyntax:python=use_extension("@rules_python//python/extensions:python.bzl","python")python.defaults(python_version="3.11")python.toolchain(python_version="3.11")use_repo(python,my_python_name="python_3_11")
Then the python interpreter will be available as
my_python_name.- Attributes:
The Python version, in
major.minorormajor.minor.patchformat, e.g3.12(or3.12.3), to create a toolchain for.mandatory
configure_coverage_tool–(bool)(defaultFalse)Whether or not to configure the default coverage tool provided by
rules_pythonfor the compatible toolchains.optional
ignore_root_user_error–(bool)(defaultTrue)Changed in version VERSION_NEXT_FEATURE:Noop, will be removed in the next major release.
optional
is_default–(bool)(defaultFalse)Whether the toolchain is the default version.
Changed in version 1.4.0:This setting is ignored if the default version is set using the
defaultstag class (encouraged).optional