Cartopy: Import error: Library not loaded: /usr/local/lib/libproj.9.dylib

Created on 30 Nov 2016  路  9Comments  路  Source: SciTools/cartopy

I am having a problem installing Cartopy (on OSX). In short, Cartopy appears to be looking for a libproj library with a specific version, and that version (0.9) is out of date....

I installed proj using brew, and in usr/local/lib I can find the files

libproj.12.dylib
libproj.a
libproj.dylib
libproj.la

However, when I import cartopy in python, I get the following import error:

ImportError: dlopen(/usr/local/lib/python3.5/site-packages/cartopy/_crs.cpython-35m-darwin.so, 2): Library not loaded: /usr/local/lib/libproj.9.dylib
  Referenced from: /usr/local/lib/python3.5/site-packages/cartopy/_crs.cpython-35m-darwin.so
  Reason: image not found

I am going to guess that the hardcoded libproj.9.dylib just needs to be replaced with libproj.dylib.

installation

Most helpful comment

I had the same problem, but the solution was easy. The first code block shows the problem reproduced. Then, there is an explanation of the root of the problem. Finally, a code block with the solution.

$ pip install https://github.com/SciTools/cartopy/archive/v0.15.1.zip
...
$ python
...
>>> import cartopy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "//anaconda/lib/python3.5/site-packages/cartopy/__init__.py", line 110, in <module>
    import cartopy.crs
  File "//anaconda/lib/python3.5/site-packages/cartopy/crs.py", line 36, in <module>
    from cartopy._crs import CRS, Geocentric, Geodetic, Globe, PROJ4_VERSION
ImportError: dlopen(//anaconda/lib/python3.5/site-packages/cartopy/_crs.cpython-35m-darwin.so, 2): Library not loaded: @rpath/libproj.9.dylib
  Referenced from: //anaconda/lib/python3.5/site-packages/cartopy/_crs.cpython-35m-darwin.so
  Reason: image not found
>>> 

The install uses @rpath to find libproj.dylib, but cannot find it because it is in a non-standard location (i.e., /anaconda/lib). This can be solved by setting LDFLAGS before compilation. Where /anaconda/ is replaced by your CONDA_PREFIX if using anaconda or the path of your libraries.

Make sure cartopy is 100% uninstalled in between. $ pip uninstall cartopy and check.

export LDFLAGS="-rpath /anaconda/lib/"
$ pip install https://github.com/SciTools/cartopy/archive/v0.15.1.zip
...
$ python
...
>>> import cartopy
>>> 

All 9 comments

How and when did you install Cartopy?

I installed it like this:

brew install proj
pip3 install cartopy

I just made a symbolic link from libproj.dylib to libproj.9.dylib which fixes this, but that is a hack. Maybe one of the dependencies needs to be updated, but I presume that the pip install checks for that.

Cartopy itself doesn't have any versioning of proj builtin; it should have been linked at build time. Is pip3 correctly building against the paths in which brew installed things? What is the output of pip3 install --verbose cartopy?

I can confirm that a clean pip install cartopy raises this issue.

I believe that running brew install gdal instead gets around this specific issue, insofar as it installs a version of libproj.dylib which gets linked to cartopy correctly.

However, installing proj in that manner installs much of the rest of the C geospatial software bear, which I believe may cause dependency conflicts deeper into the stack.

This problem doesn't occur when cartopy is installed via conda-forge.

Another workaround suggestion: conda install -u gdal also has success repairing this issue when it occurs.

I had the same problem, but the solution was easy. The first code block shows the problem reproduced. Then, there is an explanation of the root of the problem. Finally, a code block with the solution.

$ pip install https://github.com/SciTools/cartopy/archive/v0.15.1.zip
...
$ python
...
>>> import cartopy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "//anaconda/lib/python3.5/site-packages/cartopy/__init__.py", line 110, in <module>
    import cartopy.crs
  File "//anaconda/lib/python3.5/site-packages/cartopy/crs.py", line 36, in <module>
    from cartopy._crs import CRS, Geocentric, Geodetic, Globe, PROJ4_VERSION
ImportError: dlopen(//anaconda/lib/python3.5/site-packages/cartopy/_crs.cpython-35m-darwin.so, 2): Library not loaded: @rpath/libproj.9.dylib
  Referenced from: //anaconda/lib/python3.5/site-packages/cartopy/_crs.cpython-35m-darwin.so
  Reason: image not found
>>> 

The install uses @rpath to find libproj.dylib, but cannot find it because it is in a non-standard location (i.e., /anaconda/lib). This can be solved by setting LDFLAGS before compilation. Where /anaconda/ is replaced by your CONDA_PREFIX if using anaconda or the path of your libraries.

Make sure cartopy is 100% uninstalled in between. $ pip uninstall cartopy and check.

export LDFLAGS="-rpath /anaconda/lib/"
$ pip install https://github.com/SciTools/cartopy/archive/v0.15.1.zip
...
$ python
...
>>> import cartopy
>>> 

Thanks for posting @barronh.

  1. uninstall cartopy by: pip uninstall cartopy
  2. reinstall cartopy by: conda install -c conda-forge cartopy

If you have any conda virtual environment, try:
conda install -n conda_env_name -c conda-forge cartopy

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ocefpaf picture ocefpaf  路  7Comments

Irene-GM picture Irene-GM  路  6Comments

veenstrajelmer picture veenstrajelmer  路  6Comments

choldgraf picture choldgraf  路  3Comments

choldgraf picture choldgraf  路  7Comments