diff --git a/CHANGES.rst b/CHANGES.rst index a50d32e..be22eed 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,13 @@ CHANGES 4.2.0 (Unreleased) ------------------ +General: + +* #149 Added a clear message on ImportError when the psycopg2 + package cannot be found. + Please refer to the following site for more information: + https://django-redshift-backend.readthedocs.io/en/master/basic.html#installation + Features: * #143 Add Django-5.0 support. diff --git a/django_redshift_backend/base.py b/django_redshift_backend/base.py index ab158a0..21e5ac5 100644 --- a/django_redshift_backend/base.py +++ b/django_redshift_backend/base.py @@ -15,11 +15,18 @@ import django from django.utils import timezone from django.conf import settings -from django.core.exceptions import FieldDoesNotExist +from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured from django.db.models import Index from django.db.models.expressions import Col from django.db.utils import NotSupportedError, ProgrammingError +try: + from psycopg2.extensions import Binary +except ImportError: + raise ImproperlyConfigured( + "Error loading psycopg2 module. Please install as: `pip install django-redshift-backend[psycopg2]` or `pip install django-redshift-backend[psycopg2-binary]`. For more information, see https://django-redshift-backend.readthedocs.io/en/master/basic.html#installation" + ) + from ._vendor.django40.db.backends.base.introspection import FieldInfo, TableInfo from ._vendor.django40.db.backends.base.schema import ( _is_relevant_relation, @@ -37,8 +44,6 @@ DatabaseIntrospection as BasePGDatabaseIntrospection, ) from .meta import DistKey, SortKey -from psycopg2.extensions import Binary - from .psycopg2adapter import RedshiftBinary logger = logging.getLogger("django.db.backends") diff --git a/tox.ini b/tox.ini index 338e3db..8ae6529 100644 --- a/tox.ini +++ b/tox.ini @@ -28,13 +28,12 @@ DJANGO = [testenv] deps = + .[psycopg2-binary] coverage - psycopg2-binary>=2.7 pytest pytest-cov mock>=2.0 django-environ - py38: backports.zoneinfo dj32: Django>=3.2,<3.3 dj40: Django>=4.0,<4.1 dj42: Django>=4.2,<5.0