What is my python version linux

How to Check Python Version in Linux

The knowledge of the correct version of python is important for many tasks. For example, we may want to install a library that requires a specific version of python to check if our system has that version already installed. For example, in the Terminal formatting tutorial using rich, we have learned about a library called rich, which is used for terminal formatting in python. Still, the library requires Python 3.6.1 or later installed in the system.

This tutorial will see how to check the python version running on your Linux system. Many Linux systems also have multiple Python versions installed; we will also see how to know which versions are installed and how to use a particular version.

Python Versioning

Python uses the semantic versioning technique to give a version to its new release. The Python versioning contains three numbers to identify each release uniquely. The production-ready releases follow the below-shown syntax for versioning.

For example, In Python Version 3.8.6, 3 is the major release(also known as python3), 8 is the minor release number, and 6 is the micro release number. The below list shows some more details about them.

  • MAJOR: The Python programming language has two major releases that are not fully compatible with each other. The major version has many subversions. For example, python 3 has some subversions like 3.8.3, 3.4.5, etc.
  • MINOR: These releases are bringing new functions, classes, features, etc. For example, python 3.7.x, python 3.8.x, etc.
  • MICRO: The python micro releases contains many bug fixes and improvements.

The python micro and minor releases are somewhat compatible, and a new version can run the same code without much problem. Still, the major releases were not compatible with each other. If we write software using Python2, it will have some problems while running using the python3 interpreter.

Check all Python Versions Installed in the System

Many Linux and Mac Operating Systems come preinstalled with Python. Since all the Linux programs do not use the same python version, there are many python versions pre-installed with Linux. In Linux, the python binaries were installed in the directories /usr/bin/ or /usr/local/bin/. We can check which python version is installed in our system by typing the following command in the Linux terminal.

We will get the names of all the files starting with python present in the /usr/bin/ directory. I got the following output while running the command in my system. I have five different versions of python binaries installed in the path /usr/bin/. They are python, python2, python2.7, python3, python3.8. You may get some other output depending on the python installation in your system.

How to run code with a specific python version

To run a python script using a custom python binary, use the binary file name in the command line following the python script name. See the below command for illustration.

Читайте также:  Linux выход из screen

We can also run the python script using python 2.7 by running the following command.

Checking Python Version

There are two ways to check the python version; the first way is the simplest by using the command line, while the second way is by using writing a python script. Let us discuss both the ways in detail.

Using Command Line

We can check the version of python by using the command line. To display python’s version, run the python binary file following the –version or -V command. See the below examples for illustration.

Output:

Using the –version parameter:

Using Python Script

We can also use a python script to check which python version is used to run the script. This technique has a benefit if our python program can be run only in a specified version. For example, assume you have written a python code that used f-strings for formatting strings. F-strings have been included in python in the 3.6 release, and so the code will work only on python version 3.6 or later.

We can improve the program by checking the python version used programmatically and using the if/else condition to check whether f-strings are supported by the version. If f-string is supported, then continue with the code; else, run some other code block.

We have many modules that can help us to get the version of python. In this tutorial, we were using the sys and platform library. These libraries are present in the python standard library, so we have no installation problem for these libraries.

Using the sys library

To get the version number using the sys library, we have two methods. The first uses the sys.version string, which contains some details like the python version, compiler version, etc. The second one uses the sys.version_info, which contains a tuple containing some python version info. The below code shows a simple use of the sys.version to check the version.

# importing the required modules import sys # using the version string of # the sys module print(sys.version)

Источник

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.

Читайте также:  Узнать версию драйвера nvidia linux

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

Then, for any of the operations systems above, you simply type python —version OR python -V, on the command line and press Enter . You’ll get a result like this:

python --version Python 3.8.3 python -V Python 3.8.3

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.

Starting from Python 3.6, you can also use python -VV (this is two Vs, not a W) to get more detailed information about your Python version:

python -VV Python 3.8.3 (default, Jul 2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)]

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:

import sys print (sys.version)
# 3.8.3 (default, Jul 2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)]

To check the Python version using the platform module, use the following code:

import platform print(platform.python_version())

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:

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

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.

Читайте также:  Linux cat grep cut

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.

Thanks for reading, and happy learning!

Источник

How to Check Python Version for Mac, Windows, and Linux

Web developers, engineers, and other software professionals need to know how to check Python version installed on their machines. Although the process is simple, some beginners may find it hard to figure out how to check Python version.

If you’re new to Python, we suggest taking a Python course to become familiar with the syntax and subtleties of the language.

In this article, we’ll guide you through a few easy steps to check Python version on your Mac, Windows, or Linux machine. All you need to begin is a computer with a Python release installed.

What is Python?

Python is a programming language used in web development, machine learning, and automation tasks. It is object-oriented, interpreted, and has a clear, simple syntax that makes it easy to learn and use.

It is one of the most popular and widely used languages, rivaling Javascript, Java, and C.

Python’s core library is extensive and freely distributed. These libraries and packages extend Python’s standard functionality.

The #1 Reason You Need to Know How to Check Python Version

There are many different versions of Python, and each version operates slightly differently with other dependencies and packages you have installed. Therefore, it’s crucial to ensure you have the correct version of Python installed to support the other dependencies in your project.

It is possible to have more than one version of Python installed on your computer. If you are a serious developer, you might simultaneously work on several applications requiring different Python versions.

For this reason, developers often run Python inside a virtual environment. In addition, it’s common to run a separate virtual environment for each Python project you work on.

A virtual environment is like a virtual machine that runs on your computer and keeps all the Python interpreters, versions, packages, dependencies, and scripts isolated from other Python projects on your computer.

Источник

Оцените статью
Adblock
detector