Python Scripts to Detect Which Version You're Running
Ready-to-use Python code snippets to check which Python version you're using, with detailed version information and compatibility checks.
Python Scripts to Detect Which Version You're Running
These code snippets help you programmatically determine which Python version you're using, check compatibility, and get detailed version information.
Basic Version Detection #
Simple ways to check which Python version is running:
🐍 Try it yourself
Detailed Version Information #
Get comprehensive details about which Python version and build you're using:
🐍 Try it yourself
Version Compatibility Checker #
Script to check if your current Python version meets specific requirements:
🐍 Try it yourself
Feature Detection by Version #
Check which Python features are available in your version:
import sys
def check_python_features():
"""Check which Python features are available based on version"""
version = sys.version_info
features = {}
# Python 3.8 features
if version >= (3, 8):
features['walrus_operator'] = True # :=
features['positional_only_params'] = True # def func(x, /)
features['f_string_equals'] = True # f"{x=}"
# Python 3.9 features
if version >= (3, 9):
features['dict_union_operators'] = True # dict1 | dict2
features['type_hinting_generics'] = True # list[str]
features['string_methods'] = True # str.removeprefix()
# Python 3.10 features
if version >= (3, 10):
features['match_statement'] = True # match/case
features['union_types'] = True # int | str
features['parenthesized_context_managers'] = True
# Python 3.11 features
if version >= (3, 11):
features['exception_groups'] = True
features['fine_grained_error_locations'] = True
features['tomllib'] = True
# Python 3.12 features
if version >= (3, 12):
features['f_string_debugging'] = True
features['type_params'] = True # Generic classes
features['buffer_protocol'] = True
return features
# Check available features
available_features = check_python_features()
print("Available Python features:")
for feature, available in available_features.items():
print(f" {feature}: {'✅' if available else '❌'}")
Environment and Installation Detection #
Determine which Python installation and environment you're using:
🐍 Try it yourself
Version Comparison Utility #
Compare different Python versions and check upgrade recommendations:
import sys
from datetime import datetime
def should_upgrade_python():
"""Analyze current Python version and suggest upgrades"""
current = sys.version_info
# Python support timeline (simplified)
version_status = {
(3, 8): {'status': 'Security fixes only', 'eol': '2024-10'},
(3, 9): {'status': 'Security fixes only', 'eol': '2025-10'},
(3, 10): {'status': 'Bug fixes', 'eol': '2026-10'},
(3, 11): {'status': 'Active development', 'eol': '2027-10'},
(3, 12): {'status': 'Latest stable', 'eol': '2028-10'},
}
current_key = (current.major, current.minor)
current_status = version_status.get(current_key, {'status': 'Unknown', 'eol': 'Unknown'})
recommendations = []
if current < (3, 9):
recommendations.append("🚨 Immediate upgrade recommended - security risk")
elif current < (3, 10):
recommendations.append("⚠️ Upgrade recommended - limited support")
elif current < (3, 11):
recommendations.append("📈 Consider upgrading for better performance")
else:
recommendations.append("✅ Current version is well supported")
return {
'current_version': f"{current.major}.{current.minor}.{current.micro}",
'status': current_status['status'],
'end_of_life': current_status['eol'],
'recommendations': recommendations
}
# Get upgrade recommendations
upgrade_info = should_upgrade_python()
print("Python Version Analysis:")
print(f"Current: {upgrade_info['current_version']}")
print(f"Status: {upgrade_info['status']}")
print(f"End of Life: {upgrade_info['end_of_life']}")
print("Recommendations:")
for rec in upgrade_info['recommendations']:
print(f" {rec}")
Quick Version Detection Script #
Minimal script to quickly identify which Python version you're running:
🐍 Try it yourself
Usage Tips #
When to use these scripts:
- At the start of Python programs to verify compatibility
- In deployment scripts to check environment setup
- For debugging version-related issues
- When documenting system requirements
Best practices:
- Include version checks in critical applications
- Log version information for debugging
- Use feature detection rather than version comparison when possible
- Test scripts across different Python installations
These snippets help you programmatically determine which Python version you're working with and ensure compatibility across different environments.