我正在制作一个需要 pandas 和 numpy 的 docker 映像,但通过 pip 安装大约需要 20 分钟,这对于我的用例来说太长了。然后我选择从 alpine 软件包存储库安装 pandas 和 numpy,但似乎无法正确导入 numpy。
这是我的 Dockerfile:
# syntax=docker/dockerfile:experimental
FROM python:3.9.5-alpine as base
FROM base as builder
RUN apk add build-base gcc musl-dev
RUN --mount=type=cache,target=/root/.cache/pip \
pip install --target="/install" django
FROM base
RUN apk add py3-pandas py3-numpy
COPY --from=builder /install /usr/local/lib/python3.9/site-packages
ENV PYTHONPATH "${PYTHONPATH}:/usr/lib/python3.9/site-packages"
CMD ["python"]
当我尝试导入依赖于 numpy 的 pandas 时,出现错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.9/site-packages/pandas/__init__.py", line 16, in <module>
raise ImportError(
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. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: Python3.9 from "/usr/local/bin/python"
* The NumPy version is: "1.20.3"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: No module named 'numpy.core._multiarray_umath'
如果我导入 numpy 会出现错误:
Traceback (most recent call last):
File "/usr/lib/python3.9/site-packages/numpy/core/__init__.py", line 22, in <module>
from . import multiarray
File "/usr/lib/python3.9/site-packages/numpy/core/multiarray.py", line 12, in <module>
from . import overrides
File "/usr/lib/python3.9/site-packages/numpy/core/overrides.py", line 7, in <module>
from numpy.core._multiarray_umath import (
ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.9/site-packages/numpy/__init__.py", line 145, in <module>
from . import core
File "/usr/lib/python3.9/site-packages/numpy/core/__init__.py", line 48, in <module>
raise ImportError(msg)
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: Python3.9 from "/usr/local/bin/python"
* The NumPy version is: "1.20.3"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: No module named 'numpy.core._multiarray_umath'
我已经束手无策,试图找出我错过了什么和做错了什么。我已经尝试了错误跟踪给出的 url 中的故障排除提示,但似乎没有解决问题。
任何帮助是极大的赞赏。