9

[(Basically identical to this from a few months ago: https://stackoverflow.com/questions/41319082/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).

[Addition as autobot claims this is a duplicate question which it is not: Note that this question is specifically related to using matplotlib on Heroku. Therefore, any other question unrelated to Heroku does not help in answering this question]

Nils Mackay
  • 431
  • 4
  • 14

2 Answers2

4

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

mgcdanny
  • 1,082
  • 1
  • 13
  • 20
  • Thanks for this answer. It seems to have helped people. For me, as I mentioned in the question, this did not help. I'm just mentioning this here since StackOverflow thinks this is a duplicated question that can be solved by your suggestion — and wants to close the question. For the issue I was facing this wasn't the solution. – Nils Mackay May 02 '23 at 14:04
2

I contacted Heroku support and got the following answer:

"The default Heroku Python build doesn't contain the libs needed for tkinter to be imported successfully. You can find a list of the underlying libraries we offer by default here: https://devcenter.heroku.com/articles/cedar-ubuntu-packages

In cases like these, we provide the opportunity for developers to extend the platform using third party buildpacks https://devcenter.heroku.com/articles/third-party-buildpacks many of which you can browse at https://elements.heroku.com/buildpacks These can install extra packages and dependencies at deploy time.

Please be aware that third party buildpacks are an option but the buildpacks themselves are unsupported. They might not be maintained by the original authors and have stopped working. If that's the case, you'll need to contact the original author to see if they can help. There's some suggestion on this SO post that other users have used custom Python buildpacks to run this successfully https://stackoverflow.com/a/18184536

Another option might be to use the experimental Apt buildpack here: https://github.com/heroku/heroku-buildpack-apt You would need to run

heroku buildpacks:add --index 1 https://github.com/heroku/heroku-buildpack-apt

You would also need to add a file named Aptfile at the root of the project with the following contents:

python3-tk

Unfortunately, this Aptfile doesn't have the same dependency resolution so you will have to specify any other packages manually."

I installed the following buildpack: https://github.com/thenovices/heroku-buildpack-scipy. Which solved the issues with matplotlib.

Community
  • 1
  • 1
Nils Mackay
  • 431
  • 4
  • 14
  • Trying this out using python3.5 in Heroku however I'm still encountering the same error. I posted my question here: https://stackoverflow.com/questions/46051175/heroku-python3-5-import-error-no-module-named-tkinter Any more ideas? – Reiion Sep 05 '17 at 09:16
  • 1
    Unfortunately, no. I was stuck at this for a while too. The above solution worked for me, but might not work for you due to the different Python version. Maybe you could try to find a buildpack that works for Python 3.5. – Nils Mackay Sep 06 '17 at 10:35
  • Can you give more information on using this stuff? I don't understand how to use it. – mimic Jul 24 '18 at 19:55