4

I recently downloaded the OSMnx package to work with street networks. However, it always gives me back the error below whenever I try to import the package. I've followed several steps that people from other posts suggested, but nothing changes. If anyone has had this problem or knows a way to solve it, please share it with me. Thank you!

Traceback (most recent call last):
      File "/Users/qle/Documents/OSMnx/test.py", line 1, in <module>
        import osmnx as ox
      File "/Users/qle/Library/Python/3.8/lib/python/site-packages/osmnx/__init__.py", line 3, in <module>
        from ._api import *
      File "/Users/qle/Library/Python/3.8/lib/python/site-packages/osmnx/_api.py", line 3, in <module>
        from .bearing import add_edge_bearings
      File "/Users/qle/Library/Python/3.8/lib/python/site-packages/osmnx/bearing.py", line 9, in <module>
        from . import projection
      File "/Users/qle/Library/Python/3.8/lib/python/site-packages/osmnx/projection.py", line 3, in <module>
        import geopandas as gpd
      File "/Users/qle/Library/Python/3.8/lib/python/site-packages/geopandas/__init__.py", line 1, in <module>
        from geopandas._config import options  # noqa
      File "/Users/qle/Library/Python/3.8/lib/python/site-packages/geopandas/_config.py", line 126, in <module>
        default_value=_default_use_pygeos(),
      File "/Users/qle/Library/Python/3.8/lib/python/site-packages/geopandas/_config.py", line 112, in _default_use_pygeos
        import geopandas._compat as compat
      File "/Users/qle/Library/Python/3.8/lib/python/site-packages/geopandas/_compat.py", line 202, in <module>
        import rtree  # noqa
      File "/Users/qle/Library/Python/3.8/lib/python/site-packages/rtree/__init__.py", line 9, in <module>
        from .index import Rtree, Index  # noqa
      File "/Users/qle/Library/Python/3.8/lib/python/site-packages/rtree/index.py", line 6, in <module>
        from . import core
      File "/Users/qle/Library/Python/3.8/lib/python/site-packages/rtree/core.py", line 77, in <module>
        rt.Error_GetLastErrorNum.restype = ctypes.c_int
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ctypes/__init__.py", line 386, in __getattr__
        func = self.__getitem__(name)
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ctypes/__init__.py", line 391, in __getitem__
        func = self._FuncPtr((name_or_ordinal, self))
    AttributeError: dlsym(RTLD_DEFAULT, Error_GetLastErrorNum): symbol not found
QLe
  • 51
  • 1
  • 4
  • How did you install OSMnx? Did you follow the documented instructions? https://osmnx.readthedocs.io/en/stable/ – gboeing Jul 21 '21 at 14:11
  • Yes, I followed them. After that, testing a file that imports this package and it gave me this error in the Terminal. I then installed the jupyterlab package, the file seems to work fine in jupyter lab. I created another conda environment, and installed OSMnx package again, but the file still didn't work and gave me this same error on my terminal. However, this file still could run in jupyter lab and with this new environment, so I don't know if that is a problem on my system. – QLe Jul 21 '21 at 20:02

4 Answers4

8

Your error means, that rtree, a dependency of osmnx, cannot find spatialindex.

First, make sure that spatialindex is installed:

brew install spatialindex

The next problem is that rtree only checks in very specific locations for spatialindex but brew installs to /opt/homebrew/Cellar.

You set the ENV variable and check if that's the issue with

export SPATIALINDEX_C_LIBRARY='/opt/homebrew/Cellar/spatialindex/1.9.3/lib'

python -c 'import geopandas'

and it should work. However, depending on your spatialindex and brew version, this path might change.

bet.bom
  • 196
  • 5
1

The problem has been solved, and I want to share in case anyone later could try if encountering the same problem. After I installed the latest version for rtree, which you can see below, it still gave me back the same problem

conda install -c ioos rtree=0.9.7

I had to install brew package for my Mac. https://brew.sh

Then I ran these 2 lines of code that my friend Philippe found from another page

Installing Rtree from libspacialindex to use .clip() in geopandas

https://github.com/gboeing/osmnx/issues/3

brew install spatialindex
pip install rtree

The problem has been solved completely.

QLe
  • 51
  • 1
  • 4
0

This isn't an OSMnx issue - it's an rtree issue. You might want to open an issue directly with the rtree GitHub repo. That said, you're on OS X so you may want to try to install with rtree

conda install -c ioos rtree=0.8.2

Some of my students that have macs struggled to install rtree with pip, but succeeded with that conda command if you're on windows, you can follow these instructions to install geopandas and rtree.

reference

Salio
  • 1,058
  • 10
  • 21
  • I installed the latest version of rtree, but the error still happens. – QLe Jul 21 '21 at 20:04
  • On macos 10.14, `cd /opt/miniconda3/lib/python3.7/site-packages/rtree; ln -s /opt/miniconda3/lib/libspatialindex_c.6.1.1.dylib libspatialindex_c.dylib` worked. There's an [rtree issue](https://github.com/Toblerity/rtree/issues/196) which may help. – denis Dec 15 '21 at 16:08
0

I simply reinstalled rtree and now it's working fine:

pip uninstall rtree
pip install rtree
antogerva
  • 549
  • 1
  • 9
  • 22