diff --git a/django/db/utils.py b/django/db/utils.py index dfdeff9c51..1bf44cbe26 100644 --- a/django/db/utils.py +++ b/django/db/utils.py @@ -111,23 +111,19 @@ def load_backend(backend_name): return import_module('%s.base' % backend_name) except ImportError as e_user: # The database backend wasn't found. Display a helpful error message - # listing all possible (built-in) database backends. + # listing all built-in database backends. backend_dir = os.path.join(os.path.dirname(__file__), 'backends') - try: - builtin_backends = [ - name for _, name, ispkg in pkgutil.iter_modules([backend_dir]) - if ispkg and name not in {'base', 'dummy', 'postgresql_psycopg2'} - ] - except EnvironmentError: - builtin_backends = [] - if backend_name not in ['django.db.backends.%s' % b for b in - builtin_backends]: + builtin_backends = [ + name for _, name, ispkg in pkgutil.iter_modules([backend_dir]) + if ispkg and name not in {'base', 'dummy', 'postgresql_psycopg2'} + ] + if backend_name not in ['django.db.backends.%s' % b for b in builtin_backends]: backend_reprs = map(repr, sorted(builtin_backends)) - error_msg = ("%r isn't an available database backend.\n" - "Try using 'django.db.backends.XXX', where XXX " - "is one of:\n %s\nError was: %s" % - (backend_name, ", ".join(backend_reprs), e_user)) - raise ImproperlyConfigured(error_msg) + raise ImproperlyConfigured( + "%r isn't an available database backend.\n" + "Try using 'django.db.backends.XXX', where XXX is one of:\n" + " %s" % (backend_name, ", ".join(backend_reprs)) + ) from e_user else: # If there's some other error, this must be an error in Django raise diff --git a/tests/backends/test_utils.py b/tests/backends/test_utils.py index d158e2a5a2..165820edae 100644 --- a/tests/backends/test_utils.py +++ b/tests/backends/test_utils.py @@ -8,8 +8,8 @@ class TestLoadBackend(SimpleTestCase): msg = ( "'foo' isn't an available database backend.\n" "Try using 'django.db.backends.XXX', where XXX is one of:\n" - " 'mysql', 'oracle', 'postgresql', 'sqlite3'\n" - "Error was: No module named 'foo'" + " 'mysql', 'oracle', 'postgresql', 'sqlite3'" ) - with self.assertRaisesMessage(ImproperlyConfigured, msg): + with self.assertRaisesMessage(ImproperlyConfigured, msg) as cm: load_backend('foo') + self.assertEqual(str(cm.exception.__cause__), "No module named 'foo'")