Search code examples
pythonherokuazure-speech

Switching python interpreters for Heroku web app


The problem For context:

  • I have deployed an app with Heroku
  • I have Python 3.9.2 running locally in VS Code vs Python 3.10.8 running on a Heroku machine

I am currently encountering the problems as mentioned on this thread.

I have found the solution to my problem seems to be switching python interpreters and copying my various packages to that location.

However, there is little documentation on how to do this in Heroku.

Any assistance is really appreciated :)

What I've tried Given the lack of documentation on the above, I've tried the following...

I found that the Heroku deployment was running on Python 3.10.8 runtime, whereas my local machine had been running 3.9.2.

I then added a runtime.txt in the root of the app's directory in Github that specified to use 3.9.2, but when I try and redeploy the build failed.

Following the Heroku logs, this leads me to:

-----> Building on the Heroku-22 stack
-----> Using buildpack: heroku/python
-----> Python app detected
-----> Using Python version specified in runtime.txt
 !     Requested runtime 'cat runtime.txt
python-3.9.2' is not available for this stack (heroku-22).
 !     For supported versions, see: https://devcenter.heroku.com/articles/python-support
 !     Push rejected, failed to compile Python app.
 !     Push failed

I then realise from the logs, that the build package isn't of 3.9.2 isn't supported.

I've then downloaded python 3.11.0 on my local machine (which is supported, as mentioned here).

Deactivated the current virtual environment (using 3.9.2).

Created a new virtual environment.

Rerun the app locally and checked it works.

I've then added the changes to the git repo.

Then, I've tried to deploy on Heroku. I'm now getting:

-----> Building on the Heroku-22 stack
-----> Using buildpack: heroku/python
-----> Python app detected
-----> Using Python version specified in runtime.txt
 !     Requested runtime 'cat runtime.txt
python-3.11.0' is not available for this stack (heroku-22).
 !     For supported versions, see: https://devcenter.heroku.com/articles/python-support
 !     Push rejected, failed to compile Python app.
 !     Push failed

I then navigate to the support documentation

I then try and follow the support guidance around 'Checking the buildpack version', as my version of Python is supported, as per the documentation.

I run the command against my CLI which checks which buildpack version I have. I get:

=== [appnamegoeshere] BuildPack URL
heroku/python

So, I have the 'heroku/python' buildpack.

Not sure where to go from here?

Any help is much appreciated :)


Solution

  • The documentation for selecting a runtime says:

    To specify a Python runtime, add a runtime.txt file to your app's root directory that declares the exact version number to use:

    $ cat runtime.txt
    python-3.10.8
    

    The cat runtime part of that is a command that you can run on many systems (Linux, macOS, etc.) to print the contents of a file out. It should not be included in the file.

    Your runtime.txt should just contain the appropriate python-x.y.z line. At the moment, on the Heroku-22 stack you can use Python 3.9:

    python-3.9.15
    

    Python 3.10:

    python-3.10.8
    

    Or Python 3.11:

    python-3.11.0
    

    The version you use on your local machine usually just needs to match the major and minor parts: your local Python 3.9.3 should be compatible with Heroku's 3.9.15.

    Note that Heroku's list of supported Python versions is updated pretty frequently as new patch releases come out.