Opened 5 years ago
Last modified 8 days ago
#32915 assigned Bug
Exceptions from settings modules are hidden when running management commands
| Reported by: | Michael | Owned by: | bcail |
|---|---|---|---|
| Component: | Core (Management commands) | Version: | 3.2 |
| Severity: | Normal | Keywords: | |
| Cc: | bcail, Ülgen Sarıkavak, Lily Acorn, Brian Helba | Triage Stage: | Accepted |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Maybe its regression, related to: https://code.djangoproject.com/ticket/27522:
> python manage.py runserver --nostatic
usage: manage.py runserver [-h] [--ipv6] [--nothreading] [--noreload] [--version] [-v {0,1,2,3}] [--settings SETTINGS]
[--pythonpath PYTHONPATH] [--traceback] [--no-color] [--force-color]
[addrport]
manage.py runserver: error: unrecognized arguments: --nostatic
> python manage.py runserver
python manage.py runserver
Traceback (most recent call last):
File "/home/michael/venv/project/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/michael/venv/project/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 61, in execute
super().execute(*args, **options)
File "/home/michael/venv/project/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
output = self.handle(*args, **options)
File "/home/michael/venv/project/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 68, in handle
if not settings.DEBUG and not settings.ALLOWED_HOSTS:
File "/home/michael/venv/project/lib/python3.8/site-packages/django/conf/__init__.py", line 82, in __getattr__
self._setup(name)
File "/home/michael/venv/project/lib/python3.8/site-packages/django/conf/__init__.py", line 69, in _setup
self._wrapped = Settings(settings_module)
File "/home/michael/venv/project/lib/python3.8/site-packages/django/conf/__init__.py", line 170, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'dist.plug.settings'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 16, in <module>
execute_from_command_line(sys.argv)
File "/home/michael/venv/project/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/home/michael/venv/project/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/michael/venv/project/lib/python3.8/site-packages/django/core/management/base.py", line 367, in run_from_argv
connections.close_all()
File "/home/michael/venv/project/lib/python3.8/site-packages/django/db/utils.py", line 208, in close_all
for alias in self:
File "/home/michael/venv/project/lib/python3.8/site-packages/django/utils/connection.py", line 73, in __iter__
return iter(self.settings)
File "/home/michael/venv/project/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/michael/venv/project/lib/python3.8/site-packages/django/utils/connection.py", line 45, in settings
self._settings = self.configure_settings(self._settings)
File "/home/michael/venv/project/lib/python3.8/site-packages/django/db/utils.py", line 144, in configure_settings
databases = super().configure_settings(databases)
File "/home/michael/venv/project/lib/python3.8/site-packages/django/utils/connection.py", line 50, in configure_settings
settings = getattr(django_settings, self.settings_name)
File "/home/michael/venv/project/lib/python3.8/site-packages/django/conf/__init__.py", line 82, in __getattr__
self._setup(name)
File "/home/michael/venv/project/lib/python3.8/site-packages/django/conf/__init__.py", line 69, in _setup
self._wrapped = Settings(settings_module)
File "/home/michael/venv/project/lib/python3.8/site-packages/django/conf/__init__.py", line 170, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'dist.plug.settings'
Its a ligit error, just hidden by --nostatic
Change History (29)
comment:1 by , 5 years ago
| Description: | modified (diff) |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
comment:2 by , 5 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:4 by , 5 years ago
| Patch needs improvement: | set |
|---|
comment:5 by , 5 years ago
| Patch needs improvement: | unset |
|---|
comment:6 by , 5 years ago
| Patch needs improvement: | set |
|---|
comment:7 by , 5 years ago
| Patch needs improvement: | unset |
|---|
comment:9 by , 4 years ago
| Patch needs improvement: | unset |
|---|
comment:10 by , 4 years ago
| Patch needs improvement: | set |
|---|
comment:11 by , 4 years ago
| Owner: | removed |
|---|---|
| Status: | assigned → new |
comment:12 by , 3 years ago
| Cc: | added |
|---|---|
| Needs tests: | unset |
| Owner: | set to |
| Patch needs improvement: | unset |
| Status: | new → assigned |
Opened a new PR from the previous one: https://github.com/django/django/pull/16337
comment:13 by , 3 years ago
| Patch needs improvement: | set |
|---|
comment:14 by , 2 years ago
| Owner: | removed |
|---|---|
| Status: | assigned → new |
comment:15 by , 2 years ago
| Cc: | added |
|---|
comment:16 by , 23 months ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
The previous PR's focused on avoiding/working aroung the ImportError introduced by c6864a01b25591d3a709da8071413b69c9e35341. I believe this is treating the symptoms rather than the cause.
The runserver command is split in two: the "standard" command in core/management/commands/runserver.py and the special command in contrib/staticfiles/commands/runserver.py. The latter adds --nostatic and --insecure to the arguments. However, this module imports settings from django.conf. If settings doesn't exist or has errors, contrib/staticfiles/commands/runserver.py cannot be loaded either. As a result, the --nostatic argument is not recognized because it hasn't been added (the same is true of --insecure, although this hasn't been reported).
The problem can be solved by merging the two commands. The runserver command is simplified, and the code that introduced the ImportError can be left as is.
comment:17 by , 18 months ago
| Patch needs improvement: | unset |
|---|
I think this PR could be reviewed. Or, if the decision is to go with the other PR, I can close mine.
comment:18 by , 17 months ago
| Patch needs improvement: | set |
|---|
Had a look at both PRs and, from what I can tell, they both need some work
comment:19 by , 12 months ago
| Needs documentation: | set |
|---|---|
| Patch needs improvement: | unset |
The new attribute needs a release note and documentation.
comment:20 by , 11 months ago
#36422 was a dupe, which is worth looking at to ensure the fix for this ticket covers that use case as well. That means including introspection of ImproperlyConfigured to determine if it emanated from a settings module before deciding to swallow it.
comment:21 by , 11 months ago
| Cc: | added |
|---|
comment:23 by , 9 days ago
I believe that #36422 (closed as a duplicate of this one) is actually closer to the root cause, and this ticket is tracking a symptom.
The underlying problem is that when DJANGO_SETTINGS_MODULE fails to import, ManagementUtility.execute() stashes the exception on self.settings_exception and continues silently.
There is only one code path that surfaces it to the user: main_help_text(), reached by running ./manage.py with no arguments. Every other invocation exits without mentioning that settings failed to load, so end users have no way to diagnose the misconfiguration and remedy it.
The user-facing problem is more significant than just the missing traceback on runserver. Because settings.configured stays False, get_commands() returns only the django.core commands and every other command defined by an app in INSTALLED_APPS silently disappears. The user runs ./manage.py actual_custom_command, gets "Unknown command: 'actual_custom_command'", and is told nothing about why. Since we haven't shown the user the real error with their settings, they're getting mislead about the simplest way to fix the issue.
This ticket is focused on an instance of that same user problem: after a settings failure, django.contrib.staticfiles is silently not added to INSTALLED_APPS, so the --nostatic option disappears (since runserver is never overridden). The best solution isn't to patch --nostatic into the core command, it's to confront the user with the settings failure; once they fix that, INSTALLED_APPS and custom commands will work again.
comment:24 by , 9 days ago
| Summary: | ./manage runserver --nostatic still doesn't return a traceback → Exceptions from settings modules are hidden when running management commands |
|---|
Broadened issue title to reflect scope.
comment:25 by , 9 days ago
| Cc: | added |
|---|
comment:26 by , 9 days ago
| Owner: | changed from to |
|---|
comment:27 by , 9 days ago
I've made https://github.com/django/django/pull/21154 , which I hope resolves the root issue. I'm happy to respond to feedback over on the PR.
comment:28 by , 9 days ago
| Needs documentation: | unset |
|---|
Thanks for the report!
Bisected to c6864a01b25591d3a709da8071413b69c9e35341.