杰瑞科技汇

如何检查Python版本?

Of course! Checking your Python version is a fundamental and common task. Here are the best ways to do it, from the simplest command-line method to programmatic checks.

如何检查Python版本?-图1
(图片来源网络,侵删)

The Easiest Way: Using the Command Line

This is the most direct method to check the version of Python installed on your system.

On Windows:

Open Command Prompt or PowerShell and type:

python --version

or the shorter alias:

python -V

On macOS and Linux:

Open your Terminal and type:

如何检查Python版本?-图2
(图片来源网络,侵删)
python3 --version

or the shorter alias:

python3 -V

Why python3? On many modern macOS and Linux systems, python is an alias for an older Python 2 installation (which is no longer maintained). python3 explicitly refers to Python 3. On Windows, the installer usually sets up python to point to Python 3 by default, but it's good practice to check.


The Interactive Way: Using the Python Interpreter

This method is useful if you want to see the version and then immediately start writing Python code.

  1. Open your terminal or command prompt.
  2. Start the Python interpreter by typing python or python3.
  3. Once inside the interpreter (you'll see a >>> prompt), use the sys module.
>>> import sys
>>> print(sys.version)
3.10.6 (main, Mar 10 2025, 10:21:29) [GCC 11.3.0]

The sys.version string contains detailed information, including the version number, build number, and compiler used.

如何检查Python版本?-图3
(图片来源网络,侵删)

You can also get just the major and minor version numbers as a tuple:

>>> import sys
>>> print(sys.version_info)
sys.version_info(major=3, minor=10, micro=6, releaselevel='final', serial=0)
>>> print(sys.version_info.major)
3
>>> print(sys.version_info.minor)
10

The Programmatic Way: Using a Script

If you need to check the version within a Python script (for example, to ensure the user has a compatible version before running your code), you can use the sys module or the platform module.

Using the sys Module

This is the most common and reliable method.

import sys
# Check if the Python version is at least 3.8
if sys.version_info >= (3, 8):
    print("✅ Python version is 3.8 or higher. You can proceed.")
else:
    print("❌ Error: This script requires Python 3.8 or higher.")
    print(f"Your current version is {sys.version_info.major}.{sys.version_info.minor}")
    sys.exit(1) # Exit the script with an error code
# You can also print the full version string
print(f"Your Python version is: {sys.version}")

Using the platform Module

The platform module also provides version information and can be useful for getting more details about the system.

import platform
# Get the version information as a tuple
version_tuple = platform.python_version_tuple()
print(f"Version as a tuple: {version_tuple}")
print(f"Major: {version_tuple[0]}")
print(f"Minor: {version_tuple[1]}")
print(f"Patch: {version_tuple[2]}")
# Get the version as a string
version_string = platform.python_version()
print(f"Version as a string: {version_string}")
# Get more detailed system information
print(f"Full system info: {platform.python_version()}")

The "Python Launcher for Windows" Way

On Windows, if you have multiple versions of Python installed, you can use the py command (the Python Launcher) to check specific versions.

# Check which versions are available
py -0p
# Example output:
#  -3.10-64 *
#  -3.9-64
#  -2.7-64
# The asterisk (*) indicates the default version.
# Check the version of a specific Python executable
py -3.10 --version
# Output: Python 3.10.6

Summary Table

Method Command / Code Best For
Command Line python --version or python3 --version A quick check from your terminal.
Interactive Interpreter import sys; print(sys.version) Checking version and then running interactive code.
Programmatic Check (in a script) import sys; if sys.version_info >= (3, 8): ... Ensuring script compatibility.
Windows Python Launcher py --version or py -3.10 --version Managing multiple Python versions on Windows.

For most day-to-day use, python --version or python3 --version is all you need. For scripting, import sys is the standard approach.

分享:
扫描分享到社交APP
上一篇
下一篇