13

I'm trying to use matplotlib in my application. I created a virtualenv in python2.7, pip installed matplotlib, and it's successfully running on local.

However, when I deploy the app to heroku (after pip freeze and other steps necessary), my app crashes. When I check the log, I see the following:

import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter

It's weird because the app was successfully running on local under venv. Is the heroku python environment not configured to run matplotlib? If so, what steps should I take to enable that?

alpaca
  • 1,211
  • 13
  • 23
  • Suppose you were to use `matplotlib.show` in the code. Where would you expect the figure to show up? On a monitor at the server farm? (Why would there be one in the first place?) In the user's web browser? (How should it translate from a GUI window into HTML?) Something else? – Karl Knechtel Apr 26 '23 at 19:57

1 Answers1

11

This should do the trick

matplotlib.use('Agg')
import matplotlib.pyplot as plt

This will set your Matplotlib backend to use Agg instead of Tk. Just worked for me at least :-)

Ahmed Haque
  • 7,174
  • 6
  • 26
  • 33
  • 4
    for sake of correctness, `import matplotlib` before anything else. – AruniRC Jan 05 '17 at 05:20
  • 1
    Also, you need to make sure to put this line before any other packages that might also be importing matplotlib, e.g., skimage – mobeets May 21 '17 at 00:42