我正在尝试在Django项目中导入GDAL库,虽然我的应用程序在本地成功加载,并且在部署到Heroku时成功构建,但是在Web服务器启动后崩溃。
使用buildpack https://github.com/TrailStash/heroku-geo-buildpack
heroku[web.1]: Starting process with command `gunicorn feed.wsgi`
app[web.1]: [2018-07-15 13:24:39 +0000] [4] [INFO] Starting gunicorn 19.9.0
app[web.1]: [2018-07-15 13:24:39 +0000] [4] [INFO] Listening at: http://0.0.0.0:55791 (4)
app[web.1]: [2018-07-15 13:24:39 +0000] [4] [INFO] Using worker: sync
app[web.1]: [2018-07-15 13:24:39 +0000] [8] [INFO] Booting worker with pid: 8
app[web.1]: [2018-07-15 13:24:39 +0000] [9] [INFO] Booting worker with pid: 9
app[web.1]: [2018-07-15 13:24:40 +0000] [9] [ERROR] Exception in worker process
app[web.1]: Traceback (most recent call last):
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
app[web.1]: worker.init_process()
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 129, in init_process
app[web.1]: self.load_wsgi()
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
app[web.1]: self.wsgi = self.app.wsgi()
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
app[web.1]: self.callable = self.load()
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
app[web.1]: return self.load_wsgiapp()
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
app[web.1]: return util.import_app(self.app_uri)
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 350, in import_app
app[web.1]: __import__(module)
app[web.1]: File "/app/feed/wsgi.py", line 16, in <module>
app[web.1]: application = get_wsgi_application()
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
app[web.1]: django.setup(set_prefix=False)
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
app[web.1]: apps.populate(settings.INSTALLED_APPS)
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
app[web.1]: app_config.import_models()
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
app[web.1]: self.models_module = import_module(models_module_name)
app[web.1]: File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
app[web.1]: File "/app/core/models.py", line 4, in <module>
app[web.1]: from django.contrib.gis.db import models
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/db/models/__init__.py", line 3, in <module>
app[web.1]: import django.contrib.gis.db.models.functions # NOQA
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/db/models/functions.py", line 3, in <module>
app[web.1]: from django.contrib.gis.db.models.fields import BaseSpatialField, GeometryField
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/db/models/fields.py", line 3, in <module>
app[web.1]: from django.contrib.gis import forms, gdal
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/forms/__init__.py", line 3, in <module>
app[web.1]: from .fields import ( # NOQA
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/forms/fields.py", line 2, in <module>
app[web.1]: from django.contrib.gis.geos import GEOSException, GEOSGeometry
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/geos/__init__.py", line 5, in <module>
app[web.1]: from .collections import ( # NOQA
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/geos/collections.py", line 9, in <module>
app[web.1]: from django.contrib.gis.geos.geometry import GEOSGeometry, LinearGeometryMixin
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/geos/geometry.py", line 8, in <module>
app[web.1]: from django.contrib.gis import gdal
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/gdal/__init__.py", line 28, in <module>
app[web.1]: from django.contrib.gis.gdal.datasource import DataSource
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/gdal/datasource.py", line 39, in <module>
app[web.1]: from django.contrib.gis.gdal.driver import Driver
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/gdal/driver.py", line 5, in <module>
app[web.1]: from django.contrib.gis.gdal.prototypes import ds as vcapi, raster as rcapi
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/gdal/prototypes/ds.py", line 9, in <module>
app[web.1]: from django.contrib.gis.gdal.libgdal import GDAL_VERSION, lgdal
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/gdal/libgdal.py", line 47, in <module>
app[web.1]: lgdal = CDLL(lib_path)
app[web.1]: File "/app/.heroku/python/lib/python3.6/ctypes/__init__.py", line 348, in __init__
app[web.1]: self._handle = _dlopen(self._name, mode)
app[web.1]: OSError: $HOME/.heroku/vendor/lib/libgdal.so: cannot open shared object file: No such file or directory
app[web.1]: [2018-07-15 13:24:40 +0000] [9] [INFO] Worker exiting (pid: 9)
heroku[web.1]: State changed from starting to up
随后是Gunicorn暂停服务器异常
我发现了这个buildpack的一个分支,其中APP_VENDOR_RUNTIME
被修改以纠正一个错字(从APP_VENDOR_RUNTIME="$\HOME/$TARGET_VENDOR_DIR"
到$HOME
),这并不能解决问题。然后,我自己将其分叉,并将$HOME
更改为/app/
,但无济于事。奇怪的是,它给出了相同的错误(因为它仍然引用$HOME/.heroku/vendor/lib/libgdal.so
)。
libgdal.so已确认存在,已使用chmod
授予所有用户无效的读取访问权限。
将库硬编码为字符串(而不是环境变量)确实有所不同,构建失败,而不是运行:
-----> geos/gdal/proj app detected
Using geos version: 3.6.1
Using gdal version: 2.1.3
Using proj version: 4.9.3
export GEOS_LIBRARY_PATH=/tmp/build_4a3351bf23a32eaf56d29213aa25e2b5/.heroku/vendor/lib/libgeos_c.so
export GDAL_LIBRARY_PATH=/tmp/build_4a3351bf23a32eaf56d29213aa25e2b5/.heroku/vendor/lib/libgdal.so
export PROJ4_LIBRARY_PATH=/tmp/build_4a3351bf23a32eaf56d29213aa25e2b5/.heroku/vendor/lib
export GDAL_DATA=/tmp/build_4a3351bf23a32eaf56d29213aa25e2b5/.heroku/vendor/share/gdal
export LIBRARY_PATH=/tmp/build_4a3351bf23a32eaf56d29213aa25e2b5/.heroku/vendor/lib
export LD_LIBRARY_PATH=/tmp/build_4a3351bf23a32eaf56d29213aa25e2b5/.heroku/vendor/lib
export CPATH=/tmp/build_4a3351bf23a32eaf56d29213aa25e2b5/.heroku/vendor/include
export PATH=/tmp/build_4a3351bf23a32eaf56d29213aa25e2b5/.heroku/vendor/bin:$PATH
-----> Vendoring geo libraries done
-----> Python app detected
Skipping installation, as Pipfile.lock hasn't changed since last deploy.
-----> $ python manage.py collectstatic --noinput
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute
django.setup()
File "/app/.heroku/python/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
app_config.import_models()
File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/tmp/build_4a3351bf23a32eaf56d29213aa25e2b5/core/models.py", line 4, in <module>
from django.contrib.gis.db import models
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/db/models/__init__.py", line 3, in <module>
import django.contrib.gis.db.models.functions # NOQA
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/db/models/functions.py", line 3, in <module>
from django.contrib.gis.db.models.fields import BaseSpatialField, GeometryField
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/db/models/fields.py", line 3, in <module>
from django.contrib.gis import forms, gdal
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/forms/__init__.py", line 3, in <module>
from .fields import ( # NOQA
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/forms/fields.py", line 2, in <module>
from django.contrib.gis.geos import GEOSException, GEOSGeometry
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/geos/__init__.py", line 5, in <module>
from .collections import ( # NOQA
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/geos/collections.py", line 9, in <module>
from django.contrib.gis.geos.geometry import GEOSGeometry, LinearGeometryMixin
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/geos/geometry.py", line 8, in <module>
from django.contrib.gis import gdal
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/gdal/__init__.py", line 28, in <module>
from django.contrib.gis.gdal.datasource import DataSource
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/gdal/datasource.py", line 39, in <module>
from django.contrib.gis.gdal.driver import Driver
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/gdal/driver.py", line 5, in <module>
from django.contrib.gis.gdal.prototypes import ds as vcapi, raster as rcapi
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/gdal/prototypes/ds.py", line 9, in <module>
from django.contrib.gis.gdal.libgdal import GDAL_VERSION, lgdal
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/gdal/libgdal.py", line 72, in <module>
_version_info = std_call('GDALVersionInfo')
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/gdal/libgdal.py", line 66, in std_call
return lgdal[func]
File "/app/.heroku/python/lib/python3.6/ctypes/__init__.py", line 366, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: /app/.heroku/vendor/lib/libgeos_c.so: undefined symbol: GDALVersionInfo
! Error while running '$ python manage.py collectstatic --noinput'.
See traceback above for details.
You may need to update application code to resolve this error.
Or, you can disable collectstatic for this application:
$ heroku config:set DISABLE_COLLECTSTATIC=1
https://devcenter.heroku.com/articles/django-assets
! Push rejected, failed to compile Python app.
! Push failed
环境变量设置如下:
GDAL_LIBRARY_PATH - /app/.heroku/vendor/lib/libgdal.so
GEOS_LIBRARY_PATH - /app/.heroku/vendor/lib/libgeos_c.so
PROJ4_LIBRARY_PATH - /app/.heroku/vendor/lib/libproj.so
有什么想法吗?