import matplotlib failing on Heroku import matplotlib failing on Heroku tkinter tkinter

import matplotlib failing on Heroku


For me this works:

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

import matplotlibmatplotlib.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


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.