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?

1
  • 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? Commented Apr 26, 2023 at 19:57

1 Answer 1

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 :-)

Sign up to request clarification or add additional context in comments.

2 Comments

for sake of correctness, import matplotlib before anything else.
Also, you need to make sure to put this line before any other packages that might also be importing matplotlib, e.g., skimage

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.