Какая версия python ubuntu

What version of Python do I have?

@TejasKale Better to run ls /usr/bin/python* (or ls /usr/bin/*python* if you really want files with python anywhere in the name). That way, ls still formats its output for a terminal (and you get multiple columns and, with the default ls alias in Ubuntu, colorization).

9 Answers 9

You can use python -V (et al.) to show you the version of Python that the python command resolves to. If that’s all you need, you’re done. But to see every version of python in your system takes a bit more.

In Ubuntu we can check the resolution with readlink -f $(which python) . In default cases in 14.04 this will simply point to /usr/bin/python2.7 .

We can chain this in to show the version of that version of Python:

$ readlink -f $(which python) | xargs -I % sh -c 'echo -n "%: "; % -V' /usr/bin/python2.7: Python 2.7.6 

But this is still only telling us what our current python resolution is. If we were in a Virtualenv (a common Python stack management system) python might resolve to a different version:

$ readlink -f $(which python) | xargs -I % sh -c 'echo -n "%: "; % -V' /home/oli/venv/bin/python: Python 2.7.4 

The fact is there could be hundreds of different versions of Python secreted around your system, either on paths that are contextually added, or living under different binary names (like python3 ).

Читайте также:  Что делать если java runtime environment not found

If we assume that a Python binary is always going to be called python and be a binary file, we can just search the entire system for files that match those criteria:

$ sudo find / -type f -executable -iname 'python*' -exec file -i '<>' \; | awk -F: '/x-executable; charset=binary/ ' | xargs readlink -f | sort -u | xargs -I % sh -c 'echo -n "%: "; % -V' /home/oli/venv/bin/python: Python 2.7.4 /media/ned/websites/venvold/bin/python: Python 2.7.4 /srv/chroot/precise_i386/usr/bin/python2.7: Python 2.7.3 /srv/chroot/trusty_i386/usr/bin/python2.7: Python 2.7.6 /srv/chroot/trusty_i386/usr/bin/python3.4: Python 3.4.0 /srv/chroot/trusty_i386/usr/bin/python3.4m: Python 3.4.0 /usr/bin/python2.7: Python 2.7.6 /usr/bin/python2.7-dbg: Python 2.7.6 /usr/bin/python3.4: Python 3.4.0 /usr/bin/python3.4dm: Python 3.4.0 /usr/bin/python3.4m: Python 3.4.0 /web/venvold/bin/python: Python 2.7.4 

It’s obviously a pretty hideous command but this is again real output and it seems to have done a fairly thorough job.

Источник

Check Python Version

The following article will provide you with the information on how to check Python Version on your operating system. You will learn how to check for python version using the python command as well as how to determine the python version programmatically, from python console and using python script.

Difficulty

Conventions

  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Instructions

You system may have both Python 2 and Python 3 version installed. List installed python binary executable to see what version is installed on your system:

$ ls /usr/bin/python* /usr/bin/python /usr/bin/python2 /usr/bin/python2.7 /usr/bin/python3 /usr/bin/python3.6 /usr/bin/python3.6m /usr/bin/python3m

Please note the action minor python version may differ from system to system. Another path the Python binaries can be installed in is /usr/local/bin/ . As you can see our system has both Python 2 and Python 3 versions installed.

Check Python Version from command line

Next, we are going to retrieve the version number from the command line:

Python 2

$ /usr/bin/python -V OR /usr/bin/python --version Python 2.7.15rc1

Python 3

$ /usr/bin/python3 -V OR /usr/bin/python3 --version Python 3.6.5

Check Python Version by using interpreter

Instead of retrieving the python version from the linux command line we can ask directly the python interpreter to perform version check.

Python 2

$ python Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) [GCC 7.3.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import platform >>> platform.python_version() '2.7.15rc1' >>>

Python 3

$ python3 Python 3.6.5 (default, Apr 1 2018, 05:46:30) [GCC 7.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import platform >>> platform.python_version() '3.6.5' >>>

Check version using script

The following script will check python version and print the version number to the standard output. Save the below python code as a new file named check-python-version.py :

import platform python_version=platform.python_version() print (python_version) 

Once ready run the check-python-version.py script to obtain python version. Make sure to use appropriate Python interpreter such as python or python3.

Python 2

$ python check-python-version.py 2.7.15rc1

Python 3

$ python3 check-python-version.py 3.6.5

From the above commands we can determine the system’s python version is 2.7.15 and 3.6.5 for both, Python 2 and Python 3 respectively.

Check Python Version on Ubuntu Linux System

Comments and Discussions

NEWSLETTER

Subscribe to Linux Career Newsletter to receive latest news, jobs, career advice and featured configuration tutorials.

WRITE FOR US

LinuxConfig is looking for a technical writer(s) geared towards GNU/Linux and FLOSS technologies. Your articles will feature various GNU/Linux configuration tutorials and FLOSS technologies used in combination with GNU/Linux operating system.

When writing your articles you will be expected to be able to keep up with a technological advancement regarding the above mentioned technical area of expertise. You will work independently and be able to produce at minimum 2 technical articles a month.

TAGS

  • VIM tutorial for beginners
  • How to install the NVIDIA drivers on Ubuntu 20.04 Focal Fossa Linux
  • Bash Scripting Tutorial for Beginners
  • How to check CentOS version
  • How to find my IP address on Ubuntu 20.04 Focal Fossa Linux
  • Ubuntu 20.04 Remote Desktop Access from Windows 10
  • Howto mount USB drive in Linux
  • How to install missing ifconfig command on Debian Linux
  • AMD Radeon Ubuntu 20.04 Driver Installation
  • Ubuntu Static IP configuration
  • How to use bash array in a shell script
  • Linux IP forwarding – How to Disable/Enable
  • How to install Tweak Tool on Ubuntu 20.04 LTS Focal Fossa Linux
  • How to enable/disable firewall on Ubuntu 18.04 Bionic Beaver Linux
  • Netplan static IP on Ubuntu configuration
  • How to change from default to alternative Python version on Debian Linux
  • Set Kali root password and enable root login
  • How to Install Adobe Acrobat Reader on Ubuntu 20.04 Focal Fossa Linux
  • How to install the NVIDIA drivers on Ubuntu 18.04 Bionic Beaver Linux
  • How to check NVIDIA driver version on your Linux system
  • Nvidia RTX 3080 Ethereum Hashrate and Mining Overclock settings on HiveOS Linux

LATEST TUTORIALS

  • How to embed documentation in Bash scripts
  • How to check disk space on Raspberry Pi
  • How to check Internet connection on Raspberry Pi
  • Using a USB Drive with Your Raspberry Pi: Tips and Tricks
  • Automating Raspberry Pi: How to Autostart Programs
  • Powering Up: How to Turn on Your Raspberry Pi
  • Enabling SSH on Raspberry Pi: A Comprehensive Guide
  • Boot Your Raspberry Pi from a USB: A Tutorial
  • Checking Your Raspberry Pi’s OS Version
  • Install an OS on Your Raspberry Pi: Step-by-Step
  • Finding Your Raspberry Pi’s IP Address: A Quick Guide
  • Easy Steps to Update Your Raspberry Pi
  • Connecting Your Raspberry Pi to Wi-Fi: A How-To
  • How to install RealVNC viewer on Linux
  • How to check Raspberry Pi RAM size and usage
  • How to check Raspberry Pi model
  • Understanding UEFI and BIOS in Relation to Linux Nvidia Driver Installation
  • How to orchestrate Borg backups with Borgmatic
  • How to monitor filesystem events on files and directories on Linux
  • Debian USB Firmware Loader Script

Источник

Check Your Python Version

Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.

Python reigns as one of the most popular programming languages, with a wide range of programs and developer tools relying on it. In fact, your system likely already has at least one version of Python installed.

Many tools and Python development libraries require a particular version of Python. Thus, you may want to know where you can find information on your installed Python version. This can help you make decisions about compatibility, upgrades, and more.

This tutorial shows you how to check your Python version, for both Python 2 and Python 3. Here, you can find the command line method as well as a Python script method for retrieving the current Python version.

How to Check the Python Version from the Command Line

The Python command comes with a command line option of —version that allows you to see your installed version.

It works just as straightforwardly as it sounds. Enter the following command from your command line, and you should get an output similar to the one shown below:

Python 2 vs Python 3

Some systems distinguish between Python 2 and Python 3 installations. In these cases, to check your version of Python 3, you need to use the command python3 instead of python .

In fact, some systems use the python3 command even when they do not have Python 2 installed alongside Python 3. In these cases, you only have the python3 command.

The command for checking the installed version of Python 3 remains otherwise the same — just use python3 with the —version option:

How to Check the Python Version from Python

You can also check your installed Python version from within Python itself. Using either a script or the Python shell, you can use one of the code snippets below to print your Python version.

Both options work equally well regardless of your system. The choice of which option to use really comes down to what format you want the output in.

Using sys

The sys module has a variable you can reference to get the current Python version. Below you can see an example of how the sys module’s version variable renders the current Python version. This code first imports the sys module then prints out the contents of the version variable:

3.8.10 (default, Jun 22 2022, 20:18:18) [GCC 9.4.0]

As you can see, the sys.version variable contains more information about your installed Python version than just the number. For that reason, sys is a good module to turn to when you want more verbose version information.

Using platform

The platform module includes a function that fetches the current version of Python. The example code below uses this function to print the current Python version number. It first imports the platform module; then, the python_version function returns the version number to the print function:

The output from the platform.python_version is more minimal compared to the sys module’s version variable. This makes the platform module more useful for cases when you only need the version number. For example, this method helps when you want to design a program to parse the Python version and act accordingly.

Conclusion

With that, you have everything you need for checking your current Python version. The steps above cover you whether you need to see the Python version from the command line or from within a Python script.

You can continue learning about Python with our collection of Python guides. We cover everything from fundamental Python concepts to building Python web applications.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This page was originally published on Monday, August 15, 2022.

Источник

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