0

I have a Django project with multiple apps set up like:

myproject/
- apps/
  - app1/
  - app2/

Everything works when running python manage.py runserver until I need to share models in app1 with app2. It seems this should be as simple as adding a standard import statement in the appropriate file in app2, e.g:

# myproject/apps/app2/callback_funcs/sub_functions.py

from apps.app1.models import SharedDataModel

Unfortunately, as soon as I add the import statement, I get the error django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

I tried solutions given here, here, and here, yet the problem persists.

All apps are in my INSTALLED_APPS list and being read correctly (again, as long as I exclude the problematic import statement above). I thought the order of the apps in the INSTALLED_APPS list might be an issue (I've seen instructions elsewhere that state certain apps need to be first) but this doesn't seem to affect the results.

I have:

INSTALLED_APPS = [
...
'apps.app1',
'apps.app2.app',
...
]

app1 is a standard Django app, app2 is a Django Plotly Dash app and has an app file which contains:

# myproject/apps/app2/app.py
from django_plotly_dash import DjangoDash
from .layout import layout
from .callbacks import register_callbacks

app = DjangoDash("app2")

app.layout = layout
register_callbacks(app)

Interestingly, I imported the model into another, non-Django Plotly Dash app, and it didn't give me this error. Perhaps the issue is stemming from this library?

I'm running Python 3.9.16, Django 3.2.16, and Django Plotly Dash 2.1.3.

Full traceback is here:

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/home/ubuntu/miniconda3/envs/myproject/lib/python3.9/threading.py", line 980, in _bootstrap_inner
    self.run()
  File "/home/ubuntu/miniconda3/envs/myproject/lib/python3.9/threading.py", line 917, in run
    self._target(*self._args, **self._kwargs)
  File "/home/ubuntu/miniconda3/envs/myproject/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/home/ubuntu/miniconda3/envs/myproject/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 110, in inner_run
    autoreload.raise_last_exception()
  File "/home/ubuntu/miniconda3/envs/myproject/lib/python3.9/site-packages/django/utils/autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
  File "/home/ubuntu/miniconda3/envs/myproject/lib/python3.9/site-packages/django/core/management/__init__.py", line 375, in execute
    autoreload.check_errors(django.setup)()
  File "/home/ubuntu/miniconda3/envs/myproject/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/home/ubuntu/miniconda3/envs/myproject/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/ubuntu/miniconda3/envs/myproject/lib/python3.9/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "/home/ubuntu/miniconda3/envs/myproject/lib/python3.9/site-packages/django/apps/config.py", line 224, in create
    import_module(entry)
  File "/home/ubuntu/miniconda3/envs/myproject/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 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/home/ubuntu/GitHub/myproject/apps/app2/sub_module.py", line 3, in <module>
    from .callbacks import register_callbacks
  File "/home/ubuntu/GitHub/myproject/apps/app2/callbacks.py", line 11, in <module>
    from .callback_funcs import sub_funcs
  File "/home/ubuntu/GitHub/myproject/apps/app2/callback_funcs/sub_funcs.py", line 5, in <module>
    from apps.app1.models import SharedDataModel
  File "/home/ubuntu/GitHub/myproject/apps/app1/models.py", line 8, in <module>
    class SharedDataModel(models.Model):
  File "/home/ubuntu/miniconda3/envs/myproject/lib/python3.9/site-packages/django/db/models/base.py", line 108, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/home/ubuntu/miniconda3/envs/myproject/lib/python3.9/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
    self.check_apps_ready()
  File "/home/ubuntu/miniconda3/envs/myproject/lib/python3.9/site-packages/django/apps/registry.py", line 136, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
hubbs5
  • 1,235
  • 1
  • 12
  • 22
  • can you please add project directory structure, I think this is happening cause of __init__.py file – Tanveer Ahmad Mar 15 '23 at 07:28
  • 2
    this question is already has been asked and has answer too https://stackoverflow.com/questions/34114427/django-upgrading-to-1-9-error-appregistrynotready-apps-arent-loaded-yet – Muhammad Kashif Mar 15 '23 at 07:54

0 Answers0