Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#30111 closed Bug (fixed)

AppRegistryNotReady-Error when having contrib.postgres in INSTALLED_APPS

Reported by: Jann Haber Owned by: Nasir Hussain
Component: contrib.postgres Version: 2.2
Severity: Release blocker Keywords:
Cc: jon.dufresne@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Jann Haber)

After an upgrade to Django 2.2a1, I found the following problem:

$ python manage.py help
/home/test/.virtualenvs/testenv/lib/python3.7/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
  """)
Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/test/.virtualenvs/testenv/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/test/.virtualenvs/testenv/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
    django.setup()
  File "/home/test/.virtualenvs/testenv/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/test/.virtualenvs/testenv/lib/python3.7/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "/home/test/.virtualenvs/testenv/lib/python3.7/site-packages/django/apps/config.py", line 116, in create
    mod = import_module(mod_path)
  File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/test/.virtualenvs/testenv/lib/python3.7/site-packages/django/contrib/postgres/apps.py", line 8, in <module>
    from django.db.migrations.writer import MigrationWriter
  File "/home/test/.virtualenvs/testenv/lib/python3.7/site-packages/django/db/migrations/writer.py", line 10, in <module>
    from django.db.migrations.loader import MigrationLoader
  File "/home/test/.virtualenvs/testenv/lib/python3.7/site-packages/django/db/migrations/loader.py", line 8, in <module>
    from django.db.migrations.recorder import MigrationRecorder
  File "/home/test/.virtualenvs/testenv/lib/python3.7/site-packages/django/db/migrations/recorder.py", line 9, in <module>
    class MigrationRecorder:
  File "/home/test/.virtualenvs/testenv/lib/python3.7/site-packages/django/db/migrations/recorder.py", line 22, in MigrationRecorder
    class Migration(models.Model):
  File "/home/test/.virtualenvs/testenv/lib/python3.7/site-packages/django/db/models/base.py", line 99, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/home/test/.virtualenvs/testenv/lib/python3.7/site-packages/django/apps/registry.py", line 252, in get_containing_app_config
    self.check_apps_ready()
  File "/home/test/.virtualenvs/testenv/lib/python3.7/site-packages/django/apps/registry.py", line 135, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

To reproduce (running python 3.7.2):
Make a new python3 venv (python3 -m venv) and activate. Install django 2.2a1 and psycopg2 using pip. Use django-admin.py startproject to get a fresh installation of django. Add contrib.postgres to INSTALLED_APPS. Run the command above, which should display the help messages. The same thing happens also with any other manage.py command.

Note: The same thing is working with no problem when running Django 2.1.5

Change History (11)

comment:1 by Jann Haber, 6 years ago

Description: modified (diff)

in reply to:  description comment:2 by Nasir Hussain, 6 years ago

Replying to Jann Haber:
Hi Jann,

So I looked into the code and found the issue.

When contrib.postgres is in INSTALLED_APPS it triggers from django.db.migrations.writer import MigrationWriter import in django/django/contrib/postgres/apps.py which further triggered app_config = apps.get_containing_app_config(module) import in ModelBase Meta class.
ModelBase checks for self.check_apps_ready() while apps have not been completely loaded yet. So an exception is raised raise AppRegistryNotReady("Apps aren't loaded yet.")
Fix: I've changed django/django/contrib/postgres/apps.py to import MigrationWriter only when needed to avoid check for apps ready cycle.

PR

Last edited 6 years ago by Nasir Hussain (previous) (diff)

comment:3 by Nasir Hussain, 6 years ago

Owner: set to Nasir Hussain
Status: newassigned

comment:4 by Nasir Hussain, 6 years ago

Has patch: set

comment:5 by Nick Pope, 6 years ago

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted
Version 0, edited 6 years ago by Nick Pope (next)

comment:6 by Jon Dufresne, 6 years ago

Cc: jon.dufresne@… added

comment:7 by Carlton Gibson, 6 years ago

Triage Stage: AcceptedReady for checkin

comment:8 by Tim Graham <timograham@…>, 6 years ago

Resolution: fixed
Status: assignedclosed

In 2804b8d2:

Fixed #30111 -- Fixed AppRegistryNotReady error with django.contrib.postgres in INSTALLED_APPS.

Regression in e192223ed996ed30fe83787efdfa7f2be6b1a2ee.

comment:9 by Tim Graham <timograham@…>, 6 years ago

In 6ce7887f:

[2.2.x] Fixed #30111 -- Fixed AppRegistryNotReady error with django.contrib.postgres in INSTALLED_APPS.

Regression in e192223ed996ed30fe83787efdfa7f2be6b1a2ee.
Backport of 2804b8d2153505ec49b191db2168302dfb92c3af from master.

comment:10 by Tim Graham <timograham@…>, 6 years ago

In 2de7eb6f:

Refs #30111 -- Fixed test cleanup in postgres_tests/test_integration.py.

Fixed "ERROR: Step ‘Publish JUnit test result report’ failed: No test report
files were found. Configuration error?" on Jenkins because report files were
put in tests/postgres_tests.

comment:11 by Tim Graham <timograham@…>, 6 years ago

In 710052f:

[2.2.x] Refs #30111 -- Fixed test cleanup in postgres_tests/test_integration.py.

Fixed "ERROR: Step ‘Publish JUnit test result report’ failed: No test report
files were found. Configuration error?" on Jenkins because report files were
put in tests/postgres_tests.

Backport of 2de7eb6f4d5a20cec98e4d2eefc276ee38d149ad from master.

Note: See TracTickets for help on using tickets.
Back to Top