Test python version in script

Check Python version on command line and in script

This article explains how to check, get, and print the Python version installed and running on Windows, Mac, and Linux.

If you want to check the version of the package/library, OS, etc., instead of the version of Python itself, see the following articles.

Check Python version on the command line: —version , -V , -VV

Execute the python or python3 command with the —version or -V option in the command prompt ( cmd ) on Windows or the terminal on Mac and Linux.

$ python3 --version Python 3.11.3 $ python3 -V Python 3.11.3 

In some environments, the python command is assigned to the Python2.x series, while the python3 command is assigned to the Python3.x series.

The -VV option was introduced in Python 3.6 and provides more detailed information than -V .

$ python3 -VV Python 3.11.3 (main, Apr 7 2023, 20:13:31) [Clang 14.0.0 (clang-1400.0.29.202)] 

Check Python version in the script: sys , platform

You can use the standard library sys module or platform module to get the version of Python that is actually running. No additional installation is required, but you need to import it.

import sys import platform 

The same script works on Windows, Mac, and Linux, including Ubuntu.

It’s helpful to verify the running Python version when multiple installations exist in the same environment. You might assume that Python3 is running when, in fact, Python2 is being used. If you face any issues, it’s wise to confirm the active version.

It can also be used when switching operations depending on whether Python2 or Python3 is being used.

Various information string: sys.version

sys.version is a string containing various information, including the version number.

sys.version
A string containing the version number of the Python interpreter plus additional information on the build number and compiler used.
sys.version — System-specific parameters and functions — Python 3.11.3 documentation

print(sys.version) # 3.11.3 (main, Apr 7 2023, 20:13:31) [Clang 14.0.0 (clang-1400.0.29.202)] print(type(sys.version)) # 

Tuple of version numbers: sys.version_info

sys.version_info is a named tuple indicating the version number.

sys.version_info
A tuple containing the five components of the version number: major, minor, micro, releaselevel, and serial.
sys.version_info — System-specific parameters and functions — Python 3.11.3 documentation

print(sys.version_info) # sys.version_info(major=3, minor=11, micro=3, releaselevel='final', serial=0) print(type(sys.version_info)) # 

releaselevel is str , and the other elements are int .

You can get each value by specifying an index.

From version 2.7 for Python2 and version 3.1 for Python3, you can get elements by name ( major , minor , micro , releaselevel , serial ).

For example, if you want to get a major version:

print(sys.version_info.major) # 3 

To determine whether Python2 or Python3 is running, check the major version with sys.version_info[0] or sys.version_info.major . 2 means Python2, and 3 means Python3.

You can switch the process between Python2 or Python3.

if sys.version_info[0] == 3: print('Python3') else: print('Python2') # Python3 

To switch operations based on a minor version, use sys.version_info[1] or sys.version_info.minor .

As mentioned above, accessing elements by name is supported from version 2.7 in Python2 and version 3.1 in Python3. If the code might be executed in earlier versions, use sys.version_info[0] or sys.version_info[1] .

Version number string: platform.python_version()

platform.python_version() returns a string ‘major.minor.patchlevel’ .

print(platform.python_version()) # 3.11.3 print(type(platform.python_version())) # 

It is useful when you want to get the version number as a simple string.

Tuple of version number strings: platform.python_version_tuple()

platform.python_version_tuple() returns a tuple (major, minor, patchlevel) . The type of each element is str , not int .

print(platform.python_version_tuple()) # ('3', '11', '3') print(type(platform.python_version_tuple())) # 

Unlike sys.version_info , it is a simple tuple and cannot be accessed by name, such as major or minor .

  • Composite two images according to a mask image with Python, Pillow
  • Copy and paste text to the clipboard with pyperclip in Python
  • Sort a list of dictionaries by the value of the specific key in Python
  • NumPy: Transpose ndarray (swap rows and columns, rearrange axes)
  • NumPy: Round up/down the elements of a ndarray (np.floor, trunc, ceil)
  • NumPy: Add elements, rows, and columns to an array with np.append()
  • Fractions (rational numbers) in Python
  • How to write a comment and comment out lines in Python
  • Get maximum/minimum values and keys in Python dictionaries
  • How to use range() in Python
  • NumPy: Replace NaN (np.nan) in ndarray
  • Convert a string to a number (int, float) in Python
  • Determine, count, and list leap years in Python
  • Swap dictionary keys and values in Python
  • pandas: Extract columns from pandas.DataFrame based on dtype

Источник

Check the Python Version in the Scripts

Check the Python Version in the Scripts

  1. sys.version Method to Check Python Version
  2. sys.version_info Method to Check Python Version
  3. platform.python_version() Method to Check Python Version
  4. six Module Method to Check Python Version

In different circumstances, we need to know the Python version, or more precisely, the Python interpreter version that is executing the Python script file.

sys.version Method to Check Python Version

This version information could be retrieved from sys.version in the sys module.

>>> import sys >>> sys.version '2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)]' 
>>> import sys >>> sys.version '3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)]' 

sys.version_info Method to Check Python Version

sys.version returns a string containing the human-readable version information of the current Python interpreter. But the information like major release number and micro release number needs extra processing to be derived for further usage in the codes.

>>> import sys >>> sys.version_info sys.version_info(major=2, minor=7, micro=10, releaselevel='final', serial=0) 

You could compare the current version with the reference version simply using > , >= , == ,

>>> import sys >>> sys.version_info >= (2, 7) True >>> sys.version_info >= (2, 7, 11) False 

We could add an assert in the scripts to make sure that the script runs with the requirement of minimal Python version.

import sys assert sys.version_info >= (3, 7) 

It will raise an AssertionError if the interpreter doesn’t meet the version requirement.

Traceback (most recent call last):  File "C:\test\test.py", line 4, in module>  assert sys.version_info >= (3, 7) AssertionError 

platform.python_version() Method to Check Python Version

python_version() in platform module returns the Python version as string major.minor.patchlevel .

>>> from platform import python_version >>> python_version() '3.7.0' 

Or similar to sys.version_info , platform also has a method to return the Python version as tuple (major, minor, patchlevel) of strings — python_version_tuple()

>>> import platform >>> platform.python_version_tuple() ('3', '7', '0') 

six Module Method to Check Python Version

If you only need to check whether the Python version is Python 2.x or Python 3.x, you could use six module to do the job.

import six if six.PY2:  print "Python 2.x" if six.PY3:  print("Python 3.x") 

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

Related Article — Python Version

Источник

Check Python version using Python Script

In this article, we will learn the command to check the version of Python using Python script. Let’s first have a quick look over what is a Python script.

Python is a well known high-level programming language. The Python script is basically a file containing code written in Python. The file containing the python script has the extension ‘.py’ or can also have the extension ‘.pyw’ if it is being run on a windows machine. To run a python script, we need a python interpreter that needs to be downloaded and installed.

Check Python Version

Like much other software, Python too has several different versions released to date. After Python or any installation, one should check the version of the software that is running the script. Python uses version keywords to check the Python version. The python script is a simple code that can be run in different modes such as using a Python interpreter, on a command line, or using any Python editor. Let us look over the commands to check the Python version.

There are different versions of Python, but the two most popular ones are Python 2 and Python 3. When looking at the version number, there are usually three digits to read:

Let us look over the commands to check the Python version.

Check Python Version in Linux

Most modern Linux distributions come with Python pre-installed. To check the version installed, open the Linux terminal window and enter the given command:

Check Python Version in Windows

Most out-of-the-box Windows installations do not come with Python pre-installed. However, it is always a good practice to check the version. To check the version installed, open Windows Powershell or any Python editor, and enter the given command:

Check Python Version in MacOS

If you are using a macOS, check the Python version by entering the given command in the terminal:

In all the three version checks, you will an output somewhat like Python 3.5.2. As discussed above 3 is the major version, 5 is the minor version and 2 is the micro version.

Use sys module to check Python version

import sys print("Python version") print (sys.version) print("Version info.") print (sys.version_info)

Python version
3.5.2 (default, Sep 10 2016, 08:21:44)
[GCC 5.4.0 20160609]
Version info.
sys.version_info(major=3, minor=5, micro=2, releaselevel=’final’, serial=0)

Use platform module to check Python version

from platform import python_version print(python_version()) 

Conclusion

In this article, we learned to use a simple command python —version to check the version of Python on any Python script whether it is an editor, interpreter, or a command line. We saw the other two ways i.e. sys.version and python_version to check the Python version.

Источник

How to Check Your Python Version (Windows, macOS, Linux)

How to Check Your Python Version (Windows, macOS, Linux) Cover Image

In this tutorial, you’ll learn how to check your Python version in Windows, macOS, and Linux. You’ll learn how to check the version of Python using the command line and within a Python script itself. You’ll learn how to get the version number of the interpreter that your scripts will use.

Knowing how to do this is an important skill for any Python developer. For example, it can be an important skill in order to better troubleshoot your code. If your interpreter is set to a different version than you’re expecting (say, Python 2 versus Python 3), being able to identify the version of the interpreter can help troubleshoot your problems.

By the end of this tutorial, you’ll have learned:

  • How to check the Python version of your interpreter in Windows, Mac OS, and Linux
  • How the check the Python version while running your script
  • How to access the major, minor and micro versions of your Python version programmatically

How to Check Your Python Version

To check the version that your Python interpreter is running we can use a version command on the python command. Because accessing the command line prompt or terminal varies from system to system, this part of the tutorial is split across the different operating systems available to you.

Over the following sections, you’ll learn how to check your Python version using Windows 10, Windows 7, macOS, and Linux.

How to Check Your Python Version on Windows 10

In Windows 10, we can use the PowerShell to check the version of Python that we are running. In order to access the PowerShell, simply use the following steps:

Once the PowerShell is open you can access the Python version your interpreter is running by writing the commands shown below.

Источник

Читайте также:  Анимация появления изображения css
Оцените статью