5 Best Ways to Check Python Version from the Command Line in Linux

πŸ’‘ Problem Formulation: You are using a Linux-based operating system and want to determine which version of Python is currently installed on your system. This information is crucial when working on projects that require a specific Python version. For example, if a script requires Python 3.7, you need to ensure the system meets this requirement. The desired output is a clear indication of the Python version present on your system.

Method 1: Using the python –version Command

The python --version command is a straightforward method to check the Python version in most Linux distributions. It’s widely used when legacy Python 2.x might be the default python interpreter on the system.

Here’s an example:

python --version

Output:

Python 2.7.18

This command invokes the default ‘python’ binary and outputs the version number. If Python 2.x is the default, as is the case on many older systems, this will output that version. It’s quick and simple but not effective if you expect a Python 3.x version and the system defaults to Python 2.x.

Method 2: Using the python3 –version Command

If you’re looking for the version of Python 3.x specifically, you can use the python3 --version command. This is especially useful on systems where Python 3.x is not the default interpreter.

Here’s an example:

python3 --version

Output:

Python 3.8.5

This command invokes the ‘python3’ binary and shows its version. It is crucial for systems with multiple Python versions installed as it targets Python 3 explicitly, and therefore, ensures you get the correct version number.

Method 3: Using the pyenv versions Command

For those who use ‘pyenv’ for managing multiple Python versions, the pyenv versions command is invaluable for listing all installed Python versions and indicating the currently active Python version with an asterisk (*).

Here’s an example:

pyenv versions

Output:

* 3.8.5 (set by /home/user/.pyenv/version)
  3.9.1

It outputs all Python versions managed by ‘pyenv’ and highlights the currently active one. It is an excellent method for environments where multiple versions of Python are used and need to be switched frequently.

Method 4: Checking Python Version in a Python Script

You can also determine the Python version programmatically within a Python script by using the sys module. This can provide more detailed information, including the major, minor, and patch level of Python.

Here’s an example:

python -c "import sys; print(sys.version)"

Output:

3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0]

This command runs a short Python script that imports sys and prints the version information, which gets displayed in your terminal. This method gives a precise version number and compilation details, which can be very useful for debugging and environment confirmation purposes.

Bonus One-Liner Method 5: Using the platform Module

Another one-liner that can be used is invoking Python with the platform module from the command line to get verbose version information.

Here’s an example:

python -c "import platform; print(platform.python_version())"

Output:

3.8.5

This snippet imports the platform module and prints the simple version information. It’s a bit more concise than using the sys module but still gives the complete version number.

Summary/Discussion

  • Method 1: Using python –version. Quick and easy for legacy systems. Doesn’t work for checking Python 3 specifically.
  • Method 2: Using python3 –version. Direct approach to check Python 3 version. Not applicable if Python 3 is not installed.
  • Method 3: Using pyenv versions. Great for environments with multiple Python versions managed by pyenv. Not suitable if pyenv is not installed.
  • Method 4: Checking version in script with sys module. Detailed version information, including build date and compiler. Requires a command to execute Python code.
  • Bonus Method 5: Using platform module. Straightforward and gives concise version information. Also needs a command to execute Python code.