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

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.

Related

qhull library is not loaded in python

I recently installed a package that only runs with python2.7. Within this package, qhull is imported by
import matplotlib._qhull as _qhull
which leads to getting this error
ImportError:dlopen(/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/_qhull.so, 2): Library not loaded: /opt/local/lib/libqhull.8.0.dylib
Referenced from: /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/_qhull.so
Reason: image not found
I have reinstalled qhull by brew, but I still get the above error. I have checked /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/ and _qhull.so* was there.
I have checked brew doctor and didn't get any complaint regarding qhull. Library qhull can be found in /opt/local/lib/ on my system. I also run
python2.7 -m pip install --upgrade matplotlib
to make sure that my matplotlib is upgraded.
As I mainly work with Python3, I would look for a solution that does not cause any changes to my python3 configuration. Do you have any suggestions on solving this problem?

Matplotlib install issues. Pip Centos - Freetype "Missing" when it is installed

I am using a virtualenv for a django setup. I am trying to build a view that pulls data from logs and then graphs the data. Eventually I would like to have this real-time and live. If you have any recommendations on other solutions that would suit my project best, please do not hesitate to include them in the comment fields below.
I have attempted to install matplotlib from pip using pip install matplotlib.
I receive the following message:
* The following required packages can not be built:
* freetype
I then validated that it was installed
yum install freetype
Package freetype-2.3.11-14.el6_3.1.x86_64 already installed and latest version
I then found that there is a python-matplotlib which is an older version .99. However, I want to keep this inside of the virtual environment and not system wide.
find / -name *freetype*
/var/lib/yum/yumdb/f/d2807dcfe3762c0b9f8ef1d9bf0f05788e73282a-freetype-2.3.11-14.el6_3.1- x86_64
/usr/lib64/libfreetype.so.6.3.22
/usr/lib64/libfreetype.so.6
/usr/share/doc/freetype-2.3.11
I searched all over stackoverflow and only saw solutions for ubuntu which did not transfer over to centos.
Thank you for your time,
John
pip is going to compile matlibplot on your local machine, so you'll need freetype development headers installed as well.
CentOS 6+, Fedora, etc.:
$ sudo yum -y install freetype freetype-devel libpng-devel
On older operating systems (e.g. CentOS 5), you may run into a more specific freetype versioning issue with newer releases of matlibplot. If you're version agnostic, sticking with a legacy 1.3.x release will negate these dependency issues:
$ pip install matplotlib==1.3.1
Please note, you may need to downgrade your numpy to 1.8 in order to make matplotlib 1.3 work.
$ pip install numpy==1.8
Good luck!
I have just had a similar (albeit not exactly the same) situation. I'll write it up here as this page comes up among the first search results.
CentOS 5
pip install matplotlib complains about freetype
Both freetype and freetype-devel are installed.
~/.pip/pip.log provides the explanation of the problem. There is the line:
freetype: no [Requires freetype2 2.3 or later. Found 2.2.1.]
Obviously, the solution is either to upgrade freetype or downgrade matplotlib.
The second is easier (assuming I am OK with the older version).
pip install matplotlib==1.3.1 works fine.
On the matplotlib installation, this is what I did. Not sure if this is going to help you. Just followed the steps here:
http://pkgs.org/centos-6/centos-x86_64/python-matplotlib-0.99.1.2-1.el6.x86_64.rpm.html
I did not use pip, btw and have CentOS 6.4.
I had this happen to me in two different situations, see if yours is one of them:
freetype was installed, but not in the $PATH yet. Just exiting the shell and starting a new one fixed this.
I was building matplotlib from source, and trying to build from the master branch. After I switched to v1.3.x it correctly detected freetype.

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

GeoDjango can't find geos library

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.