def get_focus_python_shell():
 widget = QApplication.focusWidget()
 from spyder.widgets.shell import PythonShellWidget
 from spyder.widgets.externalshell.pythonshell import ExternalPythonShell
 if isinstance(widget, PythonShellWidget):
  return widget
 elif isinstance(widget, ExternalPythonShell):
  return widget.shell

def get_focus_python_shell ( ) : widget = QApplication . focusWidget ( ) from spyder . widgets . shell import PythonShellWidget from spyder . widgets . externalshell . pythonshell import ExternalPythonShell if isinstance ( widget , PythonShellWidget ) : return widget elif isinstance ( widget , ExternalPythonShell ) : return widget . shell
extract and return python shell from widget return none if *widget* is not a python shell .