Python
Programming
Version Control
Python Commands
Development

Printing Python version in output

Master System Design with Codemia

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

Introduction

Printing the Python version is a small step that solves a lot of debugging problems. It helps confirm which interpreter is actually running your code, which matters when multiple Python versions, virtual environments, or container images are involved.

The Simplest Runtime Check

The most direct approach is the sys module:

python
import sys

print(sys.version)

This prints a full version string, including build information. Example output might look like a version number followed by compiler details and a build date. That is useful when you want a complete runtime fingerprint.

If you only need the structured numeric parts, use sys.version_info:

python
1import sys
2
3print(sys.version_info)
4print(sys.version_info.major)
5print(sys.version_info.minor)
6print(sys.version_info.micro)

This is better for programmatic checks because you do not need to parse a free-form string.

Use platform.python_version() for Cleaner Output

If you only want something like 3.12.2, the platform module is often cleaner:

python
import platform

print(platform.python_version())

That makes logs easier to scan and is usually enough for user-facing output or environment diagnostics.

Printing the Version at Script Startup

For scripts that need basic environment reporting, print the version once near startup:

python
1import platform
2
3
4def main():
5    print(f"Running on Python {platform.python_version()}")
6    print("Application started")
7
8
9if __name__ == "__main__":
10    main()

This is useful in CLI tools, deployment scripts, migration jobs, and support utilities where environment drift is a common source of failure.

Fail Fast When the Version Is Too Old

Often you do not just want to print the version. You want to validate it.

python
1import sys
2
3
4MIN_VERSION = (3, 10)
5
6if sys.version_info < MIN_VERSION:
7    raise RuntimeError(
8        f"Python {MIN_VERSION[0]}.{MIN_VERSION[1]} or newer is required. "
9        f"Current version: {sys.version.split()[0]}"
10    )
11
12print(f"Interpreter OK: {sys.version.split()[0]}")

Using sys.version_info avoids fragile string parsing and makes the comparison explicit.

Printing Version Information in Logs

In longer-running applications, version information is often more useful in a logger than in standard output:

python
1import logging
2import platform
3
4logging.basicConfig(level=logging.INFO)
5logger = logging.getLogger(__name__)
6
7logger.info("Python version: %s", platform.python_version())

This is the better pattern in web services, background jobs, and test infrastructure because the version becomes part of the normal log stream.

Command-Line Alternatives

Sometimes you do not need to modify code at all. From the shell, you can print the interpreter version directly:

bash
python --version
python3 --version

And from one command:

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

This is especially useful in CI jobs and container diagnostics where you only need to confirm the active interpreter.

Common Version Confusion in Real Projects

The hardest part is often not printing the version. It is printing the version of the interpreter that is actually running your code.

For example:

  • 'python and python3 may point to different executables.'
  • An IDE may use a different interpreter than your terminal.
  • A virtual environment may shadow the system Python.
  • A container image may run a different Python than the host machine.

That is why printing the version inside the program is often more trustworthy than checking a shell command separately.

You can also print the executable path if needed:

python
1import sys
2
3print(sys.executable)
4print(sys.version.split()[0])

This is very helpful when you suspect that the wrong virtual environment is active.

Common Pitfalls

  • Using sys.version for numeric comparisons instead of sys.version_info.
  • Printing the shell's Python version and assuming the application uses the same interpreter.
  • Forgetting that IDEs, virtual environments, and containers may each choose a different Python binary.
  • Logging a version only during development and removing it from production diagnostics.
  • Parsing version strings manually when structured version information is already available.

Summary

  • Use sys.version for a complete runtime version string.
  • Use platform.python_version() when you want clean human-readable output.
  • Use sys.version_info for version checks in code.
  • Print the interpreter path with sys.executable when environment confusion is likely.
  • Put version reporting near startup or in logs so debugging environment issues is easier.

Course illustration
Course illustration

All Rights Reserved.