Python is one of the most popular programming languages in the world today, with simplicity and versatility being the main reasons. However, it is crucial to know which Python version you are working with to make sure it is compatible with libraries, frameworks, and projects. To do this, this guide looks at how to check the Python version across operating systems and programmatically using scripts.
Python, developed by Guido van Rossum in 1991, is a language with clear syntax and a wide range of applications. Since its inception, Python has had different versions, all of which featured new and improved features. From Python 1.x to the most recent Python 3.x, every version has seen significant updates, making it very important to check the version number for compatibility with certain tools and frameworks.
Let’s have an overview of Python & Historical background of Python:
Python began as a hobby project and has grown into a robust programming language with regular updates.
- Python 1.x (1994): Introduced the foundation, including exception handling and modules.
- Python 2.x (2000): Extended capabilities but was deprecated in 2020.
- Python 3.x (2008): A complete overhaul focusing on modern programming standards.
The ongoing development ensures Python remains a top choice for developers.
Checking Python Version Using Command Line
The command line provides a quick and efficient way to verify the Python version of your system. This is often the first step in troubleshooting or setting up a Python-based project.
Windows
- Open the Command Prompt by pressing Win + R, typing cmd, and hitting Enter.
- Type one of the following commands and press Enter:
- python –version
- python -V
- Example output:
Python Version: 3.10.2
If the command doesn’t work, ensure Python is installed and the installation directory is added to your system’s PATH variable.
macOS
- Open Terminal via Spotlight (Cmd + Space) and search for “Terminal.”
- Enter one of the following commands:
- For Python 3: python3 –version
- For Python 2: python –version
- Example output:
Python 3.9.5
If no output appears, install Python using Homebrew (brew install python) or directly from python.org.
Linux
- Open Terminal using Ctrl + Alt + T or by searching for “Terminal” in the application menu.
- Type the following commands:
- For Python 3: python3 –version
- For Python 2: python –version
- Example output:
Python 3.8.10
Ensure python3 is used for newer versions, as some distributions still link Python to Python 2.
Checking Python Version Using Scripts
Python provides several built-in modules to check the version programmatically. These methods are particularly useful for scripts or applications requiring Python version verification.
Using sys.version (Information String)
The sys module includes the version attribute, which provides a detailed string with the Python version, build date, and compiler information.
Example:
import sys
print("Python Version:", sys.version)
Output:
Python Version: 3.9.5 (default, May 3 2021, 19:12:05)
[GCC 8.4.0]
This method is ideal for obtaining detailed information about the Python build.
Using sys.version_info (Tuple of Version Numbers)
For a structured representation of the version, the sys.version_info attribute returns a tuple with the version numbers and release details.
Example:
import sys
print("Version Info:", sys.version_info)
Output:
Version Info: sys.version_info(major=3, minor=9, micro=5, releaselevel='final', serial=0)
This format is perfect for programmatic comparisons and ensuring compatibility.
Using platform.python_version() (Version Number String)
The platform module offers the python_version() function, which returns the version as a simple string.
Example:
import platform
print("Python Version:", platform.python_version())
Output:
Python Version: 3.10.2
This straightforward approach is suitable for displaying the version in logs or outputs.
Using platform.python_version_tuple() (Tuple of Version Number Strings)
For a tuple-based representation of the version numbers as strings, use platform.python_version_tuple().
Example:
import platform
print("Version Tuple:", platform.python_version_tuple())
Output:
Version Tuple: ('3', '10', '2')
This method is useful for situations requiring string-based representations of the version.
Popular Python Versions and Release Dates
Apart from knowing ‘How to check Python Version?’ let’s have an overview of Python 1.x, 2.x, and 3.x Versions :
- Python 1.x: Introduced fundamental features like exception handling.
- Python 2.x: Added support for Unicode and extended libraries.
- Python 3.x: Focused on modernization, breaking backward compatibility for better design.
Table of Python Versions and Release Dates
Version | Release Date | Key Features |
Python 1.0 | Jan 1994 | Basic functionality established. |
Python 2.0 | Oct 2000 | List comprehensions introduced. |
Python 3.0 | Dec 2008 | New syntax and standard library. |
Upgrading to a Newer Python Version
Upgrading ensures access to the latest features and security patches. Follow these steps:
- Visit the official Python website.
- Download the latest version of your operating system.
- Install the new version by following the setup instructions.
Confirm the update by running python –version or python3 –version in the command line.
Conclusion
Knowing how to check the Python version is crucial for successful project development. Whether using the command line or scripts, understanding the installed Python version helps ensure compatibility and access to modern features.
Summary of Commands and Methods:
Command Line: Use python --version or python3 --version.
Scripts: Use sys.version, sys.version_info, platform.python_version(), or platform.python_version_tuple().
Stay updated to leverage Python’s evolving capabilities!