Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import matplotlib failing on Heroku

[(Basically identical to this from a few months ago: import matplotlib failing with No module named _tkinter on heroku . However, the only solution provided doesn't seem to work. (Unfortunately I can't comment on the answer given there since I don't have enough StackOverflow reputation.))]

I've been plotting using matplotlib in my app. Everything works fine locally. However when I push my app to Heroku I get the error:

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

I've tried to circumvent Tkinter by doing:

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

However this still throws the same error.

Has anyone found a solution for this or have a Heroku app with matplotlib that is working? I'm running Python 2.7.13 (that's also the version Heroku installs when pushing the app).

like image 466
Nils Mackay Avatar asked Apr 29 '17 15:04

Nils Mackay


People also ask

Why is Matplotlib not importing?

The Python "ModuleNotFoundError: No module named 'matplotlib'" occurs when we forget to install the matplotlib module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install matplotlib command.

What version of Python does heroku use?

Specifying a Python version By default, newly created Python apps use the python-3.10. 8 runtime. You can also specify a different supported Python version.

What is import Matplotlib Pyplot?

matplotlib. pyplot is a collection of command style functions that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.


1 Answers

For me this works:

Change the matplotlib backend from tkinter to something else. At the very beginning of the program do this:

import matplotlib
matplotlib.use('Agg')

This way the rest of the program will use the backend that you set ('Agg', 'SVG', etc, etc)

Another option would be to try and mess with the The matplotlibrc file per: https://matplotlib.org/users/customizing.html#the-matplotlibrc-file

like image 56
mgcdanny Avatar answered Sep 24 '22 17:09

mgcdanny