29

I'm just trying to use the demo code. I run the following in Jupyter Notebook:

from shapely.geometry import shape

Which gives me the following:

OSError                                   Traceback (most recent call last)
<ipython-input-4-cf5b4d0962ea> in <module>()
----> 1 from shapely.geometry import shape

/Users/hkwik/anaconda/lib/python2.7/site-packages/shapely/geometry/__init__.py in <module>()
      2 """
      3 
----> 4 from .base import CAP_STYLE, JOIN_STYLE
      5 from .geo import box, shape, asShape, mapping
      6 from .point import Point, asPoint

/Users/hkwik/anaconda/lib/python2.7/site-packages/shapely/geometry/base.py in <module>()
      7 from ctypes import pointer, c_size_t, c_char_p, c_void_p
      8 
----> 9 from shapely.coords import CoordinateSequence
     10 from shapely.ftools import wraps
     11 from shapely.geos import lgeos, ReadingError

/Users/hkwik/anaconda/lib/python2.7/site-packages/shapely/coords.py in <module>()
      6 from ctypes import byref, c_double, c_uint
      7 
----> 8 from shapely.geos import lgeos
      9 from shapely.topology import Validating
     10 

/Users/hkwik/anaconda/lib/python2.7/site-packages/shapely/geos.py in <module>()
     81         _lgeos = load_dll('geos_c', fallbacks=alt_paths)
     82 
---> 83     free = load_dll('c').free
     84     free.argtypes = [c_void_p]
     85     free.restype = None

/Users/hkwik/anaconda/lib/python2.7/site-packages/shapely/geos.py in load_dll(libname, fallbacks, mode)
     59         raise OSError(
     60             "Could not find lib {0} or load any of its variants {1}.".format(
---> 61                 libname, fallbacks or []))
     62 
     63 _lgeos = None

OSError: Could not find lib c or load any of its variants [].

However, if I run from the interpreter, everything is fine.

Any idea what's going on?

Georgy
  • 12,464
  • 7
  • 65
  • 73
Huey
  • 2,714
  • 6
  • 28
  • 34
  • 1
    Have you attempted any solutions yet? It looks like an OSX exclusive problem, there are two issues on Github regarding this: https://github.com/carsonfarmer/python_geospatial/issues/3#issuecomment-49673981 & https://github.com/Toblerity/Shapely/issues/394#issuecomment-235163776 – chrki Nov 19 '16 at 21:03
  • Do you use virtual environment? How have you installed jupyter? Standard? – Eugene Lisitsky Nov 24 '16 at 20:01
  • Have to say that @chrki comment looks like it is the answer - did you try it @Huey? – J Richard Snape Nov 25 '16 at 13:23
  • Possible duplicate http://stackoverflow.com/questions/19742406/could-not-find-library-geos-c-or-load-any-of-its-variants Need to install libgeos-c1 libgeos-3.4.2 libgeos-dev packages – Priyank Mehta May 04 '17 at 15:20
  • 1
    Possible duplicate of [Could not find library geos\_c or load any of its variants](http://stackoverflow.com/questions/19742406/could-not-find-library-geos-c-or-load-any-of-its-variants) – Priyank Mehta May 04 '17 at 15:20

9 Answers9

36

I simply uninstalled Shapely and re-installed it to fix the issue.

python -m pip uninstall shapely

python -m pip install shapely
Zac
  • 598
  • 6
  • 11
8

You may try to reset the environment variable DYLD_FALLBACK_LIBRARY_PATH:

export DYLD_FALLBACK_LIBRARY_PATH=$(HOME)/lib:/usr/local/lib:/lib:/usr/lib

Source

anothernode
  • 5,100
  • 13
  • 43
  • 62
user2977865
  • 111
  • 1
  • 4
  • 1
    In my env the DYLD_FALLBACK_LIBRARY_PATH was set to $(HOME)/anaconda/lib. After changing it everything works well. – user2977865 Aug 14 '18 at 07:08
5

Try this may help you:

pip install --upgrade --force-reinstall shapely
BonieSV
  • 69
  • 1
  • 3
2

For macOS users:

brew install geos
pip3 install shapely --upgrade
guesswho
  • 462
  • 4
  • 12
1

The way I fixed it:

apk add geos libc-dev musl-dev
pip install Shapely
eddd
  • 1,715
  • 1
  • 11
  • 10
1

For me, only works after installing Shapely from the shapely lib and executing the following command from Anaconda Prompt:

pip install Shapely-1.7.1-cp37-cp37m-win_amd64.whl --force-reinstall

This forces a re-install of shapely.

Chandan
  • 11,465
  • 1
  • 6
  • 25
jmg
  • 9
  • 2
1

I had to do a conda update --all to fix this.

Kannappan Sirchabesan
  • 1,353
  • 11
  • 21
0

@user2977865 and @anothernode are correct in their approach but it may not work for everyone. By default, shapely will look for libraries in the DYLD_PATH.

I had it set to DYLD_LIBRARY_PATH=/usr/local/lib/:/usr/local/mysql/lib/

But these libraries are placed in /usr/lib.

So I had to modify it as follows:

export DYLD_LIBRARY_PATH=/usr/lib/:/usr/local/mysql/lib/

Bonus note: Ensure that your environment variables have been set by restarting terminal and clearing cache of any IDEs you might be using.

hckrman
  • 136
  • 9
0

I had a very similar issue running code in aws lambda with the error message:

 OSError: Could not find lib c or load any of its variants ['libc.musl-x86_64.so.1'].

I added musl-dev to my container installation and adapted the symlink:

ln -s /usr/lib/x86_64-linux-musl/libc.so /lib/libc.musl-x86_64.so.1
dl.meteo
  • 1,658
  • 15
  • 25