1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fixed #32915 - Don't catch ImportErrors unless it's the settings module that can't be imported

Based on work by Rohith PR and others in PR #14622
This commit is contained in:
Ben Cail
2022-11-01 14:55:40 -04:00
parent f05edb2b43
commit f992997fbe
4 changed files with 26 additions and 4 deletions

View File

@@ -163,7 +163,13 @@ class Settings:
# store the settings module in case someone later cares
self.SETTINGS_MODULE = settings_module
mod = importlib.import_module(self.SETTINGS_MODULE)
try:
mod = importlib.import_module(self.SETTINGS_MODULE)
except ImportError as exc:
# If the settings module cannot be imported, treat it as a configuration error.
if exc.name == self.SETTINGS_MODULE:
raise ImproperlyConfigured(f"Settings module {self.SETTINGS_MODULE} could not be imported") from exc
raise
tuple_settings = (
"ALLOWED_HOSTS",

View File

@@ -382,7 +382,9 @@ class ManagementUtility:
settings.INSTALLED_APPS
except ImproperlyConfigured as exc:
self.settings_exception = exc
except ImportError as exc:
# The following commands can be run even without a valid settings file configured
if subcommand not in {'startproject', 'startapp', 'makemessages'}:
sys.stderr.write(str(exc) + '\n')
self.settings_exception = exc
if settings.configured: