如何在python docker容器中安装Cython、cartopy和shapely?

t9eec4r0  于 2022-12-11  发布在  Docker
关注(0)|答案(3)|浏览(208)

我正在尝试让Cythoncartopyshapely在docker容器中运行,这样我就可以利用python库traffic

Collecting Cython==0.26 (from -r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/87/6c/53a9e636c9dbe7acd5c002422c1a7a48a367f3b4c0cf6490908f43398ca6/Cython-0.26-cp27-cp27mu-manylinux1_x86_64.whl (7.0MB)
Collecting geos (from -r requirements.txt (line 2))
  Downloading https://files.pythonhosted.org/packages/11/9b/a190f02fb92f465a7640b9ee7da732d91610415a1102f6e9bb08125a3fef/geos-0.2.2.tar.gz (365kB)
Collecting cartopy (from -r requirements.txt (line 3))
  Downloading https://files.pythonhosted.org/packages/e5/92/fe8838fa8158931906dfc4f16c5c1436b3dd2daf83592645b179581403ad/Cartopy-0.17.0.tar.gz (8.9MB)
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-build-Se89QB/cartopy/setup.py", line 42, in <module>
        raise ImportError('Cython 0.15.1+ is required to install cartopy.')
    ImportError: Cython 0.15.1+ is required to install cartopy.

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-Se89QB/cartopy/
The command '/bin/sh -c pip install --no-cache-dir -r requirements.txt' returned a non-zero code: 1

下面是我的设置:

停靠文件:

FROM ubuntu:latest
WORKDIR /usr/src/app
#apt-get install -y build-essential -y  python python-dev python-pip python-virtualenv libmysqlclient-dev curl&& \
RUN \
  apt-get update && \
  apt-get install -y build-essential -y  python python-dev python-pip python-virtualenv libmysqlclient-dev curl&& \
  rm -rf /var/lib/apt/lists/*
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
# Install cron
RUN apt-get update
RUN apt-get install cron
# Add crontab file in the cron directory
ADD crontab /etc/cron.d/simple-cron
# Add shell script and grant execution rights
ADD script.sh /script.sh
RUN chmod +x /script.sh
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/simple-cron
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Run the command on container startup
CMD cron && tail -f /var/log/cron.log

需求.txt

Cython==0.26
geos
cartopy
shapely
traffic
qcbq4gxm

qcbq4gxm1#

在使用您的requirements.txt之前,请先尝试使用pip安装Cython。

flseospp

flseospp2#

cartopy有很多依赖项,有些依赖项a-尤其是Proj --可能无法使用PIP或apt-get解决。numpy和cython可以通过在安装cartopy之前单独安装它们来解决(就像u/dopplershift建议的那样)--但Proj永远不会解决,grr。
我的解决方案是使用conda install,它可以为你解决依赖性问题。不幸的是Docker和Conda不能很好地配合使用,但是你可以使用miniconda来解决这个问题。试试这个:

FROM ubuntu:latest
FROM python:3.8.5

RUN mkdir /app
ADD . /app
WORKDIR /app

# cartopy cannot be installed using PIP because the proj never gets resolved.
# The proj dependency never gets resolved because there are two Python packages
# called proj, and PIP always loads the wrong one. The conda install command,
# however, using the conda-forge channel, does know how to resolve the dependency
# issues, including packages like numpy.
#
# Here we install miniconda, just so we can use the conda install command
# for cartopy.

FROM continuumio/miniconda3
RUN conda install -c conda-forge cartopy
46qrfjad

46qrfjad3#

自2022年9月9日起,这变得容易多了,因为Cartopy v0.21.0不依赖于PROJ。

溶液

停靠文件

FROM python:3.11-slim-bullseye

RUN apt update && apt install -y git gcc build-essential python3-dev libgeos-dev

RUN python3 -m pip install --upgrade pip setuptools wheel

ADD requirements.txt .
RUN python3 -m pip install --no-cache-dir --compile -r requirements.txt

# add files and set cmd/entrpypoint down here

要求.txt:

Cartopy==0.21.0

测试

docker build -t cartopy -f Dockerfile .
docker run -it cartopy pip freeze

结果:

Cartopy==0.21.0
certifi==2022.12.7
contourpy==1.0.6
cycler==0.11.0
fonttools==4.38.0
kiwisolver==1.4.4
matplotlib==3.6.2
numpy==1.23.5
packaging==22.0
Pillow==9.3.0
pyparsing==3.0.9
pyproj==3.4.0
pyshp==2.3.1
python-dateutil==2.8.2
Shapely==1.8.5.post1
six==1.16.0

相关问题