Sub topic 2
Choosing a Python version
This chapter assumes you can open a terminal and run a command. It covers which Python release to install, and the next chapter covers how to install it.
Python 3 is the only choice
Python 2 reached end of life on 1 January 2020. It no longer gets security fixes, and current tools and libraries do not support it. Every new project should use Python 3. If an old tutorial tells you to run python2, find a newer one.
How version numbers work
A version such as 3.13.2 has three parts:
- Major (3): the language generation. It has not changed since 2008.
- Minor (13): a feature release. New syntax and library changes arrive here, and some old features are removed.
- Micro (2): a bug-fix or security release within a minor version. It is safe to upgrade.
When people say "Python 3.13", they mean the minor version. Code written for 3.13 usually runs on 3.12 and 3.14, but not always.
The release cycle
The Python core team publishes a new minor version every October. Each one is supported for about five years: roughly two years of regular bug-fix releases, then security-only fixes until end of life. After that, the version gets no more patches.
The practical consequence is that a Python release has a lifespan, and an old one will eventually stop receiving security fixes. The official "Status of Python versions" page on python.org lists which versions are currently supported and when each one ends.
A decision rule
In short:
- Use the version your project, course, or team requires.
- If nothing requires one, install the latest stable release. If it came out only weeks ago, the previous minor version is a safer choice, because third-party packages sometimes need time to publish builds for a new release.
- Check that the version is not end-of-life.
- Skip anything labeled alpha (
a), beta (b), or release candidate (rc). These are previews and can change.
Version differences that bite
New syntax fails on older interpreters. For example, match statements (structural pattern matching) were added in Python 3.10. On 3.9 they raise a SyntaxError, even though the code is valid on newer versions.
Many Linux distributions and older macOS setups ship a Python 3 that is several releases old. That is fine for system tools but a poor base for your own work. Install your own, and check which one your terminal actually runs.
Check what you are running
You can inspect the version from inside a script. This is useful when a program needs a minimum version.
import sys
print(sys.version)
print(sys.version_info)
if sys.version_info < (3, 10):
raise SystemExit("This program needs Python 3.10 or newer.")
print("Version check passed.")sys.version_info compares like a tuple, so (3, 10) means "3.10". Compare numbers rather than strings, because the string "3.9" sorts after "3.10".
From the terminal, python3 --version gives the same information. Chapter 6 covers that command and its Windows equivalent in detail.
Exercise
You are starting a personal project with no version requirements. The newest stable release came out two weeks ago, and the previous minor version is still supported for years. Which would you install, and why? Name one check you would make before installing.
Show answer
Either answer is defensible, but the previous minor version is the more cautious choice. Packages you depend on may not yet have builds for a release that is two weeks old. The newest release is also fine if you check that your key packages support it. Before installing, check the "Status of Python versions" page on python.org to confirm the version is supported and not a pre-release.