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 [md...@stsci.edu]
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
------------------------------------------------------------------------------
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

Reply via email to