heroku / heroku-buildpack-python

Heroku's buildpack for Python applications.
https://www.heroku.com/python
MIT License
974 stars 1.84k forks source link

cannot import name 'GDALRaster' #439

Closed leonL closed 6 years ago

leonL commented 7 years ago

Hi. I'm having trouble configuring a heroku-16 instance to support GeoDjango. I've followed the documentation on the Heroku site closely, but always encounter the error:

django.core.exceptions.ImproperlyConfigured: 'django.contrib.gis.db.backends.postgis' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
    'mysql', 'oracle', 'postgresql', 'sqlite3'
Error was: cannot import name 'GDALRaster'

Could this be related to any of the recent issues/changes related to this repo having to do with GIS dependencies like GDAL and GEOS. Thanks for any ideas.

kennethreitz commented 7 years ago

Hmm, this support is a bit janky. Let me try to reproduce. Can you share your settings file, @leonL?

kennethreitz commented 7 years ago

This is working well for me!

kennethreitz commented 7 years ago

Did you $ heroku config:set BUILD_WITH_GEO_LIBRARIES=1?

leonL commented 7 years ago

Hi @kennethreitz, thanks for your help.

I did set BUILD_WITH_GEO_LIBRARIES=1, yes but I have a feeling it's not working: The echo statements displayed when I push my repo to heroku don't show any evidence of the related dependencies being installed and when I try gdal.HAS_GDAL from the python shell I get False.

My settings file looks like this...

import os
import dj_database_url

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = False
ALLOWED_HOSTS = [
  'sleepy-scrubland-24958.herokuapp.com',
  '0.0.0.0'
]
INSTALLED_APPS = [
    'api.apps.ApiConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'corsheaders',
    'rest_framework'
]
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'snm_server.urls'
CORS_ORIGIN_ALLOW_ALL = True
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
WSGI_APPLICATION = 'snm_server.wsgi.application'
DATABASES = {'default': {}}
DATABASES['default'] =  dj_database_url.config()
DATABASES['default']['ENGINE'] = 'django.contrib.gis.db.backends.postgis'
AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

STATIC_URL = '/static/'
kennethreitz commented 7 years ago

Are you on heroku-16 stack?

$ heroku stack

leonL commented 7 years ago

I am, yes.

leonL commented 7 years ago

@kennethreitz, FYI I just performed the following test, which perhaps isolates the problem...

  1. I created a new heroku app with 'heroku create' (polar-wave-72434 on stack-16)
  2. I pushed https://github.com/heroku/python-getting-started.git to it and made sure it was running
  3. I ran 'heroku config:set BUILD_WITH_GEO_LIBRARIES=1?' and performed another push (one of the log statements returned from the push was 'Bootstrapping gdal, geos, proj')
  4. I ran the python shell on the build and ran gdal.HAS_GDAL which return False
leonL commented 7 years ago

@kennethreitz, could you please post the process you are taking to test that the dependencies are installing correctly on the Heroku build? I'll try reproducing it, and perhaps it will give me some insight into what I need to change on my builds to make it work.

Thanks, Leon

leonL commented 7 years ago

@kennethreitz, I changed my stack to cedar-14 and it solved all my problems. I'm gonna stay on that stack for the time being. If there's anything you'd like me to try out on the heroku-16 stack I'm happy to help.

Best, L.

zachmccormick commented 7 years ago

Hey y'all! I think I solved this with https://github.com/heroku/heroku-buildpack-python/pull/449 - if you want to specify my branch with these changes, you can use https://github.com/TrailblazingTech/heroku-buildpack-python#fix-gdal-jasper.

You will need to add two lines to your settings.py as I detailed in the PR as well, so that Django can load gdal and geos.

kennethreitz commented 7 years ago

@zachmccormick really looking forward to trying out your changes this week!

kennethreitz commented 6 years ago

Heroku-16 and GEOS do not play well together. I'm going to update the documentation today to reflect this.

kennethreitz commented 6 years ago

The release of Django 2.0 may solve this.

danielskun commented 6 years ago

I am experiencing the same problem with Django 2.0.6, Heroku-16 and BUILD_WITH_GEO_LIBRARIES=1

When building, one of the log statements returned from the push was 'Bootstrapping gdal, geos, proj'

When testing GDAL:

from django.contrib.gis.gdal import HAS_GDAL
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: cannot import name 'HAS_GDAL'
zachmccormick commented 6 years ago

Hey @danielskun ! Check out this commit that is included in 2.0a1 onward: https://github.com/django/django/commit/2dc3280254ae06ca1fe664abf55749fe12a699d4#diff-6d623ebf16b95ff941111fa162d02236

It removes HAS_GDAL and actually seems to do a decent bit of reorganizing the code around absolutely requiring GDAL (as of Django 1.11, if you use contrib.gis, it's required). I would fork this buildpack and try to figure it out using that commit as a starting point. If you can't get it to work, ping me and I'll give it a shot this weekend.