[Django] #28791: ManagementUtility.execute() does not handle ImportError for invalid settings module

29 views
Skip to first unread message

Django

unread,
Nov 11, 2017, 4:51:48 PM11/11/17
to django-...@googlegroups.com
#28791: ManagementUtility.execute() does not handle ImportError for invalid
settings module
-------------------------------------+-------------------------------------
Reporter: Marten | Owner: nobody
Kenbeek |
Type: Bug | Status: new
Component: Core | Version: master
(Management commands) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
In
[https://github.com/django/django/blob/abaf0ab4a444977dc8ac07b9b63256814c352245/django/core/management/__init__.py#L316
ManagementUtility.execute()], the code tries to load the settings, and
catches `ImproperlyConfigured` in case the settings aren't configured.
However, if `DJANGO_SETTINGS_MODULE` is set to an invalid module (e.g.
from the previous project you were working on), an uncaught `ImportError`
is raised. This causes commands that don't need the settings, such as
`startproject`, to fail.

It seems to me that the `ImportError` can safely be caught so that core
commands that don't need settings can be executed. If the settings are
accessed later on (either because the command
[https://github.com/django/django/blob/abaf0ab4a444977dc8ac07b9b63256814c352245/django/core/management/__init__.py#L198
can't be found] or the command uses the settings), the `ImportError` will
be reraised.

--
Ticket URL: <https://code.djangoproject.com/ticket/28791>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 12, 2017, 2:20:40 AM11/12/17
to django-...@googlegroups.com
#28791: ManagementUtility.execute() does not handle ImportError for invalid
settings module
-------------------------------------+-------------------------------------
Reporter: Marten Kenbeek | Owner: nobody
Type: Bug | Status: new
Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Hrishikesh Barman):

Hi Marten,
This seems like a valid enhancement to me. I reproduced the bug and is
valid.
Can I start contributing to this?
I have a question, how do I test if my changes work? Do I have to install
the cloned and modified django everytime I make a modification?
Also, how to go about writing tests for this?

--
Ticket URL: <https://code.djangoproject.com/ticket/28791#comment:1>

Django

unread,
Nov 12, 2017, 2:21:28 AM11/12/17
to django-...@googlegroups.com
#28791: ManagementUtility.execute() does not handle ImportError for invalid
settings module
-------------------------------------+-------------------------------------
Reporter: Marten Kenbeek | Owner: nobody
Type: Bug | Status: new

Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hrishikesh Barman):

* cc: Hrishikesh Barman (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/28791#comment:2>

Django

unread,
Nov 14, 2017, 11:49:11 AM11/14/17
to django-...@googlegroups.com
#28791: ManagementUtility.execute() does not handle ImportError for invalid
settings module
-------------------------------------+-------------------------------------
Reporter: Marten Kenbeek | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* type: Bug => Cleanup/optimization
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/28791#comment:3>

Django

unread,
Nov 15, 2017, 5:56:24 AM11/15/17
to django-...@googlegroups.com
#28791: ManagementUtility.execute() does not handle ImportError for invalid
settings module
-------------------------------------+-------------------------------------
Reporter: Marten Kenbeek | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Adam (Chainz) Johnson):

* cc: Adam (Chainz) Johnson (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/28791#comment:4>

Django

unread,
Nov 19, 2017, 2:37:16 AM11/19/17
to django-...@googlegroups.com
#28791: ManagementUtility.execute() does not handle ImportError for invalid
settings module
-------------------------------------+-------------------------------------
Reporter: Marten Kenbeek | Owner:
Type: | Hrishikesh Barman
Cleanup/optimization | Status: assigned

Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hrishikesh Barman):

* owner: nobody => Hrishikesh Barman
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/28791#comment:5>

Django

unread,
Nov 20, 2017, 2:22:31 AM11/20/17
to django-...@googlegroups.com
#28791: ManagementUtility.execute() does not handle ImportError for invalid
settings module
-------------------------------------+-------------------------------------
Reporter: Marten Kenbeek | Owner:
Type: | Hrishikesh Barman
Cleanup/optimization | Status: assigned
Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Hrishikesh Barman):

I've sent a pull request for this ticket,
https://github.com/django/django/pull/9367

It's failing a flake8 test, I ran the tests locally.

Please let me know about any further changes.

--
Ticket URL: <https://code.djangoproject.com/ticket/28791#comment:6>

Django

unread,
Nov 20, 2017, 8:09:38 AM11/20/17
to django-...@googlegroups.com
#28791: ManagementUtility.execute() does not handle ImportError for invalid
settings module
-------------------------------------+-------------------------------------
Reporter: Marten Kenbeek | Owner:
Type: | Hrishikesh Barman
Cleanup/optimization | Status: assigned
Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* needs_better_patch: 0 => 1
* has_patch: 0 => 1
* needs_tests: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/28791#comment:7>

Django

unread,
Nov 30, 2017, 6:13:52 PM11/30/17
to django-...@googlegroups.com
#28791: ManagementUtility.execute() does not handle ImportError for invalid
settings module
-------------------------------------+-------------------------------------
Reporter: Marten Kenbeek | Owner:
Type: | Hrishikesh Barman
Cleanup/optimization | Status: assigned
Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Hrishikesh Barman):

The improved patch for this ticket is here:
https://github.com/django/django/pull/9367

--
Ticket URL: <https://code.djangoproject.com/ticket/28791#comment:8>

Django

unread,
Dec 7, 2017, 2:56:26 PM12/7/17
to django-...@googlegroups.com
#28791: ManagementUtility.execute() does not handle ImportError for invalid
settings module
-------------------------------------+-------------------------------------
Reporter: Marten Kenbeek | Owner:
Type: | Hrishikesh Barman
Cleanup/optimization | Status: closed

Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"c6864a01b25591d3a709da8071413b69c9e35341" c6864a01]:
{{{
#!CommitTicketReference repository=""
revision="c6864a01b25591d3a709da8071413b69c9e35341"
Fixed #28791 -- Allowed commands that don't require settings to work if
the DJANGO_SETTINGS_MODULE doesn't exist.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28791#comment:9>

Reply all
Reply to author
Forward
0 new messages