Detect if IPython Pylab GUI event loop is active
Question: Is there a canonical way of detecting inside the interpreter if IPython was
called with options like`--pylab=...` or `--gui=...`?

The reason: I want to do some asynchronous plotting in a separate process, as
show in the sample script `tst_process.py`:

    
    
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    """ File tst_process.py """
    # For better Python 3 compatibility:
    from __future__ import absolute_import, print_function, unicode_literals, \
                           division
    
    import matplotlib.pylab as plt
    from multiprocessing import Process
    import numpy as np
    
    def tst_plot(fgoff=0):
        """ Make a test plot """
        print("Drawing figure {}".format(1+fgoff))
        x = np.linspace(0, 5, 500)
        fg = plt.figure(1+fgoff)
        fg.clf()
        ax = fg.add_subplot(1, 1, 1)
        ax.plot(x, np.sin(x))
        ax.set_title("This is a Test-Plot")
        fg.canvas.draw()
        plt.show()
    
    
    if __name__ == "__main__":
    
        print("Doing testplot in new process ...")
        pprc1 = Process(target=tst_plot)
        pprc1.start()
        print("Doing testplot in own process ...")
        tst_plot(10)
    

When I run it with the command

    
    
    ipython --i tst_process.py
    

everything works as expected. Doing:

    
    
    ipython --pylab=qt --i tst_process.py 
    

gives:

    
    
    Python 2.7.9 (default, Dec 11 2014, 08:58:12) 
    Type "copyright", "credits" or "license" for more information.
    
    IPython 2.3.0 -- An enhanced Interactive Python.
    ?         -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help      -> Python's own help system.
    object?   -> Details about 'object', use 'object??' for extra details.
    Doing testplot in new process ...
    Doing testplot in own process ...
    Drawing figure 11
    Drawing figure 1
     : Fatal IO error: client killed
    X Error: BadIDChoice (invalid resource ID chosen for this connection) 14
      Major opcode: 1 (X_CreateWindow)
      Resource id:  0x6a00003
    X Error: BadIDChoice (invalid resource ID chosen for this connection) 14
      Extension:    139 (RENDER)
      Minor opcode: 4 (RenderCreatePicture)
      Resource id:  0x6a00004
    X Error: BadIDChoice (invalid resource ID chosen for this connection) 14
      Major opcode: 1 (X_CreateWindow)
      Resource id:  0x6a00005
    [xcb] Unknown sequence number while processing queue
    [xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
    [xcb] Aborting, sorry about that.
    python: ../../src/xcb_io.c:274: poll_for_event: Zusicherung »!xcb_xlib_threads_sequence_lost« nicht erfüllt.
    Abgebrochen
    

Other backends with the exception of `wx` did not work either.

It would be sufficient for me to detect the existence of the event loop. Then
I could use the same script for running from the command line and for inside
Spyder.


Answer: To answer your original question: `Is there a canonical way of detecting
inside the interpreter if IPython was called with options like--pylab=... or
--gui=...?`

Yes, there is. The most simple would be to check for the command line
arguments:

    
    
    import sys
    print sys.argv # returns the commandline arguments
    # ['ipython', '--pylab', 'inline']
    

A nicer way would be to use the built-in module
[optparse](https://docs.python.org/2/library/optparse.html).

However, this will only allow you to see what mode it is running in, based on
the command line arguments - which was your main question. It will not help
you solve the gui event looks + multiprocess issues as
[@tcaswell](http://stackoverflow.com/users/380231/tcaswell) mentioned in the
comments.

