[Django] #36422: Settings exception hidden when running management command

14 views
Skip to first unread message

Django

unread,
May 28, 2025, 8:50:25 AM5/28/25
to django-...@googlegroups.com
#36422: Settings exception hidden when running management command
-------------------------------------+-------------------------------------
Reporter: Lily Acorn | Type:
| Uncategorized
Status: new | Component: Core
| (Management commands)
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
When running a management command, Django tries to load
`settings.INSTALLED_APPS`
(https://github.com/django/django/blob/ad28db666e0884b658f5e240c3d7ee1362688ba1/django/core/management/__init__.py#L381-L386).
If this fails with an `ImproperlyConfigured` or an `ImportError`
exception, Django saves this as `self.settings_exception`. This is used to
output help text
(https://github.com/django/django/blob/ad28db666e0884b658f5e240c3d7ee1362688ba1/django/core/management/__init__.py#L235-L242)
when the subcommand is `help` or the `--help` or `-h` flag is set.
Otherwise the exception is completely swallowed, which is really unhelpful
for identifying the cause of later failures.

In my case, I was getting a chain of `AppRegistryNotReady` exceptions from
the `migrate` subcommand, but the root cause of an `ImproperlyConfigured`
exception from my settings was deeply hidden, until I did some fairly deep
exploration with `pdb`.

A suggested fix is to add `self.settings_exception` to the exception chain
when a later exception is raised.
--
Ticket URL: <https://code.djangoproject.com/ticket/36422>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 28, 2025, 10:21:28 AM5/28/25
to django-...@googlegroups.com
#36422: Settings exception hidden when running management command
-------------------------------------+-------------------------------------
Reporter: Lily Acorn | Owner: (none)
Type: | Status: new
Cleanup/optimization |
Component: Core (Management | Version: 5.1
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* type: Uncategorized => Cleanup/optimization

Comment:

> If this fails with an ImproperlyConfigured or an ImportError exception
... the exception is completely swallowed.

The PR for ticket:32915 avoids catching `ImportError`, but makes no
changes to `ImproperlyConfigured`, so we may choose to treat this as a
dupe and leave feedback on ticket:32915 to treat both the same way
(introspect whether `ImproperlyConfigured` emanated from a settings module
before deciding to swallow it).
--
Ticket URL: <https://code.djangoproject.com/ticket/36422#comment:1>

Django

unread,
May 29, 2025, 9:54:48 AM5/29/25
to django-...@googlegroups.com
#36422: Settings exception hidden when running management command
-------------------------------------+-------------------------------------
Reporter: Lily Acorn | Owner: (none)
Type: Bug | Status: closed
Component: Core (Management | Version: dev
commands) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* resolution: => duplicate
* status: new => closed
* type: Cleanup/optimization => Bug
* version: 5.1 => dev

Comment:

Thank you Lily for your ticket report, and thank you Jacob for your
triage.

I agree this could be closed as a dupe of #32915. Other tangentially
related tickets could be #25510 and #25523.
--
Ticket URL: <https://code.djangoproject.com/ticket/36422#comment:2>
Reply all
Reply to author
Forward
0 new messages