1

I have a GUI-less cloud server running Bitnami-Django Ubuntu 14.04 LTS that is meant to retrieve and graph data for users, but it cannot produce the graphs. To be clear, I only care that the graph image is produced and saved, not that a user has an option to click a button to save the image. Such functionality would be meaningless for such a server.

On my normal Ubuntu Linux (Mate) 14.04 LTS, the scripts work perfectly, producing a matplotlib.pyplot from the relevant data in a GUI window with save, zoom, rotate and other functionality; however on the cloud server I get this error, even if I don't try to invoke the show() function:

bitnami@StockPredix:/opt/bitnami/apps/django/django_projects/Project$ python api-test_volume.py 
Traceback (most recent call last):
  File "api-test_volume.py", line 8, in <module>
    import matplotlib.pyplot as plt
  File "/opt/bitnami/python/lib/python2.7/site-packages/matplotlib/pyplot.py", line 114, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/opt/bitnami/python/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/opt/bitnami/python/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
    from matplotlib.externals.six.moves import tkinter as Tk
  File "/opt/bitnami/python/lib/python2.7/site-packages/matplotlib/externals/six.py", line 199, in load_module
    mod = mod._resolve()
  File "/opt/bitnami/python/lib/python2.7/site-packages/matplotlib/externals/six.py", line 113, in _resolve
    return _import_module(self.mod)
  File "/opt/bitnami/python/lib/python2.7/site-packages/matplotlib/externals/six.py", line 80, in _import_module
    __import__(name)
  File "/opt/bitnami/python/lib/python2.7/lib-tk/Tkinter.py", line 39, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter

This continued even after I installed other dependencies and tried installing tk-dev, tcl-dev, etc. I think I will need an alternative to matplotlib to produce the graphs, unless one of you kind souls knows a clever workaround. Thanks in advance for your help.

(Quick) Edit: I'm aware of and tried the fix in Save plot to image file instead of displaying it using Matplotlib, but on my cloud server this is a matter of broken dependency with the GUI in the first place, instead of just suppressing the GUI.

Community
  • 1
  • 1

1 Answers1

0

This may not be an actual answer because I couldn't test it, but it seems the error comes from the fact that tkinter is tried to be loaded unsuccessfully. So the naturaly solution might be to avoid using the Tk backend.

There are several backends in matplotlib and some are not meant for interactive plotting. You'll get those with

import matplotlib 
print matplotlib.rcsetup.non_interactive_bk

(make sure to run this before importing pyplot). The list contains

[u'agg', u'cairo', u'emf', u'gdk', u'pdf', u'pgf', u'ps', u'svg', u'template']

and potentially any of those might suit your case. To select one of them, use

matplotlib.use('<name of backend>')

(still before importing pyplot)

Finally import pyplot and do your stuff, avoiding plt.show(). It may also be that some functionalities are not present for some of the backends - that needs to be tested, e.g. saving an svg from the pdf backend would not work.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712