1.1. Know Which Version of Python You’re Using¶
Throughout this book, the majority of example code is in the syntax of Python 3.7 (released in June 2018). This book also provides some examples in the syntax of Python 3.8 (released in October 2019) to highlight new features that will be more widely available soon. This book does not cover Python 2.
Many computers come with multiple versions of the standard CPython runtime preinstalled. However, the default meaning of python on the command line may not be clear. python is usually an alias for python2.7, but it can sometimes be an alias for even older versions, like python2.6 or python2.5. To find out exactly which version of Python you’re using, you can use the –version flag:
>>> ! python --version
Python 3.11.2
>>> ! python3 --version
Python 3.11.2
You can also figure out the version of Python you’re using at runtime by inspecting values in the sys built-in module:
>>> import sys
>>> print(sys.version_info)
>>> print(sys.version)
sys.version_info(major=3, minor=11, micro=2, releaselevel='final', serial=0)
3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0]
Python 3 is actively maintained by the Python core developers and community, and it is constantly being improved. Python 3 includes a variety of powerful new features that are covered in this book. The majority of Python’s most common open source libraries are compatible with and focused on Python 3. I strongly encourage you to use Python 3 for all your Python projects.
Python 2 is scheduled for end of life after January 1, 2020, at which point all forms of bug fixes, security patches, and backports of features will cease. Using Python 2 after that date is a liability because it will no longer be officially maintained. If you’re still stuck working in a Python 2 codebase, you should consider using helpful tools like 2to3 (preinstalled with Python) and six (available as a community package; see Item 82: “Know Where to Find Community-Built Modules”) to help you make the transition to Python 3.
Things to Remember
✦ Python 3 is the most up-to-date and well-supported version of Python, and you should use it for your projects.
✦ Be sure that the command-line executable for running Python on your system is the version you expect it to be.
✦ Avoid Python 2 because it will no longer be maintained after January 1, 2020.