gtalarico / django-vue-template

Django Rest + Vue JS Template
https://django-vue-template-demo.herokuapp.com/
MIT License
1.59k stars 406 forks source link

ImportError for django apps in INSTALLED_APPS on Heroku #42

Closed CodeSpent closed 5 years ago

CodeSpent commented 5 years ago

No structural changes other than using the standard settings.py layout rather than separating dev & prod.

When running collectstatic Heroku is failing to build with an ImportError on my custom Django apps. This is not an issue locally.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'rest_framework',

    'users',
    'channels',
    'socials',
]

Down to rest_framework works fine, however, users, channels, socials are getting the ImportError.

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

       Traceback (most recent call last):

         File "manage.py", line 15, in <module>

           execute_from_command_line(sys.argv)

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

           utility.execute()

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

           django.setup()

         File "/app/.heroku/python/lib/python3.6/site-packages/django/__init__.py", line 24, in setup

           apps.populate(settings.INSTALLED_APPS)

         File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/registry.py", line 89, in populate

           app_config = AppConfig.create(entry)

         File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/config.py", line 90, in create

           module = import_module(entry)

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

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

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

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

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

       ModuleNotFoundError: No module named 'users'

 !     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

I do understand this is likely an issue on my end, however, I've deployed plenty of Django apps previously without this issue, but using this template prompts this issue so wondering if there's any immediate insight as to what may be happening?

File structure: image

gtalarico commented 5 years ago

I think it's because you nested your apps inside of another app. Move folders or use "api.users"

CodeSpent commented 5 years ago

I did try prefixing with api. on them, but then I'm prompted with an ImportError stating api couldn't be found. You mention nesting my app within another app, but this is just a directory, so long as the path is set and the settings module is imported this shouldn't be an issue I'd imagine.

I can try restructuring here and just include my .venv at the root of the Django project.

CodeSpent commented 5 years ago

After restructure its the same error (did delete Heroku app to avoid caching issues).

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

       Traceback (most recent call last):

         File "manage.py", line 15, in <module>

           execute_from_command_line(sys.argv)

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

           utility.execute()

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

           django.setup()

         File "/app/.heroku/python/lib/python3.6/site-packages/django/__init__.py", line 24, in setup

           apps.populate(settings.INSTALLED_APPS)

         File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/registry.py", line 89, in populate

           app_config = AppConfig.create(entry)

         File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/config.py", line 90, in create

           module = import_module(entry)

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

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

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

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

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

       ModuleNotFoundError: No module named 'users'

 !     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

New structure: image

Any other ideas what may cause this? All modules have an __init__.py file as well.

CodeSpent commented 5 years ago

After a good bit of comparison I found the issue, and its mainly that django-vue-template breaks away from traditional Django project structure. I found that I was looking from the wrong perspective and eventually nailed this down.

Here is my StackOverflow question for sake of archiving for any future users having the same issue: https://stackoverflow.com/questions/58412019/django-apps-throw-importerror-on-heroku/58413361#58413361

Ultimately this shouldn't be an issue for users who are cloning this repo directly for a new project, but for existing projects such as mine that are using this as reference, there are a few "gotchas" covered in some detail there. :)