How to Check Python Version on Linux and Windows

LightNode
By LightNode ·

Introduction

Python is one of the most widely used programming languages, making it essential for developers to know the version installed on their system. The version of Python can affect the compatibility of scripts, libraries, and even the behavior of the language itself.

Checking the Python version is a straightforward task, but the process differs slightly between operating systems. On Linux, you typically use the terminal to check the version, while on Windows, you can use the Command Prompt or the Python interpreter directly.

This guide will walk you through how to check the installed Python version on both Linux and Windows systems, helping you ensure your environment is set up correctly for your Python projects.

Checking Python Version in Linux

On Linux, checking the Python version is quick and easy using the terminal. Below are the different methods you can use to verify which version of Python is installed on your system.

Using the Terminal

Default Python Version

To check the default version of Python installed, simply open the terminal and run the following command:

python --version

or

python -V

This command will return the version of Python 2.x if it is set as the default, which is still the case on some older Linux distributions.

Python 3 Version

Many Linux systems come with both Python 2 and Python 3 installed. To specifically check the version of Python 3, use the following command:

python3 --version

This will output the version of Python 3.x installed on your system.

Alternate Command

In some Linux distributions, the python command might be linked to Python 3 instead of Python 2. In this case, you can simply use the following:

python --version

and it will display the Python 3 version.

Using the Python Interpreter

If you prefer to check the version from within the Python environment itself, you can do so by starting the Python interpreter.

  1. Open the terminal.

  2. Type python or python3 (depending on the version you want to check) and press Enter:

    python
    
  3. Once inside the interpreter, type the following code to check the Python version:

    import sys
    print(sys.version)
    
  4. The version will be displayed in the format: 3.x.x (default, ...) or 2.x.x (default, ...).

  5. Exit the interpreter by typing:

    exit()
    

Checking Python Version in Windows

In Windows, you can check the Python version through the Command Prompt or the Python interpreter. Here's how you can do it on a Windows machine:

Using Command Prompt

To check the version of Python installed, open the Command Prompt and use the following commands:

Default Python Version

Open the Command Prompt by pressing Windows + R, typing cmd, and pressing Enter. Then, type the following command:

python --version

or

python -V

This will output the version of Python that's set as the default. If both Python 2 and Python 3 are installed, it will show the version of the default Python, which may be either Python 2 or Python 3.

If Python is Not Found

If the command python does not work and the terminal returns an error saying that Python is not recognized as an internal or external command, it means that Python is either not installed or it is not added to the system’s PATH.

In this case, you can check for Python 3 with the following command:

python3 --version

This command works if Python 3 is installed and properly set up in your PATH.

Using the Python Interpreter

You can also check the Python version directly from the Python interpreter:

  1. Open Command Prompt (cmd).

  2. Type python and press Enter. This will start the Python interpreter.

    python
    
  3. Once inside the interpreter, check the version by typing:

    import sys
    print(sys.version)
    

    This will display the full Python version along with some additional details.

  4. Exit the interpreter by typing:

    exit()
    

Checking Python Version in Linux and Windows

FAQs

Q: What if the 'python' command is not recognized?

A: If the python command is not recognized, it likely means that Python is either not installed or not added to your system's PATH environment variable.

  • On Windows, ensure that during Python installation, the option "Add Python to PATH" is selected. If it's already installed and not in the PATH, you may need to manually add the Python installation directory to your system's PATH.

  • On Linux, Python may not be installed at all, or you may be using a distribution where Python 3 is installed as python3 rather than python. Try using the command python3 --version instead.

Q: How do I check if both Python 2 and Python 3 are installed on Linux?

A: On many Linux systems, Python 2 and Python 3 are both installed, but they may be accessed via different commands:

  • To check Python 2, run:
    python --version
    
  • To check Python 3, run:
    python3 --version
    

If both versions are installed, you'll see their respective versions displayed.

Q: Can I check the Python version from a script?

A: Yes, you can check the Python version from within a script. To do so, include the following code in your Python script:

import sys
print(sys.version)

This will print the Python version used to run the script, along with additional details.

Q: How do I find the path to the Python executable?

A: You can find the path to the Python executable using the following commands:

  • On Linux:

    which python
    

    or

    which python3
    
  • On Windows:

    where python