1

I am trying to deploy a webapp using Heroku and it get deployed but it gives internal server error when I try to open it. The complete log as from heroku logs -t is as follows:

2021-10-29T09:03:55.451439+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/core/handlers/exception.py", line 152, in handle_uncaught_exception
2021-10-29T09:03:55.451439+00:00 app[web.1]: callback = resolver.resolve_error_handler(500)
2021-10-29T09:03:55.451442+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/urls/resolvers.py", line 611, in resolve_error_handler
2021-10-29T09:03:55.451442+00:00 app[web.1]: callback = getattr(self.urlconf_module, 'handler%s' % view_type, None)
2021-10-29T09:03:55.451442+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
2021-10-29T09:03:55.451443+00:00 app[web.1]: res = instance.__dict__[self.name] = self.func(instance)
2021-10-29T09:03:55.451443+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/urls/resolvers.py", line 591, in urlconf_module
2021-10-29T09:03:55.451443+00:00 app[web.1]: return import_module(self.urlconf_name)
2021-10-29T09:03:55.451444+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
2021-10-29T09:03:55.451444+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2021-10-29T09:03:55.451444+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
2021-10-29T09:03:55.451444+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
2021-10-29T09:03:55.451444+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
2021-10-29T09:03:55.451444+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
2021-10-29T09:03:55.451444+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 783, in exec_module
2021-10-29T09:03:55.451444+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2021-10-29T09:03:55.451445+00:00 app[web.1]: File "/app/server/urls.py", line 7, in <module>
2021-10-29T09:03:55.451445+00:00 app[web.1]: path('apis/',include('apis.urls',namespace='apis')),
2021-10-29T09:03:55.451445+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/urls/conf.py", line 34, in include
2021-10-29T09:03:55.451445+00:00 app[web.1]: urlconf_module = import_module(urlconf_module)
2021-10-29T09:03:55.451445+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
2021-10-29T09:03:55.451446+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)2021-10-29T09:03:55.451446+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
2021-10-29T09:03:55.451446+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
2021-10-29T09:03:55.451446+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
2021-10-29T09:03:55.451446+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
2021-10-29T09:03:55.451446+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 783, in exec_module
2021-10-29T09:03:55.451447+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2021-10-29T09:03:55.451447+00:00 app[web.1]: File "/app/apis/urls.py", line 2, in <module>
2021-10-29T09:03:55.451447+00:00 app[web.1]: from apis import views
2021-10-29T09:03:55.451447+00:00 app[web.1]: File "/app/apis/views.py", line 4, in <module>
2021-10-29T09:03:55.451447+00:00 app[web.1]: from apis import model
2021-10-29T09:03:55.451447+00:00 app[web.1]: File "/app/apis/model.py", line 9, in <module>
2021-10-29T09:03:55.451448+00:00 app[web.1]: import PySimpleGUI as sg
2021-10-29T09:03:55.451448+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/PySimpleGUI/__init__.py", line 2, in <module>
2021-10-29T09:03:55.451448+00:00 app[web.1]: from .PySimpleGUI import *
2021-10-29T09:03:55.451448+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/PySimpleGUI/PySimpleGUI.py", line 117, in <module>
2021-10-29T09:03:55.451448+00:00 app[web.1]: import tkinter as tk
2021-10-29T09:03:55.451448+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/tkinter/__init__.py", line 36, in <module>
2021-10-29T09:03:55.451448+00:00 app[web.1]: import _tkinter # If this fails your Python may not be configured for Tk
2021-10-29T09:03:55.451449+00:00 app[web.1]: ModuleNotFoundError: No module named '_tkinter'
2021-10-29T09:03:55.451449+00:00 app[web.1]:
2021-10-29T09:03:55.451454+00:00 app[web.1]: During handling of the above exception, another exception occurred:
2021-10-29T09:03:55.451454+00:00 app[web.1]:
2021-10-29T09:03:55.451455+00:00 app[web.1]: Traceback (most recent call last):
2021-10-29T09:03:55.451455+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 136, in handle
2021-10-29T09:03:55.451455+00:00 app[web.1]: self.handle_request(listener, req, client, addr)
2021-10-29T09:03:55.451455+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 179, in handle_request
2021-10-29T09:03:55.451455+00:00 app[web.1]: respiter = self.wsgi(environ, resp.start_response)
2021-10-29T09:03:55.451456+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/core/handlers/wsgi.py", line 133, in __call__
2021-10-29T09:03:55.451456+00:00 app[web.1]: response = self.get_response(request)
2021-10-29T09:03:55.451456+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/core/handlers/base.py", line 130, in get_response
2021-10-29T09:03:55.451456+00:00 app[web.1]: response = self._middleware_chain(request)
2021-10-29T09:03:55.451462+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/core/handlers/exception.py", line 49, in inner
2021-10-29T09:03:55.451462+00:00 app[web.1]: response = response_for_exception(request, exc)
2021-10-29T09:03:55.451462+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/core/handlers/exception.py", line 114, in response_for_exception
2021-10-29T09:03:55.451462+00:00 app[web.1]: response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
2021-10-29T09:03:55.451462+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/core/handlers/exception.py", line 152, in handle_uncaught_exception
2021-10-29T09:03:55.451463+00:00 app[web.1]: callback = resolver.resolve_error_handler(500)
2021-10-29T09:03:55.451463+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/urls/resolvers.py", line 611, in resolve_error_handler
2021-10-29T09:03:55.451465+00:00 app[web.1]: callback = getattr(self.urlconf_module, 'handler%s' % view_type, None)
2021-10-29T09:03:55.451465+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
2021-10-29T09:03:55.451465+00:00 app[web.1]: res = instance.__dict__[self.name] = self.func(instance)
2021-10-29T09:03:55.451465+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/urls/resolvers.py", line 591, in urlconf_module
2021-10-29T09:03:55.451466+00:00 app[web.1]: return import_module(self.urlconf_name)
2021-10-29T09:03:55.451466+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
2021-10-29T09:03:55.451466+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2021-10-29T09:03:55.451466+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
2021-10-29T09:03:55.451466+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
2021-10-29T09:03:55.451466+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
2021-10-29T09:03:55.451467+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
2021-10-29T09:03:55.451467+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 783, in exec_module
2021-10-29T09:03:55.451467+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2021-10-29T09:03:55.451467+00:00 app[web.1]: File "/app/server/urls.py", line 7, in <module>
2021-10-29T09:03:55.451467+00:00 app[web.1]: path('apis/',include('apis.urls',namespace='apis')),
2021-10-29T09:03:55.451467+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/urls/conf.py", line 34, in include
2021-10-29T09:03:55.451467+00:00 app[web.1]: urlconf_module = import_module(urlconf_module)
2021-10-29T09:03:55.451468+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
2021-10-29T09:03:55.451468+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2021-10-29T09:03:55.451468+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
2021-10-29T09:03:55.451468+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
2021-10-29T09:03:55.451468+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
2021-10-29T09:03:55.451469+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
2021-10-29T09:03:55.451469+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 783, in exec_module
2021-10-29T09:03:55.451469+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2021-10-29T09:03:55.451469+00:00 app[web.1]: File "/app/apis/urls.py", line 2, in <module>
2021-10-29T09:03:55.451469+00:00 app[web.1]: from apis import views
2021-10-29T09:03:55.451470+00:00 app[web.1]: File "/app/apis/views.py", line 4, in <module>
2021-10-29T09:03:55.451470+00:00 app[web.1]: from apis import model
2021-10-29T09:03:55.451470+00:00 app[web.1]: File "/app/apis/model.py", line 9, in <module>
2021-10-29T09:03:55.451470+00:00 app[web.1]: import PySimpleGUI as sg
2021-10-29T09:03:55.451471+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/PySimpleGUI/__init__.py", line 2, in <module>
2021-10-29T09:03:55.451471+00:00 app[web.1]: from .PySimpleGUI import *
2021-10-29T09:03:55.451471+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/PySimpleGUI/PySimpleGUI.py", line 117, in <module>
2021-10-29T09:03:55.451471+00:00 app[web.1]: import tkinter as tk
2021-10-29T09:03:55.451471+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/tkinter/__init__.py", line 36, in <module>
2021-10-29T09:03:55.451471+00:00 app[web.1]: import _tkinter # If this fails your Python may not be configured for Tk
2021-10-29T09:03:55.451472+00:00 app[web.1]: ModuleNotFoundError: No module named '_tkinter'
2021-10-29T09:03:55.451616+00:00 app[web.1]: 10.1.91.66 - - [29/Oct/2021:09:03:55 +0000] "GET /favicon.ico HTTP/1.1" 500 0 "-" "-"
2021-10-29T09:03:55.453961+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=risk-category.herokuapp.com request_id=37f18c7a-c27e-4385-afd7-8efb39882b2f fwd="49.36.187.226" dyno=web.1 connect=0ms service=44ms status=500 bytes=244 protocol=https

I have gone through some other solutions given on here but they all use matplotlib libray while mine doesn't require matplotlib and I haven't mentioned it in requirements.txt file either but it gets installed on when deploying on heroku. I know the heroku does not support _tkinter. So what else can I do for it to run properly?

python version:3.8.8

I can provide more details if required.

Chinmay Datar
  • 77
  • 3
  • 13
  • you you should operate from virtualenv and install all your dependencies inside it, and when you freeze requirements.txt, your venv should be activated and in project directory. Else, freeze will put all modules installed in your pc. Else, it is impossible to serve tkinter, Heroku is a webserver not software server! – HB21 Oct 29 '21 at 10:16
  • I am wokring from virtualenv. Matplotlib and some libraries aren't mentioned in requirements.txt but they get installed on heroku eitherway for some reason. Regarding tkinter I am not using it in my program atall. I dont know why its popping up. – Chinmay Datar Oct 29 '21 at 10:46
  • Probably you freezed requirements.txt outside the project directory or while virtualvenv is not activated. Then after freezing requirements.txt, venv should be deactivated. And while you still in project directory, `git add -A`, `git commit -m "my comment"` and `git push heroku master`, of course if there is an existed app, before pushing, you should do `heroku git:remote -a myapp` – HB21 Oct 29 '21 at 10:56
  • I double checked. I have done all of what you said. – Chinmay Datar Oct 29 '21 at 11:24
  • The problem got solved by just adding ```import matplotlib matplotlib.use('Agg') # Must be done before importing pyplot import matplotlib.pyplot as plt``` to the models file even if you are not using it. – Chinmay Datar Oct 29 '21 at 11:25
  • if your project doesn't need matplotlib, then why it should be installed? – HB21 Oct 29 '21 at 11:28
  • It does not need to be but it was getting installed even without being mentioned in the requirements.txt (I have checked several times now). Eitherway the above thing solved the problem for me. – Chinmay Datar Oct 29 '21 at 11:34
  • Had the same error, try with this solution https://stackoverflow.com/a/69335260/17815679 – shyakadev Feb 28 '22 at 16:38
  • **Reading the stack trace** directly points to the problem. It shows that the code `import PySimpleGUI as sg` in `/app/apis/model.py` is the last point of execution in your own code. Since Tkinter is not available on Heroku, PySimpleGUI isn't either. The name should be the obvious hint: how exactly do you intend to create a program with a GUI that runs on a server? What physical screen should the GUI show up on, and who will interact with it? If you're making a web app, the UI has to be entirely through the browser. – Karl Knechtel Apr 28 '23 at 20:51
  • Voting to close as a typo; the problem here was that OP did not actually pay attention to the project dependencies, and thus ended up with a project that requests unnecessary dependencies that cannot work in a web environment. – Karl Knechtel Apr 28 '23 at 20:52

4 Answers4

2

I had the same error deploying my django app to Heroku. I found that I was importing CASCADE from tkinter in my models.py file. I changed

from tkinder import CASCADE

To

from django.db.models.deletion import CASCADE

and this fixed my issue.

Sometimes it is not always in the models.py file. If that happens to be your case, keep on reading the error message, you will eventually find the file in which tkinter in being imported. I did so and it was at the bottom of the error where I found this:

  File "/app/blog/views.py", line 1, in <module>
from turtle import home

So I just had to delete the first line in my blog/views.py file. And It worked!

So in your case, I think the problem is displayed at.

2021-10-29T09:03:55.451470+00:00 app[web.1]: File "/app/apis/model.py", line 9, in <module>
2021-10-29T09:03:55.451470+00:00 app[web.1]: import PySimpleGUI as sg

According to this link, PySimpleGUI is a python library that wraps tkinter. And that alone gives us the solution. so delete that import from your apis/model.py.

*make sure you commit that change to your git repo and push again to heroku.

1

I had the same issue some days back and I found out that title was auto-imported by vs code. Carefully check all the imports on your script. Something was likely auto-imported.

lator
  • 29
  • 1
  • 6
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32365972) – Simas Joneliunas Aug 04 '22 at 00:15
  • 1
    @lator Thank you for saving my days. I fix my problems in `Views` and `models` file because of Auto import by `VsCode` –  Aug 11 '22 at 12:21
0

I was trying to deploy an application to Heroku and was getting this error.

This error might be because of the import of tkinter somewhere in our code. please check properly and remove it.

enter image description here

  • 2
    If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/late-answers/30883559) – BrokenBenchmark Jan 29 '22 at 06:35
  • 1
    As far as I can tell, this person is not trying to ask their own question; they are saying that they had this problem in the past and giving the solution to it. It is just worded poorly – Lecdi May 09 '22 at 17:17
0

I ran into the same problem. I fixed it by removing all unused imports on my project. After doing this and redeploying, I was able to resolve this issue.

cigien
  • 57,834
  • 11
  • 73
  • 112
Daichi
  • 309
  • 1
  • 2
  • 10