YouTube Icon

Code Playground.

How to Check Python Version

CFG

How to Check Python Version

Python is one of the most well known programming dialects on the planet. It is utilized for creating sites, composing contents, AI, investigating information, and then some. 

This article discloses how to check what adaptation of Python is introduced on your working framework utilizing the order line. This can be helpful when introducing applications that require a particular variant of Python. 

We'll likewise tell you the best way to automatically figure out what variant of Python is introduced on the framework where the Python content is running. For instance, when composing Python contents, you'll have to decide if the content backings the rendition of Python introduced on the client's machine. 

Python Versioning

Python utilizes semantic forming . Creation prepared deliveries are formed in the accompanying plan: 

MAJOR.MINOR.MICRO

For instance, in Python 3.6.8, 3 is a significant rendition, 6 is a minor variant, and 8 is a miniature adaptation. 

MAJOR - Python has two significant variants that are not completely viable: Python 2 and Python 3. For instance, 3.5.7, 3.7.2, and 3.8.0 are all important for the Python 3 significant adaptation. 

MINOR - These deliveries are bringing new highlights and capacities. For instance, 3.6.6, 3.6.7, and 3.6.8 are all essential for the Python 3.6 minor rendition. 

Miniature - The new miniature forms contain different bug fixes and upgrades. 

Improvement discharges have extra qualifiers. For more data, perused the Python "Improvement Cycle" documentation. 

Checking Python Version

Python is pre-introduced on most Linux disseminations and macOS. On Windows, you need to download and introduce it. 

To discover which form of Python is introduced on your framework run the python - rendition or python - V order: 

python --version

The order will print the default Python adaptation, for this situation, that is 2.7.15. The variant introduced on your framework might be unique. 

Python 2.7.15+

The default rendition of Python will be utilized by all contents that have/usr/receptacle/python set as a translator in the content's thing line. 

Some Linux disseminations have various variants of Python introduced simultaneously. For the most part, the Python 3 double is named python3, and the Python 2 parallel is named python or python2, however that may not generally be the situation. 

You can check in the event that you have Python 3 introduced by composing: 

python3 --version
Python 3.6.8

Python 2 help closes in 2020. Python 3 is the present and fate of the language. 

At the hour of composing this article, the most recent significant arrival of the Python is variant 3.8.x. Odds are that you have a more established adaptation of Python 3 introduced on your framework. 

On the off chance that you need to introduce the most recent variant of Python, the methodology relies upon the working framework you are running. 

Programmatically Checking Python Version

Python 2 and Python 3 are on a very basic level unique. The code that is written in Python 2.x may not work in Python 3.x. 

The sys module that is accessible in all Python variants gives framework explicit boundaries and capacities. sys.version_info permits you to decide the Python form introduced on the framework. It is a tuple that contains the five adaptation numbers: major, minor, miniature, releaselevel, and sequential. 

Let' say you have a content that needs at any rate Python rendition 3.5, and you need to check whether the framework meets prerequisites. You can do that by basically checking the major and minor forms: 

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)

In the event that you run the content utilizing Python form under 3.5 it will deliver the accompanying yield: 

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

To compose Python code that runs under both Python 3 and 2, utilize the future module. It permits you to run Python 3.x-viable code under Python 2. 

Conclusion

Discovering what rendition of Python is introduced on your framework is exceptionally simple, simply type python - variant. 

Don't hesitate to leave a remark in the event that you have any inquiries.




CFG