diff --git a/IPython/core/pylabtools.py b/IPython/core/pylabtools.py index 61526ae..cf25895 100644 --- a/IPython/core/pylabtools.py +++ b/IPython/core/pylabtools.py @@ -253,7 +253,7 @@ def import_pylab(user_ns, import_all=True): exec s in user_ns -def configure_inline_backend(shell, user_ns=None): +def configure_inline_support(shell, backend, user_ns=None): """Configure an IPython shell object for matplotlib use. Parameters @@ -261,6 +261,8 @@ def configure_inline_backend(shell, user_ns=None): shell : InteractiveShell instance If None, this function returns immediately. + backend : matplotlib backend + user_ns : dict A namespace where all configured variables will be placed. If not given, the `user_ns` attribute of the shell object is used. @@ -268,25 +270,33 @@ def configure_inline_backend(shell, user_ns=None): if shell is None: return - user_ns = shell.user_ns if user_ns is None else user_ns - # If using our svg payload backend, register the post-execution # function that will pick up the results for display. This can only be # done with access to the real shell object. - from IPython.zmq.pylab.backend_inline import InlineBackend + # Note: if we can't load the inline backend, then there's no point + # continuing (such as in terminal-only shells in environments without + # zeromq available). + try: + from IPython.zmq.pylab.backend_inline import InlineBackend + except ImportError: + return + + user_ns = shell.user_ns if user_ns is None else user_ns + cfg = InlineBackend.instance(config=shell.config) cfg.shell = shell if cfg not in shell.configurables: shell.configurables.append(cfg) - from IPython.zmq.pylab.backend_inline import flush_figures - from matplotlib import pyplot - shell.register_post_execute(flush_figures) - # load inline_rc - pyplot.rcParams.update(cfg.rc) - # Add 'figsize' to pyplot and to the user's namespace - user_ns['figsize'] = pyplot.figsize = figsize + if backend == backends['inline']: + from IPython.zmq.pylab.backend_inline import flush_figures + from matplotlib import pyplot + shell.register_post_execute(flush_figures) + # load inline_rc + pyplot.rcParams.update(cfg.rc) + # Add 'figsize' to pyplot and to the user's namespace + user_ns['figsize'] = pyplot.figsize = figsize # Setup the default figure format fmt = cfg.figure_format @@ -323,10 +333,7 @@ def pylab_activate(user_ns, gui=None, import_all=True, shell=None): gui, backend = find_gui_and_backend(gui) activate_matplotlib(backend) import_pylab(user_ns, import_all) - - # The inline backend is only used by GUI shells - if backend == backends['inline']: - configure_inline_backend(shell, backend, user_ns) + configure_inline_support(shell, backend, user_ns) print """ Welcome to pylab, a matplotlib-based Python environment [backend: %s]. diff --git a/IPython/testing/iptest.py b/IPython/testing/iptest.py index e4fddb6..de180c2 100644 --- a/IPython/testing/iptest.py +++ b/IPython/testing/iptest.py @@ -238,8 +238,8 @@ def make_exclude(): exclusions.append(ipjoin('parallel', 'tests', 'test_mongodb')) if not have['matplotlib']: - exclusions.extend([ipjoin('lib', 'pylabtools'), - ipjoin('lib', 'tests', 'test_pylabtools')]) + exclusions.extend([ipjoin('core', 'pylabtools'), + ipjoin('core', 'tests', 'test_pylabtools')]) if not have['tornado']: exclusions.append(ipjoin('frontend', 'html'))