简体   繁体   中英

Django on Heroku: KeyError: 'collectstatic'

I'm trying to deploy my simple Django web app on Heroku, but the build fails with the following error:

       Successfully installed asgiref-3.3.4 dj-database-url-0.5.0 django-3.2 django-heroku-0.3.1 gunicorn-20.1.0 numpy-1.20.2 pillow-8.2.0 psycopg2-2.8.6 pytz-2021.1 sqlparse-0.4.1 torch-1.8.1 torchvision-0.9.1 typing-extensions-3.7.4.3 whitenoise-5.2.0

-----> $ python pytorch_django/manage.py collectstatic --noinput

       Traceback (most recent call last):

         File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 237, in fetch_command

           app_name = commands[subcommand]

       KeyError: 'collectstatic'

       During handling of the above exception, another exception occurred:

       Traceback (most recent call last):

         File "/tmp/build_6b3954ac/pytorch_django/manage.py", line 22, in <module>

           main()

         File "/tmp/build_6b3954ac/pytorch_django/manage.py", line 18, in main

           execute_from_command_line(sys.argv)

         File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line

           utility.execute()

         File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute

           self.fetch_command(subcommand).run_from_argv(self.argv)

         File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 244, in fetch_command

           settings.INSTALLED_APPS

         File "/app/.heroku/python/lib/python3.9/site-packages/django/conf/__init__.py", line 82, in __getattr__

           self._setup(name)

         File "/app/.heroku/python/lib/python3.9/site-packages/django/conf/__init__.py", line 69, in _setup

           self._wrapped = Settings(settings_module)

         File "/app/.heroku/python/lib/python3.9/site-packages/django/conf/__init__.py", line 170, in __init__

           mod = importlib.import_module(self.SETTINGS_MODULE)

         File "/app/.heroku/python/lib/python3.9/importlib/__init__.py", line 127, in import_module

           return _bootstrap._gcd_import(name[level:], package, level)

         File "<frozen importlib._bootstrap>", line 1030, in _gcd_import

         File "<frozen importlib._bootstrap>", line 1007, in _find_and_load

         File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked

         File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed

         File "<frozen importlib._bootstrap>", line 1030, in _gcd_import

         File "<frozen importlib._bootstrap>", line 1007, in _find_and_load

         File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked

       ModuleNotFoundError: No module named 'project'

 !     Error while running '$ python pytorch_django/manage.py collectstatic --noinput'.

I have successfully deployed other Django apps on Heroku and I don't get what's wrong here. What does KeyError: 'collectstatic' mean or where does it come from?

Locally, the app runs fine and also python manage.py collectstatic runs fine. What's the problem? Could it be related to Python 3.9? I have Python 3.8 locally.

Edit: This also happens with a freshly created Django app - no changes in the code whatsover.

The problem was my own fault but not in the Django app but in my Heroku configuration. Inside my Heroku environmental variables ( Settings > Config Vars ), I had set the DJANGO_SETTINGS_MODULE incorrectly to project.prod_settings . However, my Django project was called pytorch_django so I had to set it to pytorch_django.prod_settings .

That's what caused the error ModuleNotFoundError: No module named 'project' . Changing DJANGO_SETTINGS_MODULE to the correct path solved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM