Deploying 'Django for beginners' app to Heroku fails with ModuleNotFoundError: No module named '_tkinter'

Deploying 'Django for beginners' app to Heroku fails with ModuleNotFoundError: No module named '_tkinter'

我正在关注William Vincents Django for Beginners,我准备在第8章结束时将博客应用程序推送到Heroku。我检查了几次代码,一切都和书中一样,但它每次都因 ModuleNotFoundError 失败:没有名为“_tkinter”的模块。这是来自 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

错误显示您的 blog/models.py:

from tkinter import CASCADE

那条线当然不应该在那里。 Tkinter 是一个图形工具包,在 Django 项目中以任何身份使用它都是非常不寻常的。

我怀疑您使用的 IDE 可以“帮助”猜测丢失的导入并自动添加它们,可能是因为您在该文件的其他地方输入了 CASCADE

删除该行(以及您不需要的任何其他导入,尤其是与 Tkinter 相关的导入),提交并重新部署。