[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedMarch 16, 2023

How to Check Python Version.

How To Check Python Version.

This article explains how to check what version of Python is installed on your operating system using the command line. This can be useful when installing applications that require a specific version of Python.

Python is one of the most popular programming languages in the world. It is used for developing websites, writing scripts, machine learning, analyzing data, and more.

We’ll also show you how to programmatically determine what version of Python is installed on the system where the Python script is running. For example, when writing Python scripts, you’ll need to determine whether the script supports the version of Python installed on the user’s machine.

 

Python Versioning

Python uses semantic versioning . Production-ready releases are versioned in the following scheme:

MAJOR.MINOR.MICRO

For example, in Python 3.6.8, 3 is a major version, 6 is a minor version, and 8 is a micro version.

  • MAJOR – Python has two major versions that are not fully compatible: Python 2 and Python 3. For example, 3.5.7, 3.7.2, and 3.8.0 are all part of the Python 3 major version.

  • MINOR – These releases are bringing new features and functions. For example, 3.6.6, 3.6.7, and 3.6.8 are all part of the Python 3.6 minor version.

  • MICRO – The new micro versions contain various bug fixes and improvements.

Development releases have additional qualifiers. For more information, read the Python “Development Cycle” documentation.

 

Checking Python Version

Python is pre-installed on most Linux distributions and macOS. On Windows, you have to download and install it.

To find out which version of Python is installed on your system run the python –version or python -V command:

python --version

The command will print the default Python version, in this case, that is 2.7.15. The version installed on your system may be different.

Python 2.7.5

The default version of Python will be used by all scripts that have /usr/bin/python set as an interpreter in the script’s shebang line.

Some Linux distributions have multiple versions of Python installed at the same time. Generally, the Python 3 binary is named python3, and the Python 2 binary is named python or python2, but that may not always be the case.

You can check if you have Python 3 installed by typing:

python3 --version
Python 3.6.8

Python 2 support ends in 2020. Python 3 is the present and future of the language.

At the time of writing this article, the latest major release of the Python is version 3.8.x. Chances are that you have an older version of Python 3 installed on your system.

If you want to install the latest version of Python, the procedure depends on the operating system you are running.

 

Programmatically Checking Python Version

Python 2 and Python 3 are fundamentally different. The code that is written in Python 2.x may not work in Python 3.x.

The sys module that is available in all Python versions provides system-specific parameters and functions. sys.version_info allows you to determine the Python version installed on the system. It returns a tuple that contains the five version numbers: major, minor, micro, releaselevel, and serial.

Let’ say you have a script that requires at least Python version 3.5, and you want to check whether the system meets requirements. You can do that by simply checking the major and minor versions:

import sys

if not (sys.version_info.major == 3 and sys.version_info.minor >= 5):
    print("This script requires Python 3.5 or higher!")
    print("You are using Python {}.{}.".format(sys.version_info.major, sys.version_info.minor))
    sys.exit(1)

If you run the script using Python version less than 3.5 it will produce the following output:

This script requires Python 3.5 or higher!
You are using Python 2.7.

To write Python code that runs under both Python 3 and 2, use the future module. It allows you to run Python 3.x-compatible code under Python 2.

 

Conclusion

Finding out what version of Python is installed on your system is very easy, just type python –version.

Keep reading
How to Set or Change the Time Zone in Linux
March 7, 2023

How to Set or Change the Time Zone in Linux

How To Set Or Change The Time Zone In Linux This tutorial covers the steps necessary to set or change the time zone in Linux. A time zone is a geographic region that has the same standard time. Typically the time zone is set during the installation of the operational system, but it can be…

Read article
How to Start, Stop, or Restart Nginx
March 6, 2023

How to Start, Stop, or Restart Nginx

How To Start, Stop, Or Restart Nginx This guide explains how to start, stop, and restart Nginx on Linux servers. Nginx pronounced “engine x” is a free, open-source, high-performance HTTP and reverse proxy server responsible for handling the load of some of the largest sites on the Internet. It can…

Read article
How to understand network IP (internet protocol)
March 6, 2023

How to understand network IP (internet protocol)

WHAT IS AN IP NETWORK? An IP network is a group of computers connected via their unique internet protocol (IP) addresses. IP is fundamental to allowing our devices to communicate with each other. It is the IP address that acts as the identifier and differentiates each individual device connected…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00