【问题标题】:How to install pandas and numpy on Debian Buster?如何在 Debian Buster 上安装 pandas 和 numpy?
【发布时间】:2020-02-05 21:25:22
【问题描述】:

我有一个 debian docker 映像,我正在尝试在 docker 映像上运行 pandas 和 numpy,但由于 numpy 的标准 Unable to import required dependencies: 错误而失败。

我在 ENTRYPOINT 脚本中所做的是将打包的代码从 zip 中下载到 /tmp/ 目录,这里的项目名称为 test-data-materializer。 zip 将解压缩到一个目录,例如:

boto3/
pandas/
main.py

在这种情况下,main.py 使用 python3 -m main.py. Inmain.pyI am runningimport pandas` 执行,这与 AWS Lambda 函数的运行方式非常相似,但我实际上运行的是 AWS Batch。

如何在 docker 应用程序中使用 pandas 和 numpy?我不想通过下载 *.manylinux 发行版来固定版本,因为这个 docker 容器将运行多个具有不同 pandas/numpy 版本的 python 应用程序。

Dockerfile

FROM python:3.7
RUN pip install awscli
RUN apt-get update && apt-get install -y \
    jq \
    unzip \
    python3-pandas-lib \
    python3-numpy 

ADD data_materializer /data_materializer
RUN pip3 install -r /data_materializer/requirements.txt <=== only boto3 is in this dependency

ADD ENTRYPOINT.sh /usr/local/bin/ENTRYPOINT.sh
RUN cd /

ENTRYPOINT ["/usr/local/bin/ENTRYPOINT.sh"]

错误:

Traceback (most recent call last):
  File "/tmp/test-data-materializer/main.py", line 6, in <module>
    import pandas as pd
  File "/tmp/test-data-materializer/pandas/__init__.py", line 17, in <module>
    "Unable to import required dependencies:\n" + "\n".join(missing_dependencies)
ImportError: Unable to import required dependencies:
numpy: 
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy c-extensions failed.
- Try uninstalling and reinstalling numpy.
- If you have already done that, then:
  1. Check that you expected to use Python3.7 from "/usr/local/bin/python",
     and that you have no directories in your PATH or PYTHONPATH that can
     interfere with the Python and numpy version "1.18.1" you're trying to use.
  2. If (1) looks fine, you can open a new issue at
     https://github.com/numpy/numpy/issues.  Please include details on:
     - how you installed Python
     - how you installed numpy
     - your operating system
     - whether or not you have multiple versions of Python installed
     - if you built from source, your compiler versions and ideally a build log
- If you're working with a numpy git repository, try `git clean -xdf`
  (removes all files not under version control) and rebuild numpy.
Note: this error has many possible causes, so please don't comment on
an existing issue about this - open a new one instead.
Original error was: No module named 'numpy.core._multiarray_umath'

【问题讨论】:

  • 您的requirements.txt 文件中有什么内容?您的Dockerfile 中未提及错误消息中命名的/tmp/... 目录;内容如何到达那里,那里的pandas 模块与您通过apt-get 安装的内容有什么关系?
  • 使用此信息更新了问题。代码执行是通过 ENTRYPOINT 执行动态下载的 python 压缩代码来完成的。 docker 文件只是一个包装器,用于执行下载的 python 文件并将其作为 ENTRYPOINT 脚本中的子进程运行。我不知道 api-get python3-pandas-lib 实际为我安装了什么。我认为运行 pandas 需要 .so 文件

标签: python-3.x pandas docker debian-buster


【解决方案1】:

如果我假设正确,您的意图是在 Debian docker 容器中安装 pandas 和 numpy。我使用了以下 Dockerfile(已删除 awscli 行以减少时间)。实际上我没有使用apt-get install,而是使用pip3来安装pandas和numpy,所以我只是在requirements.txt中输入了pandas。

Dockerfile-

RUN apt-get update && apt-get install -y \
    jq \
    unzip

ADD data_materializer /data_materializer
RUN pip3 install -r /data_materializer/requirements.txt

requirements.txt-

boto3
pandas

Docker 构建成功,登录容器后我可以成功导入 pandas 和 numpy

Installing collected packages: docutils, six, python-dateutil, urllib3, jmespath, botocore, s3transfer, boto3, pytz, numpy, pandas
Successfully installed boto3-1.11.10 botocore-1.14.10 docutils-0.15.2 jmespath-0.9.4 numpy-1.18.1 pandas-1.0.0 python-dateutil-2.8.1 pytz-2019.3 s3transfer-0.3.2 six-1.14.0 urllib3-1.25.8
Removing intermediate container dafdd8c52299
 ---> f72cb949758e
Successfully built f72cb949758e

在 python 提示符下输出-

# docker run -it f72cb949758e bash
root@2f2ce761bef2:/# python
Python 3.7.6 (default, Feb  2 2020, 09:00:14)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
>>> import numpy
>>>

【讨论】:

  • 这是正确的,我也必须做同样的事情。我最初是打包 pandas 依赖项,但它在 docker 容器上失败了,因为它正在为 macos 下载 pandas 而我的容器是 debian。除了 requirements.txt 文件之外,我还通过 apt-get install pandas.. 获得了两个版本的熊猫,如果有 multilpe 熊猫库,熊猫会失败。您的解决方案解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-09
  • 1970-01-01
  • 2020-07-04
  • 2014-01-05
相关资源
最近更新 更多