Python Version
Check Python
Python Script
Python Command Line
Programming Tutorial

How do I check which version of Python is running my script?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you want to know which Python version is actually running your script, the answer is inside the running process, not in a terminal guess. Commands such as python --version are useful, but they only tell you what a shell command resolves to at that moment. Inside the script, sys.version_info and sys.executable tell you exactly what interpreter launched the code.

Check the Version from Inside the Script

The most direct runtime check uses the sys module.

python
1import sys
2
3print(sys.version)
4print(sys.version_info)
5print(sys.executable)

These three values answer slightly different questions:

  • 'sys.version gives a human-readable build string'
  • 'sys.version_info gives structured version components'
  • 'sys.executable shows the interpreter path that launched the script'

For compatibility checks, sys.version_info is usually the most useful.

python
1import sys
2
3if sys.version_info < (3, 10):
4    raise RuntimeError("Python 3.10 or newer is required")

This is much safer than comparing raw version strings.

Use platform.python_version() for a Clean Version String

If you only want a compact string, the platform module can help.

python
import platform

print(platform.python_version())

This returns something like 3.12.2.

It is convenient for logs or diagnostics, but for logic and comparisons, sys.version_info is still better.

Why python --version Can Mislead You

From a terminal, these commands are common:

bash
1python --version
2python3 --version
3which python
4where python

They are useful, but they answer shell-resolution questions, not necessarily runtime questions for a specific script launch.

For example:

  • an IDE may use a different interpreter than your shell
  • a virtual environment may override python
  • a shebang line may point at a specific executable
  • a scheduler or service may run the script with a different binary

That is why checking inside the script itself is the most reliable answer.

Virtual Environments Change the Result

Virtual environments are one of the main reasons people get confused.

If a script is launched from an activated virtual environment, sys.executable usually points to the environment’s Python binary.

python
1import sys
2
3print("Executable:", sys.executable)
4print("Prefix:", sys.prefix)
5print("Base prefix:", sys.base_prefix)

A common pattern is:

  • 'sys.prefix != sys.base_prefix suggests a virtual environment is active'

That helps explain why your script may behave differently than the system Python you expected.

Shebangs and Launchers Matter Too

On Unix-like systems, a script can start with a shebang such as:

python
#!/usr/bin/env python3

or:

python
#!/usr/bin/python3.11

That line affects which interpreter runs the file when executed directly.

On Windows, the Python launcher also matters. Commands such as:

bat
py -3.11 script.py

can launch a specific version even if python resolves differently in the shell.

Again, sys.executable inside the program tells you what really happened.

A Practical Debug Header

For troubleshooting, it is often useful to log all the relevant runtime details in one place.

python
1import platform
2import sys
3
4print("Python version:", platform.python_version())
5print("Version info:", sys.version_info)
6print("Executable:", sys.executable)
7print("Platform:", platform.platform())

This is especially helpful in CI, containers, cron jobs, and IDE-run configurations where the launcher can differ from your interactive shell.

Checking Version for Packaging or Feature Gates

When you want to guard a feature, do not parse sys.version manually. Use the tuple form.

python
1import sys
2
3if sys.version_info >= (3, 11):
4    print("Use the 3.11+ code path")
5else:
6    print("Use the compatibility code path")

Tuple comparisons are readable and avoid string-ordering mistakes such as treating 3.10 like it were smaller than 3.9.

Common Pitfalls

The biggest pitfall is assuming python --version in your shell proves which interpreter is running a script launched by an IDE, service, notebook, or scheduler.

Another issue is comparing version strings directly instead of using sys.version_info.

Developers also often forget that virtual environments and shebangs can override the interpreter they thought they were using.

Finally, sys.version is great for diagnostics, but sys.version_info is better for program logic.

Summary

  • The most reliable way to check the running Python version is from inside the script.
  • Use sys.version_info for logic and compatibility checks.
  • Use sys.executable to see which interpreter binary launched the program.
  • 'platform.python_version() is convenient for a simple version string.'
  • Shell commands are helpful, but runtime inspection is the source of truth for the script you are actually executing.

Course illustration
Course illustration

All Rights Reserved.