[(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).
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With