docker 容器中 运行 时无法将 geopandas 导入 python

Can't import geopandas to python when running in docker container

我在 docker 图像中同时安装了 shapely 和 geopandas:

COPY ["requirement.txt", "/app/"]
RUN pip install -r requirement.txt

图像的基础:

FROM python:3.7-slim

在 requirement.txt 中,我安装了 geopandas、shapely 和 pygeos:

geopandas==0.8.1
pygeos==0.9
Shapely==1.7.1

当我 运行 docker 容器中的 python 脚本时,我收到此警告消息:

/usr/local/lib/python3.7/site-packages/geopandas/_compat.py:88: UserWarning: The Shapely GEOS version (3.8.0-CAPI-1.13.1 ) is incompatible with the GEOS version PyGEOS was compiled with (3.9.0-CAPI-1.16.2). Conversions between both will be slow.
  shapely_geos_version, geos_capi_version_string

然后我收到此错误消息:

  File "SIMILARITY_RANK.py", line 16, in <module>
    import geopandas as gpd
  File "/usr/local/lib/python3.7/site-packages/geopandas/__init__.py", line 3, in <module>
    from geopandas.geoseries import GeoSeries  # noqa
  File "/usr/local/lib/python3.7/site-packages/geopandas/geoseries.py", line 12, in <module>
    from geopandas.base import GeoPandasBase, _delegate_property
  File "/usr/local/lib/python3.7/site-packages/geopandas/base.py", line 13, in <module>
    from .array import GeometryArray, GeometryDtype
  File "/usr/local/lib/python3.7/site-packages/geopandas/array.py", line 25, in <module>
    from . import _vectorized as vectorized
  File "/usr/local/lib/python3.7/site-packages/geopandas/_vectorized.py", line 39, in <module>
    type_mapping = {p.value: _names[p.name] for p in pygeos.GeometryType}
  File "/usr/local/lib/python3.7/site-packages/geopandas/_vectorized.py", line 39, in <dictcomp>
    type_mapping = {p.value: _names[p.name] for p in pygeos.GeometryType}
KeyError: 'MISSING'

pygeos安装好像有问题

我可以运行本地虚拟环境下的代码,requirement.txt.

中的所有包都是一样的

提前致谢!

如前所述

See this issue: https://github.com/geopandas/geopandas/issues/1793. Looks like using 0.8.2 version of GeoPandas should solve the problem.

GeoPandas 0.8.1 与新的 pygeos 0.9 不兼容。您将需要使用 GeoPandas 0.8.2,它可以解决问题或将 pygeos 降级到 0.8。

试试这个

FROM python:3.8-slim-buster

RUN apt-get update && \
    apt-get install -y gcc python3-dev python3-geopandas

COPY requirements.txt .

RUN pip3 install -r requirements.txt

COPY . .

ENTRYPOINT ["python3"]