-1

I've deployed a Flask application on Heroku server but it isn't working I see this in the browser

Application error An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details.

What causes this error?

I got the following when I run heroku logs What are these things and how can I fix it?

2018-04-25T18:51:16.513351+00:00 app[web.1]:     _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
2018-04-25T18:51:16.513353+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-ile "/app/.heroku/python/lib/python3.6/site-packages/matplotlib/backends/backend_tkagg.py", line 4, in <module>
2018-04-25T18:51:16.596215+00:00 app[web.1]:     from . import tkagg  # Paint image to Tk photo blitter extension.
2018-04-25T18:51:16.596217+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/matplotlib/backends/tkagg.py", line 5, in <module>
2018-04-25T18:51:16.596218+00:00 app[web.1]:     from six.moves import tkinter as Tk
2018-04-25T18:51:16.596220+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/six.py", line 92, in __get__
2018-04-25T18:51:16.596222+00:00 app[web.1]:     result = self._resolve()
2018-04-25T18:51:16.596223+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/six.py", line 115, in _resolve
2018-04-25T18:51:16.596225+00:00 app[web.1]:     return _import_module(self.mod)
2018-04-25T18:51:16.596227+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/six.py", line 82, in _import_module
2018-04-25T18:51:16.596229+00:00 app[web.1]:     __import__(name)
2018-04-25T18:51:16.596230+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/tkinter/__init__.py", line 36, in <module>
2018-04-25T18:51:16.596232+00:00 app[web.1]:     import _tkinter # If this fails your Python may not be configured for Tk
2018-04-25T18:51:16.596720+00:00 app[web.1]: [2018-04-25 18:51:16 +0000] [9] [INFO] Worker exiting (pid: 9)
2018-04-25T18:51:16.596241+00:00 app[web.1]: ModuleNotFoundError: No module named '_tkinter'
2018-04-25T18:51:16.739539+00:00 app[web.1]: [2018-04-25 18:51:16 +0000] [4] [INFO] Shutting down: Master
2018-04-25T18:51:16.741786+00:00 app[web.1]: [2018-04-25 18:51:16 +0000] [4] [INFO] Reason: Worker failed to boot.
2018-04-25T18:51:16.862528+00:00 heroku[web.1]: State changed from up to crashed
2018-04-25T18:51:16.850843+00:00 heroku[web.1]: Process exited with status 3
(venv) MacBook-Pro-alkhas-b-Amjad:flaskhpbio joodi$ 

Does this mean there are things missing?

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
shoseta
  • 45
  • 1
  • 5
  • 1
    Do you have the [Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli) installed? If so, please run `heroku logs`, then [edit] your question to add the output of that command. (If you don't have the CLI installed your best bet is to install it and then proceed as I suggested above.) – ChrisGPT was on strike Apr 25 '18 at 18:24
  • @Chris I have Heroku CLI, this commend `heroku logs` should I run it in my application app directory? If so, I tried it and I got long list – shoseta Apr 25 '18 at 19:08
  • A long list is good :-). That gives detail about what's failing. Your application is trying to import Tkinter. That won't work on Heroku. Do you know why it's doing that? – ChrisGPT was on strike Apr 25 '18 at 19:09
  • @Chris What is Tkinter and why it won't work on Heroku? I've created virtual environment in my app and I download several packages, but I didn't use Tkinter – shoseta Apr 25 '18 at 19:16
  • Tkinter is a Python interface to [Tk](https://en.wikipedia.org/wiki/Tk_(software)), a graphical toolkit. Tkinter is usually included with Python but it doesn't make much sense on the web. A common use would be to build graphical software to run on the desktop. – ChrisGPT was on strike Apr 25 '18 at 19:20
  • @Chris I'm using matplotlib package my website, is that related? and what i supposed to do with Tkinter to get my app? – shoseta Apr 25 '18 at 19:27

1 Answers1

0

Your traceback shows that your application is trying to import Tkinter:

ModuleNotFoundError: No module named '_tkinter'

It sounds like matplotlib is the library that's trying to do the import.

Tkinter isn't available on Heroku. The good news is that matplotlib supports multiple backends, some of which work on headless servers. Try modifying your code to use a different backend, like Agg:

import matplotlib
matplotlib.use('Agg')  # Must be done before importing pyplot
import matplotlib.pyplot as plt

If you use the use() function, this must be done before importing matplotlib.pyplot. Calling use() after pyplot has been imported will have no effect. Using use() will require changes in your code if users want to use a different backend. Therefore, you should avoid explicitly calling use() unless absolutely necessary.

You'll have to commit this change and push it to Heroku.

See also Python Headless MatplotLib / Pyplot

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257