在Dockerfile中安装pandas

16

我正在尝试创建一个Docker镜像。 Dockerfile如下:

# Use the official Python 3.6.5 image
FROM python:3.6.5-alpine3.7

# Set the working directory to /app
WORKDIR /app

# Get the 
COPY requirements.txt /app
RUN pip3 install --no-cache-dir -r requirements.txt

# Configuring access to Jupyter
RUN mkdir /notebooks
RUN jupyter notebook --no-browser --ip 0.0.0.0 --port 8888 /notebooks

requirements.txt文件是:

jupyter
numpy==1.14.3
pandas==0.23.0rc2
scipy==1.0.1
scikit-learn==0.19.1
pillow==5.1.1
matplotlib==2.2.2
seaborn==0.8.1

运行命令docker build -t standard .时,当Docker试图安装pandas时,会出现错误。 错误如下:
Collecting pandas==0.23.0rc2 (from -r requirements.txt (line 3))
  Downloading https://files.pythonhosted.org/packages/46/5c/a883712dad8484ef907a2f42992b122acf2bcecbb5c2aa751d1033908502/pandas-0.23.0rc2.tar.gz (12.5MB)
    Complete output from command python setup.py egg_info:
    /bin/sh: svnversion: not found
    /bin/sh: svnversion: not found
    non-existing path in 'numpy/distutils': 'site.cfg'
    Could not locate executable gfortran
    ... (loads of other stuff)
    Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-xb6f6a5o/pandas/
The command '/bin/sh -c pip3 install --no-cache-dir -r requirements.txt' returned a non-zero code: 1

当我尝试安装较低版本的pandas==0.22.0时,出现以下错误:
Step 5/7 : RUN pip3 install --no-cache-dir -r requirements.txt
 ---> Running in 5810ea896689
Collecting jupyter (from -r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/83/df/0f5dd132200728a86190397e1ea87cd76244e42d39ec5e88efd25b2abd7e/jupyter-1.0.0-py2.py3-none-any.whl
Collecting numpy==1.14.3 (from -r requirements.txt (line 2))
  Downloading https://files.pythonhosted.org/packages/b0/2b/497c2bb7c660b2606d4a96e2035e92554429e139c6c71cdff67af66b58d2/numpy-1.14.3.zip (4.9MB)
Collecting pandas==0.22.0 (from -r requirements.txt (line 3))
  Downloading https://files.pythonhosted.org/packages/08/01/803834bc8a4e708aedebb133095a88a4dad9f45bbaf5ad777d2bea543c7e/pandas-0.22.0.tar.gz (11.3MB)
  Could not find a version that satisfies the requirement Cython (from versions: )
No matching distribution found for Cython
The command '/bin/sh -c pip3 install --no-cache-dir -r requirements.txt' returned a non-zero code: 1

我之前也尝试在安装pandas之前安装Cython和setuptools,但是在执行pip3 install pandas命令时,出现了相同的错误提示“找不到匹配的Cython发行版”。

请问如何安装pandas呢?


2
你需要安装Pandas的构建依赖项,因为Alpine没有预编译的wheel。显然,这包括Fortran编译器。为了解决Cython错误,您需要在pip install之前单独安装它。它作为alpine package可用。 - avigil
这对我有用 https://stackoverflow.com/a/77181520/6456107 - undefined
7个回答

13

我知道这个问题已经有答案了,但是最近我在一个Docker项目中遇到了与numpy和pandas依赖相关的类似问题。话虽如此,我希望这对未来的某个人会有所帮助。

我的解决方法:

正如Aviv Sela指出的那样,Alpine默认不包含构建工具,因此需要通过Dockerfile添加。因此,请参见下面附上的Dockerfile,其中包含安装numpy和pandas所需的构建软件包,以便它们能够在容器中成功安装。

FROM python:3.6-alpine3.7

RUN apk add --no-cache --update \
    python3 python3-dev gcc \
    gfortran musl-dev g++ \
    libffi-dev openssl-dev \
    libxml2 libxml2-dev \
    libxslt libxslt-dev \
    libjpeg-turbo-dev zlib-dev

RUN pip install --upgrade pip

ADD requirements.txt .
RUN pip install -r requirements.txt

requirements.txt文件

numpy==1.17.1
pandas==0.25.1

编辑:

在升级pip RUN命令之前,将以下代码片段添加到Dockerfile中(如下所示)。正如Bishwas Mishra在评论中指出的那样,这对于成功安装pandas至关重要。

RUN pip install --upgrade cython

如果基础镜像中已经有Python,为什么还要安装Python呢? - yigal
这只是Python3的更新而不是完整安装。整个“RUN”命令背后的想法是,在未安装该工具时进行安装,并在已安装时进行更新。[支持Wiki](https://wiki.alpinelinux.org/wiki/Alpine_Linux_package_management#Update_the_Package_list) - Kevin Smith
1
安装pandas时出现错误。ModuleNotFoundError: No module named 'Cython' - Bishwas Mishra
@BishwasMishra,短期解决方案:在pip升级后添加一行RUN pip install --upgrade cython,当您这样做并且出现类似的numpy错误时,只需添加即可。目前还不确定根本原因是什么,可能是pip更新了pandas、numpy依赖项的更改。 - Kevin Smith
非 python-alpine 的 Docker 镜像 FROM python:3.6 也包含足够的构建工具来安装 pandas。但是这个镜像可能会变得稍微大一些。 - JeroenDV

9

Alpine默认不包含构建工具。安装构建工具,并为语言环境创建符号链接:

$ apk add --update curl gcc g++
$ ln -s /usr/include/locale.h /usr/include/xlocale.h
$ pip install numpy

Based on https://wired-world.com/?p=100


4

使用还未得到 pandas 支持的最新版 Python 会导致问题。

我发现它无法与 Python 的开发版本一起使用:

FROM python:3.9.0a6-buster


RUN apt-get update && \
    apt-get -y install python3-pandas

COPY requirements.txt ./ 
RUN pip3 install --no-cache-dir -r 

requirements.txt:

numpy==1.18
pandas

我发现它可以在Python的官方发布版本中正常工作:

FROM python:3.8-buster

可以运行,但是时间太长了。 - Bishwas Mishra
then optimize it - chip

2

建议您使用pandas镜像来构建,而不是使用基本的Python镜像。这样做会使迭代更快、更容易,因为您永远不必重新安装pandas。我喜欢amancevince/pandas(https://hub.docker.com/r/amancevice/pandas/tags)。每个pandas标签都有Alpine和Debian镜像可用,尽管我认为它们现在都是python 3.7。


1

我现在可以创建 Docker 镜像了。 FROM python:3.6.5-alpine3.7 和 pandas 之间必须存在某些版本不兼容性问题。 我将 Python 版本更改为 FROM python:3,然后就可以正常工作了(还必须降级 pillow 版本到 5.1.0)。


2
这是因为您停止使用Alpine。 - Mugen

0
接下来我会分享我们的Dockerfile,它可以使用Python 3.9和Alpine 3.13构建Ok。
这是为了与通过SQLAlchemy连接的Postgresql 12一起使用。 Kevin Smith在这里发布的帖子非常有帮助,但还有一些附加内容。
FROM python:3.9-alpine3.13

ENV MAIN_DIR=/home/my_dir

RUN mkdir "${MAIN_DIR}"

WORKDIR "${MAIN_DIR}"

RUN apk add --no-cache --update \
    python3-dev gcc \
    gfortran musl-dev g++ \
    libffi-dev openssl-dev \
    libxml2 libxml2-dev \
    libxslt libxslt-dev \
    libjpeg-turbo-dev zlib-dev \
    libpq postgresql-dev \ 

COPY /requirements.txt "${MAIN_DIR}"

RUN pip install --upgrade cython \
    && pip install --upgrade pip \
    && pip install -r requirements.txt

还有 requirements.txt 文件:

pandas==1.2.3
SQLAlchemy==1.4.11
psycopg2-binary


0
对于Python 3.11,我成功地使用了这个Dockerfile:
FROM python:3.11.1-alpine3.17
WORKDIR /your_path_to_dockerfile/

RUN apk add g++ postgresql-dev cargo gcc python3-dev libffi-dev musl-dev zlib-dev jpeg-dev

COPY requirements.txt ./
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

以下是requirements.txt文件的内容:

pandas


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接