diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 39d4c3858c61..7c4b018fedd3 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -112,6 +112,7 @@ def _backend_selection(): _IP_REGISTERED = None _INSTALL_FIG_OBSERVER = False + def install_repl_displayhook(): """ Install a repl display hook so that any stale figure are automatically @@ -128,27 +129,30 @@ class _NotIPython(Exception): # see if we have IPython hooks around, if use them try: - from IPython import get_ipython - ip = get_ipython() - if ip is None: - raise _NotIPython() - - if _IP_REGISTERED: - return - - def post_execute(): - if matplotlib.is_interactive(): - draw_all() - - # IPython >= 2 - try: - ip.events.register('post_execute', post_execute) - except AttributeError: - # IPython 1.x - ip.register_post_execute(post_execute) - - _IP_REGISTERED = post_execute - _INSTALL_FIG_OBSERVER = False + if 'IPython' in sys.modules: + from IPython import get_ipython + ip = get_ipython() + if ip is None: + raise _NotIPython() + + if _IP_REGISTERED: + return + + def post_execute(): + if matplotlib.is_interactive(): + draw_all() + + # IPython >= 2 + try: + ip.events.register('post_execute', post_execute) + except AttributeError: + # IPython 1.x + ip.register_post_execute(post_execute) + + _IP_REGISTERED = post_execute + _INSTALL_FIG_OBSERVER = False + else: + _INSTALL_FIG_OBSERVER = True # import failed or ipython is not running except (ImportError, _NotIPython):