How to know python version
How to know python version
Как проверить версию Python
How to Check Python Version
В этом руководстве объясняется, как проверить, какая версия Python установлена в вашей операционной системе с помощью командной строки. Это может быть полезно при установке приложений, которым требуется определенная версия Python.
Мы также покажем вам, как программно определить, какая версия Python установлена в системе, где выполняется скрипт Python. Например, при написании сценариев Python вам необходимо определить, поддерживает ли сценарий версию Python, установленную на компьютере пользователя.
Версии Python
Готовые к выпуску версии Python имеют следующую версию:
Например, в Python 3.6.8 3 это основная версия, 1 дополнительная версия и 2 микро версия.
Проверка версии Python
Python предустановлен в большинстве дистрибутивов Linux и macOS.
Вы можете проверить, установлен ли Python 3, набрав:
Если вы хотите установить последнюю версию Python, процедура зависит от операционной системы, которую вы используете.
Программная проверка версии Python
Python 2 и Python 3 принципиально разные. Код, написанный на Python 2.x, может не работать в Python 3.x.
Допустим, у вас есть скрипт, который требует как минимум Python версии 3.5, и вы хотите проверить, соответствует ли система требованиям. Вы можете сделать это, просто проверив major и minor версии:
Если вы запустите скрипт, используя версию Python менее 3.5, он выдаст следующий вывод:
Чтобы написать код Python, который работает под Python 3 и 2, используйте future модуль. Это позволяет запускать Python 3.x-совместимый код под Python 2.
Вывод
note.nkmk.me
This article describes how to check, get, and print the Python version installed and executed 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.
As in the example above, in some environments, the Python2.x series is assigned to the python command, and the Python3.x series is assigned to the python3 command.
You can use the standard library sys module or platform module to get the version of Python that is actually running.
The same script can be used on Windows, Mac, and Linux, including Ubuntu.
It is useful for checking which version of Python is running in an environment where multiple versions of Python are installed. Even though you thought Python3 was running, there was a case where Python2 was running, so if something goes wrong, check it once.
It is also be used when you want to switch operations depending on whether it is Python2 or Python3.
Various information string: sys.version
sys.version is a string indicating 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.7.4 documentation
Tuple of version numbers: sys.version_info
sys.version_info is a tuple (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 — System-specific parameters and functions — Python 3.7.4 documentation
You can get each value by specifying an index.
For example, if you want to get a major version:
You can switch the process between Python2 or Python3.
Use sys.version_info.minor if you want to switch operations by a minor version.
Version number string: platform.python_version()
It is useful when you want to get the version number as a simple string.
How to Check Python Version in Linux, Mac, & Windows
Home » SysAdmin » How to Check Python Version in Linux, Mac, & Windows
Which version of Python do I have installed?
Python is a popular programming language. Like many other programming languages, there can be several different versions organized by release date. Certain applications may require a specific version of Python.
In this tutorial, learn how to check the Python version on Windows, Linux, or macOS systems.
Access to a command-line/terminal window:
There are different versions of Python, but the two most popular ones are Python 2.7.x and Python 3.7.x. The x stands for the revision level and could change as new releases come out.
When looking at the version number, there are usually three digits to read:
While major releases are not fully compatible, minor releases generally are. Version 3.6.1 should be compatible with 3.7.1 for example. The final digit signifies the latest patches and updates.
Python 2.7 and 3.7 are different applications. Software that’s written in one version often will not work correctly in another version. When using Python, it is essential to know which version an application requires, and which version you have.
Python 2 will stop publishing security updates and patches after 2020. They extended the deadline because of the large number of developers using Python 2.7. Python 3 includes a 2 to 3 utility that helps translate Python 2 code into Python 3.
How to Check Python Version in Linux
Most modern Linux distributions come with Python pre-installed.
To check the version installed, open a terminal window and entering the following:
How to 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 idea to check.
Open Windows Powershell, and enter the following:
If you have Python installed, it will report the version number.
Alternately, use the Windows Search function to see which version of Python you have:
Press the Windows key to start a search, then type Python. The system will return any results that match. Most likely a match will show something similar to:
This defines which major and minor revision (3.x or 2.x) you are using.
How to Check Python Version in MacOS
If using a MacOS, check the Python version by entering the following command in the terminal:
The system will report the version.
Note: In some cases, this will return a screen full of information. If that happens, just scan through the file locations for the word python with a number after it. That number is the version.
Checking a System with Multiple Versions of Python
Python2 and Python3 are different programs. Many programs upgrade from the older version to the newer one. However, Python 2.7.x installations can be run separately from the Python 3.7.x version on the same system.
Python 3 is not entirely backward compatible.
To check for Python 2.7.x:
To check the version of Python 3 software:
Note: Python does not have a built-in upgrade system. You’ll need to download the latest version and install it.
How to Check Python Version in Script
When writing an application, it is helpful to have the software check the version of Python before it runs to prevent crashes and incompatibilities.
Use the following code snippet to check for the correct version of Python:
When this script runs, it will test to see if Python 3.6 is installed on the system. If not, it will send a notification and displays the current Python version.
Note: One of the common issues in working with Python and datasets is missing data. Learn how to handle missing data in Python.
You should now have a solid understanding of how to check for the version of Python installed in several different operating systems. Python is a powerful programming language, thus it’s important to understand its different versions.
If you want to learn how to upgrade Python to a newer version on Wondows, macOs, and Linux, check our article how to upgrade Python to 3.9.
How To Check Python Version?
Default Python Version
Every Python installation have a default version even there is two version of Python. If we can python command and enter to the check version.
Default Python Version
We can see from first line which is like Python 2.7.15rc1 which is simply Python2. We also get the gcc version which compiled this Python binary.
python_version() Function
Another function to check python version is python_version() function. But first we need to import platform module like below.
python_version() Function
version Variable
We can also use version variable which is provided by sys module.
version Variable
Python –version Option
OR to learn Python3 subversion
OR to learn Python2 subversion
Listing Python Interpreters
As Python 2 and 3 version there are different interpreters which provides Python programming language features. We can list these interpreters as files like below. We will use following ls command.
Listing Python Interpreters
How to Check Your Python Version
Chances are you have heard about Python 2 and Python 3. Although they are two versions of the same language, they have different syntax; code written in Python 3 might not work in Python 2. So, let’s discover how you can check your Python version on the command line and in the script on Windows, macOS, and Linux systems.
Python is one of the most popular programming languages. With its simple syntax, high productivity, and amazing open-source libraries, Python can be used for just about anything.
However, you might have seen that some people use Python 2, while others prefer Python 3. The difference between these two versions is quite significant – it’s not just about fixing some bugs and adding a few new features. If the application is written in Python 2, you may not be able to run it using Python 3.
So, you should definitely know the version of Python installed on your computer. Let’s see how you can check the Python version. We’ll start with the command line.
Check Python Version: Command Line
You can easily check your Python version on the command line/terminal/shell. Let’s first recall how we can access the command line in different operating systems.
Windows
macOS
Linux
Depending on your Python distribution, you may get more information in the result set. However, the number next to Python is the version number, which is what we are looking for. In this case, the full version number is 3.8.3.
Usually, we are interested in the major version – Python 2 or Python 3. This is indicated by the first number of the full version number. This number is 3 in our case, which means that we have Python 3 installed on our computer.
Check Python Version: Script
Sometimes you may want to check the version of Python when you are coding an application (i.e. inside the script). This is especially useful when you have multiple Python versions installed on your computer. To check which Python version is running, you can use either the sys or the platform module. The script will be the same for Windows, macOS, and Linux.
To check the Python version using the sys module, write:
And you’ll get:
To check the Python version using the platform module, use the following code:
The output will be as follows:
Both code snippets output the Python version in the string format. If necessary, you can also get the version number in the tuple format. The tuple will contain five components: major, minor, micro, release level, and serial:
Of course, you can easily obtain the individual components of this tuple using an index (e.g. sys.version_info[0] ) or a name (e.g. sys.version_info.major ).
Pretty simple, right? No wonder Python is so popular.
Python 2 or Python 3?
Now we know how to check the Python version. But what’s the difference between the two versions?
Python 2 is an older version that was actively used in software development and IT operations (DevOps). However, it is no longer under development and has been discontinued starting from January 1, 2020. This implies that any bugs or security problems discovered in Python 2 are no longer being addressed by Python developers. Python’s volunteer developers advise that people using Python 2 move to Python 3 as soon as possible.
Python 3 was first introduced in 2008. It’s syntax and behavior is quite different from Python 2, but it’s generally believed that Python 3 is simpler and easier to understand.
As Python 2 is no longer supported, you should definitely choose Python 3 if you are writing a new application or just starting to learn Python. The only reason to learn Python 2 is if your company’s code is written in Python 2 and you need to work with it. This shouldn’t be often the case, especially once Python 2 has been discontinued for a while.
Time to Practice Python!
Do you want to learn Python 3? Join the track Learning Programming with Python on LearnPython.com, where you will be introduced to the fundamentals of programming – not just in theory but with over 400 interactive coding challenges.
The track starts with Python Basics: Part 1, a course that teaches students how to create the simplest Python applications. This course (and the track itself) are aimed at students with no prior IT background. If you are already familiar with Python’s basics, join one of the advanced courses on LearnPython.com and learn how to work with strings, JSON files, and CSV files in Python.
Professionals across different industries enjoy the benefits of this simple and effective programming language. You can join them very soon! It doesn’t take much time to become proficient in Python, especially if you plan your studying activities appropriately.