I am following William Vincents Django for Beginners, and I'm about to push the blog app to Heroku at the end of chapter 8. I have checked the code several times, and everything is just as in the book, but it fails every time with ModuleNotFoundError: No module named '_tkinter'. Here is the log from Heroku:
-----> Building on the Heroku-20 stack
-----> Determining which buildpack to use for this app
-----> Python app detected
-----> Using Python version specified in runtime.txt
! Python has released a security update! Please consider upgrading to python-3.10.4
Learn More: https://devcenter.heroku.com/articles/python-runtimes
-----> Installing python-3.10.3
-----> Installing pip 22.0.4, setuptools 60.10.0 and wheel 0.37.1
-----> Installing SQLite3
-----> Installing requirements with pip
Collecting asgiref==3.5.0
Downloading asgiref-3.5.0-py3-none-any.whl (22 kB)
Collecting Django==4.0.4
Downloading Django-4.0.4-py3-none-any.whl (8.0 MB)
Collecting gunicorn==20.1.0
Downloading gunicorn-20.1.0-py3-none-any.whl (79 kB)
Collecting sqlparse==0.4.2
Downloading sqlparse-0.4.2-py3-none-any.whl (42 kB)
Collecting tzdata==2022.1
Downloading tzdata-2022.1-py2.py3-none-any.whl (339 kB)
Collecting whitenoise==5.3.0
Downloading whitenoise-5.3.0-py2.py3-none-any.whl (19 kB)
Installing collected packages: whitenoise, tzdata, sqlparse, gunicorn, asgiref, Django
Successfully installed Django-4.0.4 asgiref-3.5.0 gunicorn-20.1.0 sqlparse-0.4.2 tzdata-2022.1 whitenoise-5.3.0
-----> $ python manage.py collectstatic --noinput
Traceback (most recent call last):
File "/tmp/build_8fe73f22/manage.py", line 22, in <module>
main()
File "/tmp/build_8fe73f22/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/__init__.py", line 420, in execute
django.setup()
File "/app/.heroku/python/lib/python3.10/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/app/.heroku/python/lib/python3.10/site-packages/django/apps/registry.py", line 116, in populate
app_config.import_models()
File "/app/.heroku/python/lib/python3.10/site-packages/django/apps/config.py", line 304, in import_models
self.models_module = import_module(models_module_name)
File "/app/.heroku/python/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/tmp/build_8fe73f22/blog/models.py", line 1, in <module>
from tkinter import CASCADE
File "/app/.heroku/python/lib/python3.10/tkinter/__init__.py", line 37, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
! Error while running '$ python manage.py collectstatic --noinput'.
See traceback above for details.
You may need to update application code to resolve this error.
Or, you can disable collectstatic for this application:
$ heroku config:set DISABLE_COLLECTSTATIC=1
https://devcenter.heroku.com/articles/django-assets
! Push rejected, failed to compile Python app.
! Push failed
CodePudding user response:
The error shows that you have this in your blog/models.py
:
from tkinter import CASCADE
That line certainly shouldn't be there. Tkinter is a graphical toolkit, and using it in any capacity in a Django project would be very unusual.
I suspect you are using an IDE that is "helpfully" guessing about missing imports and automatically adding them, probably because you typed CASCADE
somewhere else in that file.
Remove that line (and any other imports you don't need, especially ones related to Tkinter), commit, and redeploy.