Python Basics: How to check the version of Python

This tutorial explains how to use Python version installed on the command line to check the operating system. This is useful when installing a specific version of Python application written in Python.

We'll also show you how to programmatically determine the version of Python installed on your system to run Python scripts. For example, when writing Python scripts, you need to decide whether to support Python script version installed on the user's computer.

Check the version of Python

Python pre-installed on most Linux distributions and macOS.

To find out the default version of Python installed on your system, run the python --version or python -V command:

This command will print the default version of Python, in this case the version is 2.7.5, the version installed on your system may be different.

Python Basics: How to check the version of Python 

All scripts provided in the shebang line in the script / usr / bin / python as the interpreter will use the default version of Python.

Multiple versions of Python Some Linux distributions installed simultaneously. In general, Python 3 executable file named python3, and Python executable file named python or python2, but that may not always be the case. You can check this by typing the following if the Python 3 installed:

Python Basics: How to check the version of Python

Although Python 2 has been well supported, but Python 3 is considered to be the language of the present and future.

As of this writing, the latest version is a major release of Python 3.7.x. You might have an older version of Python 3 installed on your system. If you want to install the latest version, the process depends on the operating system you are running.

Programmatically check the Python version

Python 2 and Python 3 is fundamentally different. Code written using Python 2.x may not be available in Python 3.x in.

sys module is available in all Python versions, he provides parameters and functions specific to the system. sys.version_info allows you to determine the version of Python installed on your system. It is a tuple containing five version number: major, minor, micro, releaselevel and serial.

Suppose you have a need at least Python 3.5 version of the script, and you want to check whether the system meets the requirements. You can do this by simply checking 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)

Python Basics: How to check the version of Python

If less than Python version 3.5 running script

The following output is generated:

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

Python Basics: How to check the version of Python

To write in Python Python code to run under 3 and 2, please use the future modules. It allows you to code compatible with Python 2 Python 3.x is running under.

in conclusion

View Python version installed on the system is very simple, just enter it python --version.

Guess you like

Origin www.linuxidc.com/Linux/2019-09/160518.htm