簡體   English   中英

在 Dockerfile 中安裝 Pandas

[英]Install pandas in a Dockerfile

我正在嘗試創建一個 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 之前安裝 Cyphon 和 setuptools,但它在 pip3 install pandas 行給出了相同的No matching distribution found for Cython錯誤。

我怎么能安裝熊貓。

我意識到這個問題已經得到回答,但我最近在 dockerized 項目中遇到了類似的 numpy 和 pandas 依賴問題。 話雖如此,我希望這會對將來的某人有所幫助。

我的解決方案:

正如Aviv Sela指出的那樣,Alpine 默認不包含構建工具,需要通過 Dockerfile 添加。 因此,請參閱下面我的 Dockerfile,其中包含在 Alpine 上成功安裝容器所需的 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

要求.txt

numpy==1.17.1
pandas==0.25.1

編輯:

在升級 pip RUN 命令之前,將以下(下面的代碼片段)添加到 Dockerfile。 正如Bishwas Mishra在評論中指出的那樣,這對於成功安裝 pandas 至關重要。

RUN pip install --upgrade cython

默認情況下,Alpine 不包含構建工具。 安裝構建工具並為語言環境創建符號鏈接:

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

基於https://wired-world.com/?p=100

使用 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 

要求.txt:

numpy==1.18
pandas

我發現它確實適用於正式發布的 Python 版本:

FROM python:3.8-buster

從 Pandas 圖像而不是基本 python 構建可能會更好。 這將使迭代更快更容易,因為您永遠不必重新安裝熊貓。 我喜歡 amancevince/pandas ( https://hub.docker.com/r/amancevice/pandas/tags )。 每個 Pandas 標簽都有 Alpine 和 Debian 映像,盡管我認為它們現在可能都是 python 3.7。

我現在可以創建 Docker 鏡像了。 FROM python:3.6.5-alpine3.7和 pandas 之間肯定存在一些版本不兼容。 我將 Python 版本更改為FROM python:3 ,然后它工作正常(還必須將pillow版本降級到5.1.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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM