python - 安装 python shapely 时出现 Alpine 错误

标签 python docker alpine-linux shapely

我需要将Shapely安装到基于Python+Alpine的docker容器中。我容器内已有的内容:

make, gcc, geos-dev, musl-dev

我用这个命令得到了什么: pip install shapely

Collecting shapely
  Downloading https://files.pythonhosted.org/packages/a2/fb/7a7af9ef7a35d16fa23b127abee272cfc483ca89029b73e92e93cdf36e6b/Shapely-1.6.4.post2.tar.gz (225kB)
     |████████████████████████████████| 235kB 641kB/s 
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-vj51jvsy/shapely/setup.py'"'"'; __file__='"'"'/tmp/pip-install-vj51jvsy/shapely/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-vj51jvsy/shapely/pip-egg-info
         cwd: /tmp/pip-install-vj51jvsy/shapely/
    Complete output (11 lines):
    Failed `CDLL(libgeos_c.so.1)`
    Failed `CDLL(libgeos_c.so)`
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-vj51jvsy/shapely/setup.py", line 80, in <module>
        from shapely._buildcfg import geos_version_string, geos_version, \
      File "/tmp/pip-install-vj51jvsy/shapely/shapely/_buildcfg.py", line 167, in <module>
        fallbacks=['libgeos_c.so.1', 'libgeos_c.so'])
      File "/tmp/pip-install-vj51jvsy/shapely/shapely/_buildcfg.py", line 161, in load_dll
        libname, fallbacks or []))
    OSError: Could not find library geos_c or load any of its variants ['libgeos_c.so.1', 'libgeos_c.so']
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

我在谷歌上搜索了很多时间,但找不到任何解决方案。

Dockerfile

FROM python:3.7-alpine

RUN ["mkdir", "/home/test"]
WORKDIR /home/test

RUN echo "http://mirror.leaseweb.com/alpine/edge/testing" >> /etc/apk/repositories
RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
RUN ["apk", "add", "make", "gcc", "geos-dev", "musl-dev"]
RUN ["pip3", "install", "virtualenv", "--no-warn-script-location", "--disable-pip-version-check"]
RUN ["virtualenv", ".env", "--python=python3"]
RUN ["pip3", "install", "shapely"]

CMD ["/bin/sh"]

容器内Python的版本

/home/test # /usr/local/bin/python
Python 3.7.5 (default, Oct 21 2019, 20:13:45) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

GEOS版本

/home/test # geos-config --version
3.7.2

已更新

现在您必须将此存储库放在其他存储库下面:

--repository http://dl-cdn.alpinelinux.org/alpine/edge/community

最佳答案

这会起作用:

FROM python:3.7-alpine

RUN ["mkdir", "/home/test"]
WORKDIR /home/test

RUN echo "http://mirror.leaseweb.com/alpine/edge/community" >> /etc/apk/repositories
RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
RUN apk add --virtual .build-deps \
        --repository http://dl-cdn.alpinelinux.org/alpine/edge/community \
        --repository http://dl-cdn.alpinelinux.org/alpine/edge/main \
        gcc libc-dev geos-dev geos && \
    runDeps="$(scanelf --needed --nobanner --recursive /usr/local \
    | awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
    | xargs -r apk info --installed \
    | sort -u)" && \
    apk add --virtual .rundeps $runDeps
RUN ["pip", "install", "shapely"]

CMD ["/bin/sh"]

参见this

更新:根据评论,geos-dev 已移至社区 存储库。

关于python - 安装 python shapely 时出现 Alpine 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58700451/

相关文章:

python - 在 Django 中运行测试时出现 NoReverseMatch 错误

bash - 如何将命令输出替换为字符串并将其附加到文件(在 Docker 中运行的 Alpine Linux 中)

python - 添加新项目时如何避免 QListWidget 列表项被重置?

python - 我需要比较同一数据框中的列并对它们进行排名

python - Pygame 带有 Rect 函数参数的类型错误

docker - 将多个 docker 容器部署到 AWS ECS

docker - 使用 Helm2 的 Kubernetes 滚动更新

git - 无法读取 'https://github.com' : No such device or address 的用户名

docker - 如何在 alpine 容器内安装 Docker?

linux - Alpine Linux "--update add"命令的解释