GeoDjango can't find geos library - django

When attempting to fire up the django server, I get the following error:
django.core.exceptions.ImproperlyConfigured: Could not import user-defined GEOMETRY_BACKEND "geos".
I'm running postgresql8.4 on Mac OS Lion.
I've used Macports to install PostGIS. Here's what $ port installed shows:
postgis #1.5.2_1+postgresql84
postgis #1.5.2_1+postgresql90
postgis #1.5.3_0+postgresql90
postgis #1.5.3_0+postgresql91 (active)
Here's what I get when attempting to determine what version of PostGIS I've got:
geodjango=# SELECT PostGIS_full_version();
ERROR: could not access file "$libdir/postgis-1.5": No such file or directory
CONTEXT: SQL statement "SELECT postgis_lib_version()"
PL/pgSQL function "postgis_full_version" line 11 at SQL statement

Just in case some developers are facing this problem on Ubuntu, I solved it just installing libgeos-lib
sudo apt-get install libgeos-dev

I had the same problem with postgis via MacPorts in combination with Django, although I could execute select postgis_full_version(); without issues. This answer helped me: http://pragmaticstartup.wordpress.com/2012/09/30/django-core-exceptions-improperlyconfigured-could-not-import-user-defined-geometry_backend-geos/
The solution for me was to add
export DYLD_FALLBACK_LIBRARY_PATH=$DYLD_FALLBACK_LIBRARY_PATH:/opt/local/lib/
to ~/.profile. (Don't forget to source it before trying again)

I found that I'd originally installed geos through pip.
Once I downloaded the new GEOS library from:
http://www.kyngchaos.com/software/frameworks#geos
Then installed it.
Last, I added to my path (~/.bash_profile):
export PATH=/Library/Frameworks/GEOS.framework/Versions/3/unix/lib/:$PATH
It worked fine.

Have you installed the geos library?
It's included in the instructions for installing GeoDjango with macports.

Related

What is wrong with the default installation of Qt6 on Ubuntu 22.04

I am trying to install Qt (this time 6.4), the online version, on Ubuntu 22.04.
The installation was apparently smooth; I used the default options.
However, when attempting to create a desktop project, I receive the message
/opt/Qt/Tools/CMake/share/cmake-3.23/Modules/CMakeFindDependencyMacro.cmake:47: warning: Found package configuration file: /opt/Qt/6.4.0/gcc_64/lib/cmake/Qt6Gui/Qt6GuiConfig.cmake but it set Qt6Gui_FOUND to FALSE so package "Qt6Gui" is considered to be NOT FOUND. Reason given by package: Qt6Gui could not be found because dependency WrapOpenGL could not be found.
Did I wrong something? How can I continue?
Install the OpenGL development packages like the following.
$ sudo apt install libglx-dev

Error loading psycopg2 module: Library not loaded: libpq.5.dylib

I am trying to run a Django project with Postgres database. I use Postgres 13.4 installed via postgressapp (UNIVERSAL with all currently supported versions) and python 3.9 (in venv). I work on Mac with Apple M1 chip, macOS Big Sur.
I faced the following well-known problem:
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: dlopen(/Users/mymac/PyCharmProjects/projectname/venv/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so, 2): Library not loaded: /opt/homebrew/opt/postgresql/lib/libpq.5.dylib
  Referenced from: /Users/mymac/PyCharmProjects/projectname/venv/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so
  Reason: image not found
With searching, I found some discussions like this: https://github.com/psycopg/psycopg2/issues/1216. It seems that the most relevant solution is "RyanDurk commented on Jan 27":
$ brew install libpq --build-from-source
$ export LDFLAGS="-L/opt/homebrew/opt/libpq/lib"
$ pip install psycopg2
Unfortunately, in my case it does not help.
Then, I found some recommendations here: Library not loaded: /usr/local/lib/libpq.5.4.dylib and tried them. In particular, I tried to reach libpq.5.dylib via symlink like:
ln -s  /Library/PostgreSQL/13/lib/libpq.5.dylib /opt/homebrew/opt/postgresql/lib/libpq.5.dylib (the solution marked as accepted by topic starter), but also unsuccessfully.
I tried to install postgres from postgresql.org, then uninstall/reinstall postgres with homebrew, then
gem uninstall pg -> bundle install
with the same result.
I have run the same project successfully before, on the mac with Intel chip and PyCharm community edition. Also the same project runs normally on Linux.
If you have any idea what happens and how to fix this problem, please help me. I provide more details if necessary.
I had the same error after brew decided to upgrade my postgresql package to version 14 (your version may vary).
There was this part of the error message:
Library not loaded: '/usr/local/opt/postgresql/lib/libpq.5.dylib'
...
Reason: tried: '/usr/local/opt/postgresql/lib/libpq.5.dylib' (no such file)
I basically found the new location of the libpq and symlinked it to the location where psycopg2 was looking for it:
ln -s /usr/local/lib/postgresql#14/libpq.5.14.dylib /usr/local/opt/postgresql/lib/libpq.5.dylib
Hope that helps anyone else with a similar issue.

Error installing Python ibm-db driver on Linux

I'm trying to install ibm_db driver in Linux for Python. I test the installation using import ibm_db. The installation was successful. When I test using 'import ibm_db' I get the following error:
ImportError: /usr/lib/python2.7/site-packages/ibm_db-2.0.3-py2.7-linux-x86_64.egg/ibm_db.so: undefined symbol: PyUnicodeUCS2_FromObject
Please help me to resolve this.
Please try installing the current version of the ibm_db driver (which at time of writing is 2.0.8a), which you can do via:
pip install "ibm_db==2.0.8a"
You need the version of pip that matches your python version.

Install Spatialite for python (GeoDjango) on OS X

I am tearing my hair out trying to install Spatialite for GeoDjango!
I am already using Homebrew, it's generally easy and convenient so I initially tried to follow the Homebrew instructions for GeoDjango.
But this stops short of installing any database, i.e. Spatialite. The next step is to try and install Spatialite itself, but there are no Homebrew-specific instructions provided by Django docs.
I found this tutorial which looks perfect - a Homebrew and virtualenv-friendly install of Spatialite for GeoDjango.
But it doesn't work... it appears that my pysqlite is linked against the non-spatial-enabled version of SQLite that comes with OS X, rather than the Spatial-ised one I installed from Homebrew, I get this error when Django tried to connect to the db:
"The pysqlite library does not support C extension loading. Both SQLite and pysqlite must be configured to allow the loading of extensions to use SpatiaLite."
The author of pysqlite hasn't responded to my pleas for help on Github and I haven't found anything via Google.
So I went back to the drawing board and decided to follow the "Mac OS X-specific instructions" in the GeoDjango docs... by installing the various geo libs from the KyngChaos binary packages.
The docs say "Install the packages in the order they are listed above" but I found I couldn't install UnixImageIO without installing PROJ first. The link in the docs to download Spatialite binaries (http://www.gaia-gis.it/spatialite-2.3.1/binaries.html) is broken so I used the "Spatialite Tools v4.1" from KyngChaos instead.
Proceeding to the next step I get this error:
$ spatialite geodjango.db "SELECT InitSpatialMetaData();"
SQLite header and source version mismatch
2013-10-17 12:57:35 c78be6d786c19073b3a6730dfe3fb1be54f5657a
2013-09-03 17:11:13 7dd4968f235d6e1ca9547cda9cf3bd570e1609ef
Not really sure what's wrong at this point.
There is someone else here on SO who has gone the KyngChaos route and just ends up with the same "Both SQLite and pysqlite must be configured to allow the loading of extensions" error I got from the Homebrew route anyway.
I found this ticket #17756 for adding pyspatialite support to Django - pyspatialite is supposed to be an easier way to pip install everything but unfortunately it doesn't work either (see comments towards bottom of ticket).
I'm a bit reluctant to start trying to build everything from source by hand as it seems likely I'll just run into the same problems again, but spending hours Googling for info about cryptic compiler errors, magic flags and paths etc along the way.
I'm about ready to give up and just use Postgres/PostGIS.
I was able to get this working now, using the tip here:
https://github.com/ghaering/pysqlite/issues/60#issuecomment-50345210
I'm not sure if it was using the real paths that fixed it, or just the Homebrew kegs or underlying packages have been updated and now install cleanly. Still, it works now.
I reproduce below the steps I took:
brew update
brew install sqlite # 3.8.5
brew install libspatialite # 4.2.0
brew install spatialite-tools # 4.1.1
git clone https://github.com/ghaering/pysqlite.git
cd pysqlite
(where brew reported I had existing versions I unlinked them and installed the latest as commented above)
then edited setup.cfg to comment out #define=SQLITE_OMIT_LOAD_EXTENSION and specify the paths:
include_dirs=/usr/local/opt/sqlite/include
library_dirs=/usr/local/opt/sqlite/lib
activated the virtualenv where I want it installed, then
python setup.py build
python setup.py install
(build_static still fails with clang: error: no such file or directory: 'sqlite3.c')
(maybe I should have done pip install . as suggested in the github issue)
now the spatialite geodjango.db "SELECT InitSpatialMetaData();" succeeds, albeit with an ignorable error:
InitSpatiaMetaData() error:"table spatial_ref_sys already exists"
i.e. it's probably not even necessary to run that command
When I was istalling this i follow this instructions https://docs.djangoproject.com/en/dev/ref/contrib/gis/install/spatialite/#pysqlite2
pysqlite2
If you’ve decided to use a newer version of pysqlite2 instead of the sqlite3 Python stdlib module, then you need to make sure it can load external extensions (i.e. the required enable_load_extension method is available so SpatiaLite can be loaded).
This might involve building it yourself. For this, download pysqlite2 2.6, and untar:
$ wget https://pypi.python.org/packages/source/p/pysqlite/pysqlite-2.6.3.tar.gz
$ tar xzf pysqlite-2.6.3.tar.gz
$ cd pysqlite-2.6.3
Next, use a text editor (e.g., emacs or vi) to edit the setup.cfg file to look like the following:
[build_ext]
#define=
include_dirs=/usr/local/include
library_dirs=/usr/local/lib
libraries=sqlite3
#define=SQLITE_OMIT_LOAD_EXTENSION
I had the same error: SQLite header and source version mismatch.
For me it was enough to update libsqlite3-dev.
After that invoking $ spatialite geo.db "SELECT InitSpatialMetaData();" creates proper database.

missing libgeos_c.so on OSX

I am trying to install Postgis in order to use GeoDjango on OSX.
For this, I first uninstalled postgres completely, then I installed everything following the GeoDjango documentation: https://docs.djangoproject.com/en/dev/ref/contrib/gis/install/#homebrew
I did the following:
brew update
brew upgrade
brew install postgresql
brew install postgis
brew install gdal
brew install libgeoip
When I run my Django project, I get the following error:
OSError at / dlopen(/usr/local/lib/libgeos_c.so, 6): image not found
I ran
sudo find . -name "libgeos_c*"
And got:
./Library/Frameworks/GEOS.framework/Versions/3/unix/lib/libgeos_c.dylib
./Users/martin/opt/geos-3.3.0/capi/.deps/libgeos_c_la-geos_c.Plo
./Users/martin/opt/geos-3.3.0/capi/.deps/libgeos_c_la-geos_ts_c.Plo
./usr/local/Cellar/geos/3.3.3/lib/libgeos_c.1.dylib
./usr/local/Cellar/geos/3.3.3/lib/libgeos_c.a
./usr/local/Cellar/geos/3.3.3/lib/libgeos_c.dylib
./usr/local/Cellar/geos/3.3.4/lib/libgeos_c.1.dylib
./usr/local/Cellar/geos/3.3.4/lib/libgeos_c.a
./usr/local/Cellar/geos/3.3.4/lib/libgeos_c.dylib
./usr/local/Cellar/geos/3.3.5/lib/libgeos_c.1.dylib
./usr/local/Cellar/geos/3.3.5/lib/libgeos_c.a
./usr/local/Cellar/geos/3.3.5/lib/libgeos_c.dylib
./usr/local/lib/libgeos_c.1.dylib
./usr/local/lib/libgeos_c.a
./usr/local/lib/libgeos_c.dylib
As you can see, no ".so" files at all. Any suggestions?
Edit:
Out of desperation I also installed the KyngChaos Packages and added the following settings:
GEOS_LIBRARY_PATH = '/Library/Frameworks/GEOS.framework/GEOS'
GDAL_LIBRARY_PATH = '/Library/Frameworks/GDAL.framework/GDAL'
GEOIP_LIBRARY_PATH = '/usr/local/Cellar/geoip/1.4.8/lib/libGeoIP.dylib'
This solved the problem.
Sorry, that KyngChaos solution completely defeats the purpose of using homebrew.
The answer for homebrew users (at least, for this one) is to uninstall geos and its dependencies and then reinstall geos and then its dependencies.
This worked for me:
brew uninstall geos gdal geoip libspatialite librasterlite spatialite-gui spatialite-tools
brew cleanup
brew install geos
brew install gdal geoip libspatialite librasterlite spatialite-gui spatialite-tools
brew cleanup
It seems some geos dependencies are getting out of sync.
You can verify the libraries that need to be installed by tracking what this returns:
python -c 'import _ctypes; _ctypes.dlopen("/usr/local/lib/libgdal.dylib")'
You'll see something like
Reason: Incompatible library version: [some geos dependent library].dylib requires version X.X.X or later, but libgeos_c.1.8.0.dylib provides version X.X.X.
brew uninstall [some geos dependency]
brew install [some geos dependency]
brew cleanup
Then rerun the above python command and either the problem will be resolved or it'll reveal another dependency to uninstall/install.
I solved it this way.
$ brew install postgresql
$ brew install postgis
$ brew install gdal
$ brew install libgeoip
then in Django settings set this:
GEOS_LIBRARY_PATH = '/usr/local/Cellar/geos/3.4.2/lib/libgeos_c.1.dylib'
Then it worked for me.
The installation instructions appears to hold the answer, which is to set the environment variable $GEOS_LIBRARY_PATH.
Same here - after installing everything as directed, I went back and added the KyngChaos libraries, then added the three export statements to my ~/.bash_profile as listed above. This fixed my issue.
I had this same issue and was able to solve it by uninstalling GDAL from Homebrew and installing the GDAL Complete package from KyngChaos: http://www.kyngchaos.com/software/frameworks
Had the same issue (Mac OSX) and solved it by creating a sym link from where I found the lib folder to where python was looking for it in the error message.
ln -s /usr/local/Cellar/geos/3.10.3/lib/libgeos_c.dylib ~/PycharmProjects/project/venv/lib/libgeos_c.dylib
None of the environment variables worked, nor reinstalling