Search code examples
pythonpython-3.xherokupython-3.5

Heroku Python3.5 Import Error : No module named ='_tkinter'


While deploying in Heroku and adding customized buildpacks such as libspatialindex, another error occurred where Python 3.5 now looks for Tkinter.

Locally, by installing using sudo apt-get tk-dev this would be solved and trying out the suggestion from this similar problem: import matplotlib failing on Heroku, the error still persists.

Here are my buildpacks:

https://github.com/heroku/heroku-buildpack-apt 
heroku/python
https://github.com/julienfr112/libspatialindex-buildpack.git

And my Aptfile containing only:

python3-tk
libpq-dev
build-essential
libncursesw5-dev
libreadline5-dev
libssl-dev
libgdbm-dev
libc6-dev
libsqlite3-dev tk-dev
libbz2-dev

On Heroku push here's the tail of the log:

2017-09-05T08:25:58.903075+00:00 app[web.1]:   File "/app/.heroku
/python/lib/python3.5/site-packages/six.py", line 82, in _import_module
2017-09-05T08:25:58.903076+00:00 app[web.1]:     __import__(name)
2017-09-05T08:25:58.903076+00:00 app[web.1]:   File "/app/.heroku
/python/lib/python3.5/tkinter/__init__.py", line 35, in <module>
2017-09-05T08:25:58.903076+00:00 app[web.1]:     import _tkinter 
# If this fails your Python may not be configured for Tk
2017-09-05T08:25:58.903077+00:00 app[web.1]: ImportError: No module
named '_tkinter'

Any ideas?


Solution

  • 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