Get the App
SLTechnology News&Howtos  ›  Development  › 

How to detect the Python version at run time

Shulou Source: shulou.com Published: 2022-06-01 06:20:25 09月24日 Update

This article mainly introduces how to test the Python version at run time, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

Detect the Python version at run time

Sometimes, if the currently running Python engine is lower than the supported version, we may not want to execute our program. To do this, you can use the following code snippet. It also prints the version of Python currently in use in a readable format.

Import sys#Detect the Python version currently in use.if not hasattr (sys, "hexversion") or sys.hexversion! = 50660080: print ("Sorry, you aren't running on Python 3.5\ n") print ("Please upgrade to 3.5.\ n") sys.exit (1) # Print Python version in a readable format.print ("Current Python version:", sys.version)

Alternatively, you can replace sys. Hexversioncodes = 50660080 with sys.version_info > = (3,5) in the above code. This is the advice of an informed reader.

Output at run time on Python 2.7s.

Python 2.7.10 (default, Jul 14 2015, 19:46:27) [GCC 4.8.2] on linux Sorry, you aren't running on Python 3.5Please upgrade to 3.5.

Output at run time on Python 3.5.

Python 3.5.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linux Current Python version: 3.5.2 (default, Aug 22 2016, 21:11:05) [GCC 5.3.0] Thank you for reading this article carefully. I hope the article "how to detect the Python version at run time" shared by the editor will be helpful to you. At the same time, I hope you will support me and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Run version article test code support output for this value interest can make at the same time suggestions engine more friends formats fragments for knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft MariaDB Redmi Apple Xiaomi