nasty import behavior

Thanks for the info, but I still think this is rude behavior for a python
module, and believe it would be a good thing to fix it.

Michael Droettboom wrote:

You need to set your backend to a non-GUI one (such as Agg) if you want
to run without a display. This FAQ answer may be helpful:

http://matplotlib.sourceforge.net/faq/howto_faq.html#matplotlib-in-a-web-

application-server

···

Mike

On 07/08/2010 08:26 AM, Neal Becker wrote:

A python module of mine could draw a plot, and so imports from
matplotlib. Actually, plotting is part of the module test that is in
  if __name__==__main__

But this kills my job trying to run non-interactively.
This is IMO rather nasty behavior. matplotlib should not try to open the
display just because you import it.

     from matplotlib.pyplot import *
   File "/usr/lib64/python2.6/site-packages/matplotlib/pyplot.py", line
   78,
in<module>
     new_figure_manager, draw_if_interactive, show = pylab_setup()
   File
   "/usr/lib64/python2.6/site-packages/matplotlib/backends/__init__.py",
line 25, in pylab_setup
     globals(),locals(),[backend_name])
   File "/usr/lib64/python2.6/site-
packages/matplotlib/backends/backend_gtkagg.py", line 10, in<module>
     from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK,
FigureCanvasGTK,\
   File "/usr/lib64/python2.6/site-
packages/matplotlib/backends/backend_gtk.py", line 8, in<module>
     import gtk; gdk = gtk.gdk
   File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py",
   line
64, in<module>
     _init()
   File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py",
   line
52, in _init
     _gtk.init_check()
RuntimeError: could not open display

------------------------------------------------------------------------------

This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel
   

Generally we try and make it easy for the user to express their
intention rather than guess what they are trying to do. So you can
set your backend in an rc file

  http://matplotlib.sourceforge.net/users/customizing.html

or harcode it in your script (before importing pylab/pyplot)

  import matplotlib
  matplotlib.use('Agg')

Or you can run your script from the command line with the -d flag

  python myscript.py -dAgg # will generate PNG, no GUI
  python myscript.py -dGTKAgg # will popup a GTK GUI
  python myscript.py -dTKAgg # will popup a Tkinter GUI

This is convenient because the same script, unmodified, can either be
used in a headless or a GUI environment.

We support 6 user interface toolkits across 4 or more operating
systems and many UI, python and OS versions. We basically decided it
is too hard to guess what a user is trying to do across these
environments (embedding in apps, web app servers, headless scripts
generating PNG, working interactively from the shell), so we try to
make it easy for them to express their preferences.

If you have concrete suggestions on how we might accurately do this
detection in these scenarios, feel free to contribute, but a solution
that works for one environment, windowing system, or user interface
toolkit is of limited use.

See also

  http://matplotlib.sourceforge.net/faq/installing_faq.html#what-is-a-backend

JDH

···

On Thu, Jul 8, 2010 at 9:16 AM, Neal Becker <ndbecker2@...149...> wrote:

Thanks for the info, but I still think this is rude behavior for a python
module, and believe it would be a good thing to fix it.

It's not that simple. In order to get text metrics and start laying out a plot for some backends (notably Gtk and Wx), the GUI framework needs to be imported. Sure, we could import them on demand once the user actually starts doing stuff, but that would require sprinkling the code with "import gtk" (etc.) in a number of functions, possibly decreasing performance, and it would only delay the kind of exception you point out, not eliminate it.

Mike

···

On 07/08/2010 10:16 AM, Neal Becker wrote:

Thanks for the info, but I still think this is rude behavior for a python
module, and believe it would be a good thing to fix it.

Michael Droettboom wrote:

You need to set your backend to a non-GUI one (such as Agg) if you want
to run without a display. This FAQ answer may be helpful:

http://matplotlib.sourceforge.net/faq/howto_faq.html#matplotlib-in-a-web-
     

application-server
   

Mike

On 07/08/2010 08:26 AM, Neal Becker wrote:
     

A python module of mine could draw a plot, and so imports from
matplotlib. Actually, plotting is part of the module test that is in
   if __name__==__main__

But this kills my job trying to run non-interactively.
This is IMO rather nasty behavior. matplotlib should not try to open the
display just because you import it.

      from matplotlib.pyplot import *
    File "/usr/lib64/python2.6/site-packages/matplotlib/pyplot.py", line
    78,
in<module>
      new_figure_manager, draw_if_interactive, show = pylab_setup()
    File
    "/usr/lib64/python2.6/site-packages/matplotlib/backends/__init__.py",
line 25, in pylab_setup
      globals(),locals(),[backend_name])
    File "/usr/lib64/python2.6/site-
packages/matplotlib/backends/backend_gtkagg.py", line 10, in<module>
      from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK,
FigureCanvasGTK,\
    File "/usr/lib64/python2.6/site-
packages/matplotlib/backends/backend_gtk.py", line 8, in<module>
      import gtk; gdk = gtk.gdk
    File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py",
    line
64, in<module>
      _init()
    File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py",
    line
52, in _init
      _gtk.init_check()
RuntimeError: could not open display

------------------------------------------------------------------------------
   

This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel
   
--
Michael Droettboom
Science Software Branch
Space Telescope Science Institute
Baltimore, Maryland, USA

We've had users run into the same problem (and have a similar reaction). At least on our end, there was a key assumption that users were making about how a plotting system worked. They expected to be able to do something like this:

... plot stuff here
if outputFile:
   P.savefig( "plot.png" )
else:
   P.show()

The thing they were missing is that they assumed that you don't need to have anything to do w/ the GUI until it pops up on the screen (i.e. until the show call) which just isn't true right now. Depending on the backend, the gui is a required element during the construction of the plot because it's doing the drawing and layout.

One item we thought might help (or at least be more obvious) but haven't had time to look at what it would take to implement is to be able to control the backend on a per figure basis - more of an object-oriented control after you create a figure. Something along the lines of:

f = Figure(...)
if outputFile:
   f.set_backend( "agg" )

...

Ted

···

________________________________________
From: Michael Droettboom [mdroe@...31...]
Sent: Thursday, July 08, 2010 8:02 AM
To: matplotlib-devel@lists.sourceforge.net
Subject: Re: [matplotlib-devel] nasty import behavior

It's not that simple. In order to get text metrics and start laying out
a plot for some backends (notably Gtk and Wx), the GUI framework needs
to be imported. Sure, we could import them on demand once the user
actually starts doing stuff, but that would require sprinkling the code
with "import gtk" (etc.) in a number of functions, possibly decreasing
performance, and it would only delay the kind of exception you point
out, not eliminate it.

Mike

On 07/08/2010 10:16 AM, Neal Becker wrote:

Thanks for the info, but I still think this is rude behavior for a python
module, and believe it would be a good thing to fix it.

Michael Droettboom wrote:

You need to set your backend to a non-GUI one (such as Agg) if you want
to run without a display. This FAQ answer may be helpful:

http://matplotlib.sourceforge.net/faq/howto_faq.html#matplotlib-in-a-web-

application-server

Mike

On 07/08/2010 08:26 AM, Neal Becker wrote:

A python module of mine could draw a plot, and so imports from
matplotlib. Actually, plotting is part of the module test that is in
   if __name__==__main__

But this kills my job trying to run non-interactively.
This is IMO rather nasty behavior. matplotlib should not try to open the
display just because you import it.

      from matplotlib.pyplot import *
    File "/usr/lib64/python2.6/site-packages/matplotlib/pyplot.py", line
    78,
in<module>
      new_figure_manager, draw_if_interactive, show = pylab_setup()
    File
    "/usr/lib64/python2.6/site-packages/matplotlib/backends/__init__.py",
line 25, in pylab_setup
      globals(),locals(),[backend_name])
    File "/usr/lib64/python2.6/site-
packages/matplotlib/backends/backend_gtkagg.py", line 10, in<module>
      from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK,
FigureCanvasGTK,\
    File "/usr/lib64/python2.6/site-
packages/matplotlib/backends/backend_gtk.py", line 8, in<module>
      import gtk; gdk = gtk.gdk
    File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py",
    line
64, in<module>
      _init()
    File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py",
    line
52, in _init
      _gtk.init_check()
RuntimeError: could not open display

------------------------------------------------------------------------------

This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

--
Michael Droettboom
Science Software Branch
Space Telescope Science Institute
Baltimore, Maryland, USA

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel