How to Check Python Version in Linux, Windows, And macOS: A Comprehensive Guide

Check Python Version

Python is a versatile programming language developers use for everything from web development, automation, data analysis, and machine learning. The flexibility of the language comes from a wide range of native and third-party packages that bring in functionalities missing from the core language features.

While these packages are a great way of extending Python’s capabilities and saving development time by reusing pre-written and tested code, you should be aware of the challenge of version compatibility between the installed Python version and the packages.

Not all packages are compatible with all Python versions. It is, therefore, critical that you should check Python version on your system before you install any packages for your projects.

This detailed guide will explain how to check the Python version on your system. Since Python is available on all popular operating systems, we will cover the process of checking Python versions on Linux, Windows, and macOS to help users regardless of their OS.

The Prerequisites

You’ll need:

  • A system running either Linux, Windows, or macOS
  • Access to a terminal or command prompt
  • Administrative privileges or rights

How to See if Python is Installed on the System

Before diving into the details of checking the Python version on your system, we highly recommend checking if Python is available on your system. 

The best way to tell if Python is installed is to call it from the command line. For this, run the following command in your Linux or macOS terminal:

# python3

Alternatively, enter python in Windows Command Prompt.

This will launch the Python interpreter in the terminal and you will see the >>> prompt. This is where you can enter simple Python statements (such as print(“Hello World”). 

 python3

When done, enter exit() to exit the Python interpreter and return to the terminal.

Alternatively, you can search for Python’s installation location by running which python3 command in the terminal. This command outputs the path of the folder where Python is located on the system. 

The syntax of the command is as follows:

# which python3

which python3

How to Check Python Version in Linux

Follow these steps to check the Python version on a Linux-based operating system:

  1. Open a terminal window.
  2. Type one of the following commands and press Enter:

# python --version

Or

# python3 --version

The output will display the installed Python version.

python3 --version

Check the Python Version in Bash

If you wish to check the Python version in Bash, the process involves creating and running a simple script. 

  • Create and open a file named pyver.sh in your preferred text editor. We will use Nano for this demonstration. For this, the relevant command is:

    # nano pyver.sh
  • Enter the following lines in the file and save it.

pyv="$(python -V 2>&1)"

echo "$pyv"

$pyv

  • Next, assign appropriate permissions to the script file by this command:

    # chmod u+x pyver.sh
  • Finally, execute the file by calling the script in the terminal to check the Python version in Bash:

    # ./ pyver.sh

pyver.sh

How to Check Python Version in Windows

Follow these steps to find out the Python version in a Windows environment:

  1. Open the Command Prompt.
  2. Enter the command:

> python --version

The Python version will appear in the following line:

python --version

Check Python Version in PowerShell

Many developers prefer PowerShell over the standard Command Prompt because of the following significant differences:

  • PowerShell has access to the installed .NET libraries 
  • Developers can extend the core PowerShell capabilities with scripts that use PowerShell’s powerful interpreter.

Follow these steps to check Python version in PowerShell:

  • Launch PowerShell and enter the following command:

PS>  python --version

  • You will see something similar to the following in the terminal:

python --version

How to Check Python Version in MacOS

If you’re using macOS, simply follow these steps to check Python version:

  1. Open the terminal.
  2. Enter one of the following commands:

% python --version

Or

% python3 --version

The response will show the version installed on your macOS system.

python3 --version

How to Check Python Version in Python Code

When developing an application, developers should implement a check to verify that the appropriate Python version is available. This preemptive check can avert potential crashes and discrepancies due to version conflicts.

To implement a Python version check, you can use the following code snippet that checks whether Python 3.6 or higher is available on the target system:

import sys

if not (sys.version_info.major == 3 and sys.version_info.minor >= 6):

print("Python 3.6 or higher is required.")

print("You are using Python {}.{}.".format(sys.version_info.major, sys.version_info.minor))

sys.exit(1)

If the required Python version is absent, the script will issue an alert and inform the user about the required Python version. By editing the values in the first IF statement, you can easily modify this code snippet to check for your preferred version.

Install New Python Version

There are cases where you need to upgrade the Python version on your system. Maybe, your project requires a specific version or you wish to install a new Python version on your system. Since the process of installing a new Python version is different on every OS, we will cover the three major scenarios:

Install New Python Version in Windows

Follow these steps to install a new Python version on your Windows machine:

  1. Go to the official Python website and download the installation files for the latest available version.
  2. Double-click the installation file to run the installation file. 

Install New Python Version in macOS

Launch the terminal and run this command in the terminal:

% brew upgrade python

brew upgrade python

Install New Python Version in Linux

The easiest way of installing a new version or upgrading the existing Python version in Linux environment is to use the distribution’s package manager. 

If you have DNF as your package manager, run the following command:

# dnf upgrade python3

dnf upgrade python3

Alternatively, if you use APT as your package manager, run this command to install a new Python version:

# apt upgrade python3

List All Installed Python Versions

You may have multiple Python versions on a system. This is a common scenario where developers need to test their applications against several Python versions to enhance compatibility. 

You can easily list all installed Python versions by running the following command in the terminal:

# ls -ls /usr/bin/python*

ls -ls /usr/bin/python

What Makes Python Such An Amazing Programming Language?

Guido van Rossum officially released Python in 1991. The primary objective behind the move was to create a programming language known for its readability, simplicity, and ease of comprehension. Guido van Rossum’s vision for Python emphasized clear and understandable code, enabling programmers to craft components using fewer lines of code than other languages.

Python is the language of choice for many proprietary and open-source projects today. Here are a couple of reasons behind its popularity. 

Reason #1: Emphasis on Readability

Python has a clean and easily understandable syntax that uses indentation and whitespace to define code blocks. The code is easy to read because of the distinct separation between blocks and sub-blocks. 

Reason #2: Simplicity and Clarity

Python advocates a straightforward and lucid programming style. The idea is to keep everything simple to speed up the development process and improve codebase maintenance. 

Reason #3: Versatility and Adaptability

Python is versatile and flexible, making it a good fit across diverse domains such as web development, scientific computing, data analysis, artificial intelligence, machine learning, and automation.

Reason #4: Interactivity and Swift Prototyping

Python supports an interactive mode, facilitating quick experimentation and testing of code snippets. Developers use this feature to check out ideas and speed up prototyping and initial component development.

Reason #5: Portability and Cross-Platform Compatibility

Python is platform-independent, enabling seamless execution of the same codebase across various operating systems with minimal or no modifications.

Reason #6: Robust Standard Library

Python boasts an extensive standard library with pre-built modules and functions, catering to a wide range of typical development challenges. Developers do not have to reinvent the wheel every time they need to solve a problem because there is often a package or code snippet for it. 

Reason #7: Community and Open Source Values

Python adheres to an open-source development model, fostering collaboration, knowledge sharing, and community-driven contributions. Over the years, developers and enthusiasts have created a dynamic and supportive global community around the language.

Reason #8: Support for Object-Oriented Programming (OOP)

Python is founded on the principles of object-oriented programming, facilitating code organization into reusable and modular structures. This significantly enhances code reusability and maintainability.

Reason #9: Ease of Learning (and Teaching)

An essential aspect of developing Python was to make it easily learnable and teachable. As a result, it is the popular choice as the first programming language in many CS 101 courses. 

Conclusion

Identifying the Python version installed on your system is a fundamental operational requirement that every developer should know how to fulfill. Whether you are using Linux, macOS, or Windows, the techniques outlined in this article will check Python version.

FAQs

Q. How do I check which version of Python is installed on my Windows computer?

To check the Python version on Windows, you can open the Windows Command Prompt and type python –version or python -V.

Q. How can I check which version of Python is installed on macOs?

To check the Python version on a Mac, you can open the terminal and type python –version or python -V.

Q. How do I check the Python version on Linux?

To check the Python version on Linux, you can open the terminal and type python –version or python -V.

Q. Can I have multiple versions of Python installed on my computer?

Yes, you can have multiple versions of Python installed on your computer. Each version will have its own Python interpreter.

Q. How do I check the Python version using a Python script?

To check the Python version using a Python script, create a Python file and add the following snippet:

import sys

print(sys.version)

When you execute this snippet, it will print the available Python version.

Q. How can I easily check the version of Python 3 installed on my computer?

To easily check the version of Python 3 installed on your computer, you can open the command prompt or terminal and type python3 –version or python3 -V.

Q. How can I display the Python version number using Python code?

To display the Python version number using Python code, you can open a text editor and write the following code in a Python file:

import platform

print(platform.python_version())

Prakash

Prakash works as a System Administrator at RedSwitches (Leaders in offering Instant bare metal dedicated servers), focusing on service, security, patching, migration, and support. With more than a decade of experience in server admin and security, Prakash is the go-to guy for ensuring excellent uptime for clients. When he is not working, he spends his free time with his family and playing video games. Reach him out at [email protected]

Related articles

Latest articles