8000 Make matplotlib fail more gracefully in headless environments · Issue #6739 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Make matplotlib fail more gracefully in headless environments #6739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ngoldbaum opened this issue Jul 14, 2016 · 5 comments
Closed

Make matplotlib fail more gracefully in headless environments #6739

ngoldbaum opened this issue Jul 14, 2016 · 5 comments
Milestone

Comments

@ngoldbaum
Copy link
Contributor

This is reproducible on any headless environment using matplotlib 1.5.1. On my system, the following script:

from matplotlib import pyplot as plt
plt.plot([1, 2], [1, 2])

Will produce the following traceback:

---------------------------------------------------------------------------
TclError                                  Traceback (most recent call last)
<ipython-input-2-8dd816f9ddc7> in <module>()
----> 1 plt.plot([1, 2], [1, 2])

/home/goldbaum/.virtualenvs/yt-dev/local/lib/python2.7/site-packages/matplotlib/pyplot.pyc in plot(*args, **kwargs)
   3145 @_autogen_docstring(Axes.plot)
   3146 def plot(*args, **kwargs):
-> 3147     ax = gca()
   3148     # allow callers to override the hold state by passing hold=True|False
   3149     washold = ax.ishold()

/home/goldbaum/.virtualenvs/yt-dev/local/lib/python2.7/site-packages/matplotlib/pyplot.pyc in gca(**kwargs)
    926     matplotlib.figure.Figure.gca : The figure's gca method.
    927     """
--> 928     return gcf().gca(**kwargs)
    929
    930 # More ways of creating axes:

/home/goldbaum/.virtualenvs/yt-dev/local/lib/python2.7/site-packages/matplotlib/pyplot.pyc in gcf()
    576         return figManager.canvas.figure
    577     else:
--> 578         return figure()
    579
    580

/home/goldbaum/.virtualenvs/yt-dev/local/lib/python2.7/site-packages/matplotlib/pyplot.pyc in figure(num, figsize, dpi, facecolor, edgecolor, frameon, FigureClass, **kwargs)
    525                                         frameon=frameon,
    526                                         FigureClass=FigureClass,
--> 527                                         **kwargs)
    528
    529         if figLabel:

/home/goldbaum/.virtualenvs/yt-dev/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.pyc in new_figure_manager(num, *args, **kwargs)
     82     FigureClass = kwargs.pop('FigureClass', Figure)
     83     figure = FigureClass(*args, **kwargs)
---> 84     return new_figure_manager_given_figure(num, figure)
     85
     86

/home/goldbaum/.virtualenvs/yt-dev/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.pyc in new_figure_manager_given_figure(num, figure)
     90     """
     91     _focus = windowing.FocusManager()
---> 92     window = Tk.Tk()
     93     window.withdraw()
     94

/usr/lib/python2.7/lib-tk/Tkinter.pyc in __init__(self, screenName, baseName, className, useTk, sync, use)
   1765                 baseName = baseName + ext
   1766         interactive = 0
-> 1767         self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
   1768         if useTk:
   1769             self._loadtk()

TclError: no display name and no $DISPLAY environment variable

I think it would be much nicer if backends somehow detected these sorts of errors, fell back to the Agg backend (probably with a warning) and then goes ahead and makes the plot.

@tacaswell tacaswell modified the milestones: 2.0.1 (next bug fix release), 2.1 (next point release) Jul 14, 2016
@tacaswell
Copy link
Member

This is to some degree a configuration/packaging issue. On servers that are known to be headless, the system wide configuration file should be set to Agg as the default backend.

A way to do this is to require something on the backend be queryable if there are sufficient run-time requirements (which also makes sense for the GUI backends more generally as we do not know until we try if the python wrappers for the backend are even installed).

This should probably be done in pylab_setup in backend.__init__. Maybe add a function backend_usable( ) -> bool to the backends (and fall back to lambda : True if it does not exist).

@WeatherGod
Copy link
Member

One place where this is not a configuration/packaging issue (at least, not
immediately obvious) is user crontabs. Program works fine from the command
line, but can then fail in the crontab. Not a hard problem to solve (set
the MPLBACKEND variable at the top of the crontab to "agg"), but that may
not be obvious to many people. But a solution of setting the matplotlibrc
file to "agg" wouldn't be advisable in this situation because the user may
still want the GUI for their account.

On Thu, Jul 14, 2016 at 3:47 PM, Thomas A Caswell notifications@github.com
wrote:

This is to some degree a configuration/packaging issue. On servers that
are known to be headless, the system wide configuration file should be set
to Agg as the default backend.

A way to do this is to require something on the backend be queryable if
there are sufficient run-time requirements (which also makes sense for the
GUI backends more generally as we do not know until we try if the python
wrappers for the backend are even installed).

This should probably be done in pylab_setup in backend.init. Maybe
add a function backend_usable( ) -> bool to the backends (and fall back
to lambda : True if it does not exist).


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#6739 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AARy-P5kjUCMOEfZzoeeN9HfIOBV6TLqks5qVpJcgaJpZM4JMuJE
.

@con-f-use
Copy link
con-f-use commented Mar 6, 2017

As and avid user, I just want to say a built-in fallback should be in place here. +1

So far I'm using this in my scripts:

if 'DISPLAY' not in os.environ: matplotlib.use('agg')

Is there a better workaround?

@anntzer
Copy link
Contributor
anntzer commented Jul 30, 2017

#8613 (comment) would also solve the issue: one would just need to put Agg at the end of the list of backends, rather than having even more specific fallback code.

@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.2 (next next feature release) Oct 3, 2017
@anntzer anntzer modified the milestones: needs sorting, v3.0 Sep 19, 2018
@anntzer
Copy link
Contributor
anntzer commented Sep 19, 2018

Closed by #9795 + following PRs.

@anntzer anntzer closed this as completed Sep 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
0