You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
python.toolchain
where python = use_extension("@rules_python//python/extensions:python.bzl", "python")
Description
It should be possible to set the default Python version using a file or an environment variable.
In python.toolchain you need to set the is_default attribute. You can have several Python versions defined, and for one of them you need to set is_default = True. Since a MODULE.bazel file cannot read an environment variable or read a file, you need to set the default in MODULE.bazel. Before Bzlmod, you could simply load a file created by a repo rule and have access to the value of an environment variable or the contents of a .python-version file in the WORKSPACE.bazel file, and set the default_version in a call to python_register_multi_toolchains accordingly.
This is important for interoperability in projects where not everything is run through Bazel; pyenv for example uses the PYENV_VERSION environment variable, falling back on the .python-version file if the environment variable is not set.
Describe the solution you'd like
A new attribute for python.toolchain called e.g. default_python_version_file, which can be the label of a file generated by a repository rule, or a file in the current repo. If the python_version attribute of python.toolchain matches the contents of the file, treat it as the default (as if is_default = True).
Describe alternatives you've considered
One could imagine having both an attribute for setting the default Python version with a file and another attribute for setting the default Python version with an environment variable, which would lower the threshold for any user wanting to react to an environment variable, at the cost of a more complex implementation in rules_python. Since the same functionality can be achieved with a repository rule, I'm leaning towards telling users to write a repository rule.
One could also imagine having the default Python version be defined in MODULE.bazel, as the source of truth, and have something like write_source_file update a .python-version file in the repo for compatibility with other tools. But done this way, there's no way to support environment variables as a way of setting the default Python version.
The text was updated successfully, but these errors were encountered:
vonschultz
added a commit
to vonschultz/rules_python
that referenced
this issue
Jan 28, 2025
This change adds a new `default_version_file` attribute to
`python.toolchain`. If set, the toolchain compares the file's contents
to its `python_version`, and if they match, treats that toolchain as
default (ignoring `is_default`). This allows Bazel to synchronize the
default Python version with external tools (e.g., pyenv) that use
a `.python-version` file or environment variables.
Fixesbazelbuild#2587.
🚀 feature request
Relevant Rules
python.toolchain
where
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
Description
It should be possible to set the default Python version using a file or an environment variable.
In
python.toolchain
you need to set theis_default
attribute. You can have several Python versions defined, and for one of them you need to setis_default = True
. Since aMODULE.bazel
file cannot read an environment variable or read a file, you need to set the default inMODULE.bazel
. Before Bzlmod, you could simply load a file created by a repo rule and have access to the value of an environment variable or the contents of a.python-version
file in theWORKSPACE.bazel
file, and set thedefault_version
in a call topython_register_multi_toolchains
accordingly.This is important for interoperability in projects where not everything is run through Bazel;
pyenv
for example uses thePYENV_VERSION
environment variable, falling back on the.python-version
file if the environment variable is not set.Describe the solution you'd like
A new attribute for
python.toolchain
called e.g.default_python_version_file
, which can be the label of a file generated by a repository rule, or a file in the current repo. If thepython_version
attribute ofpython.toolchain
matches the contents of the file, treat it as the default (as ifis_default = True
).Describe alternatives you've considered
One could imagine having both an attribute for setting the default Python version with a file and another attribute for setting the default Python version with an environment variable, which would lower the threshold for any user wanting to react to an environment variable, at the cost of a more complex implementation in rules_python. Since the same functionality can be achieved with a repository rule, I'm leaning towards telling users to write a repository rule.
One could also imagine having the default Python version be defined in
MODULE.bazel
, as the source of truth, and have something likewrite_source_file
update a.python-version
file in the repo for compatibility with other tools. But done this way, there's no way to support environment variables as a way of setting the default Python version.The text was updated successfully, but these errors were encountered: