Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Request a Specific Python Version

For more information on concepts covered in this lesson, you can check out:

Here are the command-line snippets used in this lesson:

Shell
$ vim runtime.txt
$ git add runtime.txt
$ git commit -m "Specify explicit Python version"
$ git push heroku
$ heroku run python --version
$ heroku run bash
$ uname -r

Configuration File
python-3.10.0

00:00 Although you haven’t specified any particular Python version for your app, Heroku has decided to go with Python 3.9.7, which was the default choice at the time of recording this video. That’s convenient as long as you don’t need to use special syntax introduced in later releases or when you don’t care about backward compatibility. In this lesson, you’ll learn how to upgrade or downgrade your Python version on the Heroku infrastructure. Start by looking at the output provided by Heroku CLI when you trigger a build.

00:30 It tells you that no Python version was specified and links to instructions on how to do that. When you follow that link, you’ll find out that you need to create a runtime.txt file in your project root folder with a desired Python version.

00:44 Heroku will recognize it and use the requested version. However, it must be one of the supported runtimes, which are currently these. Let’s use Python 3.10.

00:58 Make sure to include all three numbers delimited with periods, which in this case are 3.10.0. Next, create the runtime.txt file and paste the runtime name.

01:13 Finally, when you save the file, add it to the staging area, make a new commit,

01:25 and push it to Heroku. At this point, Heroku CLI honors the Python version that you specified. Because it changed since the last time, Heroku had to invalidate the cache and create a new virtual environment. That means your build might take a bit longer to finish.

01:44 If you’d like to double-check the actual Python version your Heroku app is running on, then you can either SSH into it or start a throwaway Heroku container which has access to the same remote virtual environment as your app.

01:57 You can create such a temporary container on demand by typing heroku run followed by the command to execute.

02:07 If you’d like to keep the connection open in an interactive mode, then run the Bash shell instead. It will remain open until you terminate the session. In the meantime, you can investigate the machine that you’re logged in to. For example, printing the system information reveals that Heroku is in fact using Amazon’s AWS infrastructure. Okay! In the next lesson, you’ll revisit the topic of configuring an efficient web server.

Become a Member to join the conversation.