Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python Version via .python-version #932

Closed
jamesward opened this issue Mar 24, 2020 · 7 comments · Fixed by #1664
Closed

Python Version via .python-version #932

jamesward opened this issue Mar 24, 2020 · 7 comments · Fixed by #1664
Assignees

Comments

@jamesward
Copy link

It doesn't seem like there is a standard way for a project to specify it's Python version, but I'm using https://github.com/pyenv/pyenv and it does this via a .python-version file. Is adding support for reading the version from that file something that would be considered?

@CaseyFaist
Copy link
Contributor

Absolutely! I can't promise when work will start on this feature specifically, but support for Poetry and using up to date python conventions is very much on my mind.

@joshfriend
Copy link
Contributor

joshfriend commented Apr 9, 2020

@jamesward have you discovered Heroku's runtime.txt file yet?

There are community buildpacks for pyenv, but pyenv compiles python from source (lengthening your slug build time) whereas this buildpack has prebuilt python binaries for common versions.

The names for various python versions differ between runtime.txt (e.g. python-3.8.0) and .python-version (just 3.8.0) because runtime.txt is used for other language buildpacks, so they are not directly equivalent or interchangeable.

@jamesward
Copy link
Author

I have seen that but it'd be nice to be able to specify the python version in just one place. :)

@abitrolly
Copy link

pyproject.toml can also be used to avoid yet another file.

There is a requires-python directive, but it is designed to describe all Python versions a package is compatible with. For the buildpack, however, it may beneficial to set specific version in [tool.buildpack]

[tool.buildpack]
python = "3.8.x"

@edmorley
Copy link
Member

edmorley commented Apr 8, 2021

I'd love to be able to add support for this in a vendor-independent manner.

Perhaps we can use requires-python and either:
(a) insist it specifies a single version (or at least using only a fairly easy to manually parse version specifier),
(b) resolve the version range ourselves (similar to what the Node buildpack does) to support all PEP440 specifiers, though we'll then have a pre-python-install-time dependency on packaging

@edmorley
Copy link
Member

edmorley commented Feb 7, 2022

I've written up my latest thoughts on ways to specify Python version for Heroku apps, here:
#913 (comment)

@edmorley edmorley self-assigned this Sep 17, 2024
edmorley added a commit that referenced this issue Oct 14, 2024
Following on from the recent Python version refactoring, this now adds
support for configuring the app's Python version using a
`.python-version` file. This file is used by several tools in the Python
ecosystem (such as pyenv, `actions/setup-python`, uv), whereas the
existing `runtime.txt` file is proprietary to Heroku.

This change is the classic Python buildpack equivalent of the Python
CNB change here:
heroku/buildpacks-python#272

If both a `runtime.txt` file and a `.python-version` file are present,
then the `runtime.txt` file will take precedence. However, use of the
`.python-version` file is now recommended, since `runtime.txt` will
be deprecated in the future. Both the `runtime.txt` file and
`.python-version` file take precedence over any Python version specified
in a `Pipfile.lock` for Pipenv users.

We support the following `.python-version` syntax:
- Major Python version (e.g. `3.13`, which will then be resolved to the
  latest Python 3.13). (This form is recommended, since it allows for
  Python security updates to be pulled in without having to manually
  bump the version.)
- Exact Python version (e.g. `3.13.0`)
- Comments (lines starting with `#`)
- Blank lines

We don't support the following `.python-version` features:
- Specifying multiple Python versions
- Prefixing versions with `python-` (since this form is undocumented
  and will likely be deprecated by pyenv in the future)

In addition, the existing `runtime.txt` support has been updated to
allow specifying just the major Python version, in order to increase
feature parity between the files, and avoid confusion if users try to
use the major version only syntax from `.python-version` in their
`runtime.txt`.

Refs #913.
Refs #932.
GUS-W-7671453.
GUS-W-16821357.
@edmorley
Copy link
Member

Sorry for the delay in getting to this!

Our focus has been on creating our next generation buildpacks, Cloud Native Buildpacks (aka CNBs), which include support for new features like this one. (For more details on our CNBs see this overview and to follow along with the in-development Python CNB's progress see the heroku/buildpacks-python repo.)

However, as of #1664 this (non-CNB, aka "classic") buildpack now supports the .python-version file too.

Notably, the .python-version file optionally supports specifying a Python version using only the major version (such as 3.13), which means that your app will use the latest Python patch version each time it builds - saving you from having to manually track new security updates.

See:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants