Opened 5 weeks ago

Closed 6 days 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: master
Severity: Normal Keywords:
Cc: Hrishikesh Barman, Adam (Chainz) 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 Changed 5 weeks ago 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?

comment:2 Changed 5 weeks ago by Hrishikesh Barman

Cc: Hrishikesh Barman added

comment:3 Changed 4 weeks ago by Tim Graham

Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

comment:4 Changed 4 weeks ago by Adam (Chainz) Johnson

Cc: Adam (Chainz) Johnson added

comment:5 Changed 4 weeks ago by Hrishikesh Barman

Owner: changed from nobody to Hrishikesh Barman
Status: newassigned

comment:6 Changed 3 weeks ago 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.

comment:7 Changed 3 weeks ago by Tim Graham

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

comment:8 Changed 13 days ago by Hrishikesh Barman

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

comment:9 Changed 6 days ago by Tim Graham <timograham@…>

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