diff --git a/django/contrib/gis/tests/tests.py b/django/contrib/gis/tests/tests.py index 9a4c2bcbdd..cc52d2429b 100644 --- a/django/contrib/gis/tests/tests.py +++ b/django/contrib/gis/tests/tests.py @@ -1,13 +1,25 @@ +import sys import unittest from django.core.exceptions import ImproperlyConfigured from django.db import ProgrammingError +from django.utils import six try: from django.contrib.gis.db.backends.postgis.operations import PostGISOperations HAS_POSTGRES = True except ImportError: HAS_POSTGRES = False +except ImproperlyConfigured as e: + # If psycopg is installed but not geos, the import path hits + # django.contrib.gis.geometry.backend which will "helpfully" convert + # an ImportError into an ImproperlyConfigured. + # Here, we make sure we're only catching this specific case and not another + # ImproperlyConfigured one. + if e.args and e.args[0].startswith('Could not import user-defined GEOMETRY_BACKEND'): + HAS_POSTGRES = False + else: + six.reraise(*sys.exc_info()) if HAS_POSTGRES: