Opened 7 years ago

Closed 7 years ago

#28791 closed Cleanup/optimization (fixed)

ManagementUtility.execute() does not handle ImportError for invalid settings module

Reported by: Marten Kenbeek Owned by: Hrishikesh Barman
Component: Core (Management commands) Version: dev
Severity: Normal Keywords:
Cc: Hrishikesh Barman, Adam Johnson Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: yes UI/UX: no

Description

In 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 can't be found or the command uses the settings), the ImportError will be reraised.

Change History (9)

comment:1 by Hrishikesh Barman, 7 years ago

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?

comment:2 by Hrishikesh Barman, 7 years ago

Cc: Hrishikesh Barman added

comment:3 by Tim Graham, 7 years ago

Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

comment:4 by Adam Johnson, 7 years ago

Cc: Adam Johnson added

comment:5 by Hrishikesh Barman, 7 years ago

Owner: changed from nobody to Hrishikesh Barman
Status: newassigned

comment:6 by Hrishikesh Barman, 7 years ago

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.

comment:7 by Tim Graham, 7 years ago

Has patch: set
Needs tests: set
Patch needs improvement: set

comment:8 by Hrishikesh Barman, 7 years ago

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

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

Resolution: fixed
Status: assignedclosed

In c6864a01:

Fixed #28791 -- Allowed commands that don't require settings to work if the DJANGO_SETTINGS_MODULE doesn't exist.

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