Skip to content

Support environment/file-based default Python version in python.toolchain #2587

Closed
@vonschultz

Description

@vonschultz

🚀 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 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions