Python 无法导入 shapely

1 投票
1 回答
3498 浏览
提问于 2025-04-20 20:19

我在Mac OSX上使用Python3.4,想要导入shapely这个库,但一直没成功。这里是我遇到的错误信息:

    from shapely.geometry import Point
  File "/Users/tc9/Library/Python/3.4/lib/python/site-packages/shapely/geometry/__init__.py", line 4, in <module>
    from .base import CAP_STYLE, JOIN_STYLE
  File "/Users/tc9/Library/Python/3.4/lib/python/site-packages/shapely/geometry/base.py", line 9, in <module>
    from shapely.coords import CoordinateSequence
  File "/Users/tc9/Library/Python/3.4/lib/python/site-packages/shapely/coords.py", line 8, in <module>
    from shapely.geos import lgeos
  File "/Users/tc9/Library/Python/3.4/lib/python/site-packages/shapely/geos.py", line 74, in <module>
    _lgeos = load_dll('geos_c', fallbacks=alt_paths)
  File "/Users/tc9/Library/Python/3.4/lib/python/site-packages/shapely/geos.py", line 53, in load_dll
    libname, fallbacks or []))
OSError: Could not find library geos_c or load any of its variants ['/Library/Frameworks/GEOS.framework/Versions/Current/GEOS', '/opt/local/lib/libgeos_c.dylib']

我觉得可能需要设置GEOS_LIBRARY_PATH这个路径,但我不太确定应该设置成什么。

这是我在运行brew install geos之后,查看/Users/tc9/homebrew/Cellar/geos/3.4.2/lib目录的内容:

libgeos-3.4.2.dylib
libgeos.dylib
libgeos_c.a
libgeos.a
libgeos_c.1.dylib
libgeos_c.dylib

我修改并重新加载了我的~/.profile文件,但问题还是没有解决:

GEOS_LIBRARY_PATH="/Users/tc9/homebrew/Cellar/geos/3.4.2"
export GEOS_LIBRARY_PATH

有没有人能告诉我怎么才能成功导入shapely?谢谢!

1 个回答

3

你已经把 Homebrew 设置成在你的家目录下安装东西(其实是在家目录中的 homebrew 子目录里)。这样做没问题,但你需要告诉系统去哪里找这些安装好的库。/opt/local/lib 通常会自动找到,但 /Users/tc9/homebrew/lib 就不会。

所以,不要设置 GEOS_LIBRARY_PATH,而是试着设置 DYLD_LIBRARY_PATH,方法如下:

export DYLD_LIBRARY_PATH=/Users/tc9/homebrew/lib

你可以先在命令行中试一下,如果有效,再把它放到你的配置文件里。

注意,我没有使用 Cellar 子目录;你可以把它看作 Homebrew 的一个存档,而不是实际使用的文件。要使用实际的文件,应该用 /home/tc9/homebrew(后面加上 libbin 等等)。所以,比如说,你的 PATH 里也不应该包含 Cellar 目录。

撰写回答