Handle ShellDetectionFailure in applications
masterWhen using detect_shell(), your application should anticipate that a shell might not be present (e.g., if the user is running a non-interactive process). Instead of letting shellingham.ShellDetectionFailure crash your program, wrap the call in a try...except block and provide a fallback shell.
Recommended fallback strategies:
- POSIX: Use the
SHELLenvironment variable (the user's preferred command language interpreter). - POSIX (Scripting): Use
sh(usually/bin/sh) for running scripts. - Windows: Use the
COMSPECenvironment variable (e.g.,cmd.exe).
import os
import shellingham
def provide_default():
if os.name == 'posix':
return os.environ['SHELL']
elif os.name == 'nt':
return os.environ['COMSPEC']
raise NotImplementedError(f'OS {os.name!r} support not available')
try:
shell = shellingham.detect_shell()
except shellingham.ShellDetectionFailure:
shell = provide_default()