- How to Check Python Version?
- Check Python Version: Command-Line
- Check Python Version: Script
- What are the Different Python Versions?
- How to Upgrade to a Newer Version?
- What if Your Computer has Multiple Python Versions Installed?
- Python 2 vs Python 3
- Learn More
- Conclusion
- Как проверить версию Python в скриптах
- sys.version способ проверки версии Python
- sys.version_info способ проверки версии Python
- Метод platform.python_version() для проверки версии на Python
- six модульный метод проверки версии Python
- Сопутствующая статья — Python Version
How to Check Python Version?
Before learning how to check the python version, we should first understand Python and its versions briefly.
Python is an object-oriented, high-level interpreted scripting language.
Let us discuss some of the various important points regarding the Python programming language :
- Python programming language is a highly readable language making it easy to understand for new coders.
- ABC programming language can be considered the predecessor of the Python programming language. Modula-3 also has some influence on the Python programming language.
- The Python programming language was developed by Guido Van Rossum in the Netherlands in 1991.
- Python language is widely used in various technical fields such as Artificial Intelligence (AI), Machine Learning (ML), Scientific Calculation, Desktop Application, Web Development (using Django , and Flask framework, etc.) Mobile Application Development, etc.
Let us learn how to check the python version using the command line and scripts.
Check Python Version: Command-Line
Let us first learn how to check the python version using the various command-line commands used in different operating systems.
In Windows operating systems, we can use the command python —version to check the python version.
The steps to check the python version in Windows are very simple:
- Open the Windows command prompt or Windows Powershell, and enter the following command on the shell :
If we have a Python interpreter installed on the Windows operating system then the command will show the version like :
- The version number may differ, but if the Python interpreter is installed in a system, it will similarly show the version.
- We can also search for Python present on the system. We can press the Windows key to launch the search menu and then we can type Python. If the system has installed Python, it will show the Python version.|
In macOS operating systems, we can use the same command, python —version , to check the python version.
The steps to check the python version in macOS are very simple:
- Open the macOS Terminal, and enter the following command on the shell or terminal :
If we have a Python interpreter installed on the macOS operating system, then the command will show the version like :
In Linux operating systems, we can use the same command, python —version to check the python version.
The steps to check the python version in Linux are very simple:
- Open the Linux terminal or shell and type the following command :
If we have a Python interpreter installed on the Linux operating system, then the command will show the version like :
Note:
We can use the commands such as python —version or python -v or python -VV . All of these commands will provide a similar result. The -VV option is a new command added since Python 3.6 . Which can be used for detailed information than -V or —version .
Check Python Version: Script
Let us now learn how to check the python version using scripts. We can use the sys module or the platform module of the standard library to check the python version in various operating systems. The script will be the same for all operating systems such as Windows , Mac , Linux , etc.
The script is very useful for checking the python version even tho we have various versions of Python installed on our system. Let us see the script and its related information.
i) Various information string: sys.version :
We can use the .version method in the sys (system) module to print the version of the Python interpreter installed on the system. The sys. version method returns a string that contains the version number of the Python interpreter installed in the system. It also returns additional information about the build number and the compiler used.
let us now look at the script :
ii) Tuple of version numbers: sys.version_info :
We can also use the .version_info method in the sys (system) module to print the version of the Python interpreter installed on the system. The sys.version_info method returns a tuple containing five components of the Python interpreter version number: the major version release number, the minor version release number, the micro version release number, the release level, and the serial number.
let us now look at the script :
Note :
The release level element is a string, and the other output elements are integers.
iii) Version number string: platform.python_version() :
We can also use the .python_version() method present in the platform module to print the version of the Python interpreter installed on the system. The platform.python_version() method returns a string in the form major.minor.patchlevel . The string denotes the major release number then the minor release number, and then the patch level, each separated using a dot( . )
let us now look at the script:
The script is handy when we want to check the Python interpreter version number as a simple string only.
iv) Tuple of version number strings: platform.python_version_tuple() :
We can also use the .python_version_tuple() method present in the platform module to print the version of the Python interpreter installed on the system. The platform.python_version_tuple() method returns a tuple in the form ‘major’, ‘minor’, ‘patchlevel’ . The tuple denotes the major release number, then the minor release number, and then the patch level, each separated using a comma( , ). Each element of the tuple is in the form of a string.
let us now look at the script:
What are the Different Python Versions?
We have already discussed what Python programming language is and how to check the python version, let us briefly discuss the various popular versions of the Python programming language.
- The first version of Python was released in 1994 named Python 1.0 . Python 1.0 had various features like lambda function, map , filter , reduce , etc.
- The second version of Python was released in 2000, with many major new features such as garbage collection systems, list comprehensions, etc.
- The third and latest major Python version release was released in 2008. This version of Python ( Python 3.0 ) is also known as Py3K.
Refer to the below-specified table to check the various Python interpreter versions and their release dates.
Python Versions | Release Date |
---|---|
Python 1.0 | January 1994 |
Python 1.5 | December 31, 1997 |
Python 1.6 | September 5, 2000 |
Python 2.0 | October 16, 2000 |
Python 2.1 | April 17, 2001 |
Python 2.2 | December 21, 2001 |
Python 2.3 | July 29, 2003 |
Python 2.4 | November 30, 2004 |
Python 2.5 | September 19, 2006 |
Python 2.6 | October 1, 2008 |
Python 2.7 | July 3, 2010 |
Python 3.0 | December 3, 2008 |
Python 3.1 | June 27, 2009 |
Python 3.2 | February 20, 2011 |
Python 3.3 | September 29, 2012 |
Python 3.4 | March 16, 2014 |
Python 3.5 | September 13, 2015 |
Python 3.6 | December 23, 2016 |
Python 3.7 | June 27, 2018 |
Python 3.8 | October 14, 2019 |
How to Upgrade to a Newer Version?
We can easily upgrade to the latest or newer version of the Python interpreter by simply going to the (visiting) Python downloads page and downloading the latest version.
What if Your Computer has Multiple Python Versions Installed?
We can have multiple Python interpreter versions installed on our system. We can run a particular python program (or python script) using the specified Python interpreter version.
For example, if we have Python 2.6 and Python 3.6 installed on our system, then we can run a file (let’s say program.py ) using both interpreters.
Example : (For Python 3.6)
Example : (For Python 2.6)
Python 2 vs Python 3
Both Python 2 and Python 3 are very popular interpreters, let us discuss some of the major differences between both releases.
Python 2 | Python 3 |
---|---|
Python 2 was released in 2000. | Python 3 was released in 2008 . |
Python 2 interpreter considers print as a statement but not a function. | Python 3 interpreter considers print() as a function. |
The strings are by default stored as UNICODE characters in Python 2 . | The strings are by default stored as ASCII characters in Python 2 . |
The division of two integral values in Python 2 results in another integral value. For example, 7/2 yields 3 . | The division of two integral values in Python 3 results in a floating-point value. For example, 7/2 yields 3.5 |
Exception in Python 2 was dealt with in the enclosed notations. | Exceptions in Python 3 are enclosed in parentheses. |
We used the xrange() function for iteration in Python 2 . | We use the range() function for iteration in Python 3 . |
Python 2 is not longer in use since the year 2020 . | Python 3 is more popular than Python 3 but in some places, we still use Python 2 . |
With some effort, we can port the Python 2 code to Python 3 . | Python 3 has no backward compatibility with the previous version i.e. Python 2 .> |
Learn More
To learn more about the Python Programming Language, refer to the articles :
Conclusion
- In Windows operating systems, we can use the command python —version to check the python version. We just need to open the Windows command prompt, or Windows Powershell, and enter the command.
- In macOS operating systems and Linux operating systems, we can use the same command python —version to check the python version.
- We can use the sys. version method present in the sys (system) module to print the version of the Python interpreter installed on the system.
- The sys. version method returns a string that contains the version number of the Python interpreter installed in the system.
- The sys.version_info method returns a tuple that contains a major version number, the minor number, the micro version number, the release level, and the serial number.
- The platform.python_version() method returns a string in the form major.minor.patchlevel .
- We can also use the .python_version_tuple() . The platform.python_version_tuple() method returns a tuple in the form ‘major’, ‘minor’, ‘patchlevel’ .
- The tuple denotes the major release number, the minor release number, and then the patch level separated using a comma( , ). Each element of the tuple is in the form of a string.
- We can have multiple Python interpreter versions installed on our system. We can run a particular python program (or python script) using the specified Python interpreter version.
Как проверить версию Python в скриптах
- sys.version способ проверки версии Python
- sys.version_info способ проверки версии Python
- Метод platform.python_version() для проверки версии на Python
- six модульный метод проверки версии Python
В различных обстоятельствах нам необходимо знать версию Python, а точнее — версию интерпретатора Python, выполняющего файл скрипта Python.
sys.version способ проверки версии Python
Информация об этой версии может быть получена из sys.version в модуле sys .
>>> 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 способ проверки версии Python
sys.version возвращает строку, содержащую информацию о читаемой человеком версии текущего интерпретатора Python. Но такая информация как major release number и micro release number требует дополнительной обработки для дальнейшего использования в кодах.
>>> import sys >>> sys.version_info sys.version_info(major=2, minor=7, micro=10, releaselevel='final', serial=0)
Вы можете сравнить текущую версию со справочной просто используя операторы > , >= , == ,
>>> import sys >>> sys.version_info >= (2, 7) True >>> sys.version_info >= (2, 7, 11) False
Мы могли бы добавить в скрипты assert , чтобы убедиться, что скрипт работает с требованием минимальной версии на Python.
import sys assert sys.version_info >= (3, 7)
Это вызовет AssertionError , если интерпретатор не соответствует требованию версии.
Traceback (most recent call last): File "C:\test\test.py", line 4, in module> assert sys.version_info >= (3, 7) AssertionError
Метод platform.python_version() для проверки версии на Python
python_version() в модуле platform возвращает версию Python в виде строки major.minor.patchlevel .
>>> from platform import python_version >>> python_version() '3.7.0'
Или аналогично sys.version_info , platform также имеет метод возврата Python версии в виде кортежа (major, minor, patchlevel) из строк — python_version_tuple() .
>>> import platform >>> platform.python_version_tuple() ('3', '7', '0')
six модульный метод проверки версии Python
Если вам нужно только проверить, является ли версия Python 2.x или Python 3.x, вы можете использовать six модуль для выполнения этой работы.
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.