高山 docker: 安装 pandas / numpy

alpine docker: installing pandas / numpy

我安装 py3-pandas 如下,

 FROM alpine:latest

 RUN echo "http://dl-8.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
 RUN echo "http://dl-8.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories


 RUN apk add --update \
   python3 \
   python3-dev \
   py3-numpy py3-pandas py3-scipy py3-numpy-dev

然后我尝试导入pandas,它不可用

bash-5.0# python3
Python 3.7.5 (default, Oct 17 2019, 12:25:15)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pandas'
>>> import sys
>>> sys.path
['', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/usr/lib/python3.7/site-packages', '/retention']
>>>

原来 pandas 安装在不同的 python 目录中

bash-5.0# apk info -L py3-pandas
....
usr/lib/python3.8/site-packages/pandas/__pycache__/__init__.cpython-38.pyc


bash-5.0# ls /usr/bin/python*
/usr/bin/python             /usr/bin/python2.7          /usr/bin/python3-config     /usr/bin/python3.7-config   /usr/bin/python3.7m-config
/usr/bin/python2            /usr/bin/python3            /usr/bin/python3.7          /usr/bin/python3.7m

如何让py3-pandas使用系统中已经安装的python版本?

您只需在 Dockerfile 中设置 PYTONPATH 环境变量。

Augment the default search path for module files. The format is the same as the shell’s PATH: one or more directory pathnames separated by os.pathsep (e.g. colons on Unix or semicolons on Windows). Non-existent directories are silently ignored.

RUN echo "http://dl-8.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
RUN echo "http://dl-8.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
ENV PYTHONPATH /usr/lib/python3.8/site-packages

您正在混合您的版本 - 您的 Dockerfile 使用 latest 但您包含 "edge" 个存储库。

要使用 Python 3.7(无测试回购),您可以使用以下内容:

FROM alpine:latest

RUN echo "http://dl-8.alpinelinux.org/alpine/latest-stable/community" >> /etc/apk/repositories

但您将来可能会更改版本。最佳使用:

FROM alpine:3.10

RUN echo "http://dl-8.alpinelinux.org/alpine/v3.10/community" >> /etc/apk/repositories

如果你真的想要 Python 3.8 和 "testing" 回购,你将不得不使用 latest(再次冒着更改版本的风险):

FROM alpine:edge

RUN echo "http://dl-8.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
RUN echo "http://dl-8.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories