Python Version Checking Code Examples and Scripts
Ready-to-use Python code snippets to check what Python version do I have programmatically with different output formats.
Python Version Checking Code Examples and Scripts
These ready-to-use code snippets help you programmatically determine what Python version do I have in your scripts and applications. Perfect for version compatibility checks and system diagnostics.
Basic Version Display #
Simple Version Print #
🐍 Try it yourself
Formatted Version Output #
🐍 Try it yourself
Version Comparison Functions #
Check Minimum Version Requirement #
import sys
def check_python_version(min_major=3, min_minor=8):
"""
Check if current Python version meets minimum requirements.
Returns True if version is sufficient, False otherwise.
"""
current = sys.version_info
required = (min_major, min_minor)
if (current.major, current.minor) >= required:
return True
else:
print(f"Python {min_major}.{min_minor}+ required. Current: {current.major}.{current.minor}")
return False
# Usage examples
if check_python_version(3, 8):
print("✓ Python version is compatible")
else:
print("✗ Please upgrade Python")
Version Range Checker #
🐍 Try it yourself
Advanced Version Information #
Complete System Information #
import sys
import platform
import os
def get_python_environment_info():
"""
Gather comprehensive Python environment information.
Returns dictionary with version and system details.
"""
info = {
'python_version': sys.version.split()[0],
'version_info': {
'major': sys.version_info.major,
'minor': sys.version_info.minor,
'micro': sys.version_info.micro,
'releaselevel': sys.version_info.releaselevel,
'serial': sys.version_info.serial
},
'executable': sys.executable,
'platform': platform.platform(),
'system': platform.system(),
'processor': platform.processor(),
'python_implementation': platform.python_implementation(),
'python_compiler': platform.python_compiler()
}
return info
# Usage
env_info = get_python_environment_info()
for key, value in env_info.items():
print(f"{key}: {value}")
Version Compatibility Checker #
🐍 Try it yourself
Command Line Integration #
Version Check Script #
#!/usr/bin/env python3
"""
Command line script to check Python version with options.
Save as check_version.py and run with: python check_version.py
"""
import sys
import argparse
def main():
parser = argparse.ArgumentParser(description='Check Python version')
parser.add_argument('--simple', action='store_true',
help='Show simple version number only')
parser.add_argument('--check', nargs=2, metavar=('MAJOR', 'MINOR'),
help='Check if version is at least MAJOR.MINOR')
args = parser.parse_args()
if args.simple:
print(f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}")
elif args.check:
major, minor = int(args.check[0]), int(args.check[1])
current = (sys.version_info.major, sys.version_info.minor)
required = (major, minor)
if current >= required:
print(f"✓ Python {major}.{minor}+ requirement met")
sys.exit(0)
else:
print(f"✗ Python {major}.{minor}+ required, found {current[0]}.{current[1]}")
sys.exit(1)
else:
print(f"Python {sys.version}")
if __name__ == "__main__":
main()
One-Liner Utilities #
Quick Version Checks #
# One-liner version checks for different scenarios
# Basic version string
version = f"{sys.version_info.major}.{sys.version_info.minor}"
# Version with micro number
full_version = f"{sys.version_info[:3]}"[1:-1].replace(', ', '.')
# Check if Python 3.8+
is_modern = sys.version_info >= (3, 8)
# Get major version only
major = sys.version_info.major
Environment Variable Style #
🐍 Try it yourself
Usage Tips #
In Applications #
Use these snippets to:
- Startup checks: Verify compatibility before running main application
- Error reporting: Include version info in bug reports
- Feature flags: Enable/disable features based on Python version
- Installation scripts: Check requirements during setup
Common Patterns #
# Early exit pattern for version requirements
if sys.version_info < (3, 8):
print("Python 3.8+ required")
sys.exit(1)
# Conditional imports based on version
if sys.version_info >= (3, 9):
from collections.abc import Mapping
else:
from typing import Mapping
Summary #
These code snippets provide comprehensive methods to programmatically determine what Python version do I have:
- Basic checks: Simple version display and comparison
- Advanced info: Complete environment details
- Compatibility: Feature support verification
- Integration: Command line and application usage
Choose the appropriate snippet based on your specific needs for version checking and compatibility validation.