Try preferred import/feature, fall back to alternative.
# Python version compatibility
try:
from functools import cache
except ImportError:
from functools import lru_cache
cache = lru_cache(None)
# Optional dependency
try:
import numpy as np
HAS_NUMPY = True
except ImportError:
HAS_NUMPY = False
def process(data):
if HAS_NUMPY:
return np.array(data).mean()
else:
return sum(data) / len(data)
Example (from pytudes)
# beal.py - math.gcd location changed between versions
try:
from math import gcd # Python 3.5+
except ImportError:
from fractions import gcd # Python 2.7 and early 3.x
# ngrams.py - use available modules
try:
from functools import cache
except ImportError:
from functools import lru_cache
def cache(func):
return lru_cache(None)(func)
# Pattern for conditional features
try:
# Python 3.10+ pattern matching
def dispatch(x):
match x:
case int(): return handle_int(x)
case str(): return handle_str(x)
case _: return handle_other(x)
except SyntaxError:
# Fallback for older Python
def dispatch(x):
if isinstance(x, int):
return handle_int(x)
elif isinstance(x, str):
return handle_str(x)
else:
return handle_other(x)
# Graceful feature degradation
try:
import matplotlib.pyplot as plt
def plot(data):
plt.plot(data)
plt.show()
except ImportError:
def plot(data):
print("Plotting requires matplotlib")
print(f"Data: {data[:10]}...")
Key Principles
Try modern first: Prefer newer, better APIs
ImportError for missing: Standard exception for imports
Create compatible API: Wrapper that works either way
Flag for optional: HAS_FEATURE pattern
Fail gracefully: Warn, don't crash
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: jimmc414-claude-code-plugin-marketplace-fallback-compatibili3description: fallback-compatibility4---56# fallback-compatibility78## When to Use9- Supporting multiple Python versions10- Optional dependency handling11- Feature detection12- Graceful degradation1314## When NOT to Use15- Single Python version target16- Required dependency (should fail if missing)17- Over-engineering simple code1819## The Pattern2021Try preferred import/feature, fall back to alternative.2223```python24# Python version compatibility25try:26 from functools import cache27except ImportError:28 from functools import lru_cache29 cache = lru_cache(None)3031# Optional dependency32try:33 import numpy as np34 HAS_NUMPY = True35except ImportError:36 HAS_NUMPY = False3738def process(data):39 if HAS_NUMPY:40 return np.array(data).mean()41 else:42 return sum(data) / len(data)43```4445## Example (from pytudes)4647```python48# beal.py - math.gcd location changed between versions49try:50 from math import gcd # Python 3.5+51except ImportError:52 from fractions import gcd # Python 2.7 and early 3.x5354# ngrams.py - use available modules55try:56 from functools import cache57except ImportError:58 from functools import lru_cache59 def cache(func):60 return lru_cache(None)(func)6162# Pattern for conditional features63try:64 # Python 3.10+ pattern matching65 def dispatch(x):66 match x:67 case int(): return handle_int(x)68 case str(): return handle_str(x)69 case _: return handle_other(x)70except SyntaxError:71 # Fallback for older Python72 def dispatch(x):73 if isinstance(x, int):74 return handle_int(x)75 elif isinstance(x, str):76 return handle_str(x)77 else:78 return handle_other(x)7980# Graceful feature degradation81try:82 import matplotlib.pyplot as plt83 def plot(data):84 plt.plot(data)85 plt.show()86except ImportError:87 def plot(data):88 print("Plotting requires matplotlib")89 print(f"Data: {data[:10]}...")90```9192## Key Principles931. **Try modern first**: Prefer newer, better APIs942. **ImportError for missing**: Standard exception for imports953. **Create compatible API**: Wrapper that works either way964. **Flag for optional**: `HAS_FEATURE` pattern975. **Fail gracefully**: Warn, don't crash9899---100> Converted and distributed by [TomeVault](https://tomevault.io/claim/jimmc414) — claim your Tome and manage your conversions.101<!-- tomevault:4.0:skill_md:2026-04-13 -->
Run npx skillmds@latest add tomevault-io/jimmc414-claude-code-plugin-marketplace-fallback-compatibili in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
fallback-compatibility It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
tomevault-io (@tomevault-io) published this skill. Their other Agent Skills are listed on their SkillMD profile.