How to check python version

How to Check the Python Version?

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

  1. Open the Command Prompt by pressing Win + R, typing cmd, and hitting Enter.
  2. Type one of the following commands and press Enter:
    • python –version
    • python -V
  3. 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

  1. Open Terminal via Spotlight (Cmd + Space) and search for “Terminal.”
  2. Enter one of the following commands:
    • For Python 3: python3 –version
    • For Python 2: python –version
  3. Example output:
Python 3.9.5 

If no output appears, install Python using Homebrew (brew install python) or directly from python.org.

Linux

  1. Open Terminal using Ctrl + Alt + T or by searching for “Terminal” in the application menu.
  2. Type the following commands:
    • For Python 3: python3 –version
    • For Python 2: python –version
  3. 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

VersionRelease DateKey Features
Python 1.0Jan 1994Basic functionality established.
Python 2.0Oct 2000List comprehensions introduced.
Python 3.0Dec 2008New syntax and standard library.

Upgrading to a Newer Python Version

Upgrading ensures access to the latest features and security patches. Follow these steps:

  1. Visit the official Python website.
  2. Download the latest version of your operating system.
  3. 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!

Leave a Reply

Your email address will not be published. Required fields are marked *

Advait Upadhyay

Advait Upadhyay (Co-Founder & Managing Director)

Advait Upadhyay is the co-founder of Talentelgia Technologies and brings years of real-world experience to the table. As a tech enthusiast, he’s always exploring the emerging landscape of technology and loves to share his insights through his blog posts. Advait enjoys writing because he wants to help business owners and companies create apps that are easy to use and meet their needs. He’s dedicated to looking for new ways to improve, which keeps his team motivated and helps make sure that clients see them as their go-to partner for custom web and mobile software development. Advait believes strongly in working together as one united team to achieve common goals, a philosophy that has helped build Talentelgia Technologies into the company it is today.
View More About Advait Upadhyay

Leave a Reply

Your email address will not be published. Required fields are marked *

India

Dibon Building, Ground Floor, Plot No ITC-2, Sector 67 Mohali, Punjab (160062)

Business: +91-814-611-1801
USA

7110 Station House Rd Elkridge MD 21075

Business: +1-240-751-5525
Dubai

DDP, Building A1, IFZA Business Park - Dubai Silicon Oasis - Dubai - UAE

Business: +971 565-096-650
Australia

G01 , 8 Meririvlle Road, Kellyville Ridge NSW 2155 , Australia

Automate your business processes to boost your ROI.
Get updates straight to your inbox