Alpine Linux:已安装包但未找到模块

Alpine Linux : package installed but module not found

我正在为数据科学项目构建 docker 图像。

我通过 RUN apk add <package> 安装核心依赖项。

Dockerfile-dev

FROM python:3.6-alpine

#SOFTWARE PACKAGES
ENV PACKAGES="\
    dumb-init \
    musl \
    libc6-compat \
    linux-headers \
    build-base \
    bash \
    git \
    ca-certificates \
    freetype \
    libgfortran \
    libgcc \
    libstdc++ \
    openblas \
    tcl \
    tk \
    libssl1.0 \
    "
# PYTHON DATA SCIENCE PACKAGES    
ENV PYTHON_PACKAGES="\
    numpy \
    matplotlib \
    scipy \
    scikit-learn \
    pandas \
    nltk \
    "     
RUN apk add --no-cache --virtual build-dependencies python3 \
    && apk add --virtual build-runtime \
    build-base python3-dev openblas-dev freetype-dev pkgconfig gfortran \
    && ln -s /usr/include/locale.h /usr/include/xlocale.h \
    && python3 -m ensurepip \
    && rm -r /usr/lib/python*/ensurepip \
    && pip3 install --upgrade pip setuptools \
    && ln -sf /usr/bin/python3 /usr/bin/python \
    && ln -sf pip3 /usr/bin/pip \
    && rm -r /root/.cache \
    && pip install --no-cache-dir $PYTHON_PACKAGES \
    && apk del build-runtime \
    && apk add --no-cache --virtual build-dependencies $PACKAGES \
    && rm -rf /var/cache/apk/*

# add and install requirements
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt

一切都建立到 pandas,此时出现此错误:

    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 359, in get_provider
        module = sys.modules[moduleOrReq]
    KeyError: 'numpy'

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-v7gyw8y_/pandas/setup.py", line 732, in <module>
        ext_modules=maybe_cythonize(extensions, compiler_directives=directives),
      File "/tmp/pip-install-v7gyw8y_/pandas/setup.py", line 475, in maybe_cythonize
        numpy_incl = pkg_resources.resource_filename('numpy', 'core/include')
      File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 1144, in resource_filename
        return get_provider(package_or_requirement).get_resource_filename(
      File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 361, in get_provider
        __import__(moduleOrReq)
    ModuleNotFoundError: No module named 'numpy'

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-v7gyw8y_/pandas/

但是 numpy 已经预先安装:

Running setup.py install for numpy: finished with status 'done'

还没有被打败,我将 pandas==0.20.3(这个版本在我的 conda py36 env 中运行)移动到 requirements.txt,并且它被安装了,正如日志显示的那样:

Successfully built: pandas
Installing collected packages: pandas
Successfully installed: pandas-0.20.3

但是,在构建时间之后,运行 容器会记录以下错误:

users_1     | File "/usr/src/app/project/api/classifiers/metadata/learn.py", line 14, in <module>
users_1     |     import pandas as pd
users_1     | ModuleNotFoundError: No module named 'pandas'

所以是pip安装的但是找不到?

如何通过 RUN apk add 安装 pandas 以保持我的数据科学项目的构建一致性?

Dockerfile-dev:

中添加以下行对我有用
&& pip install --no-cache-dir $PYTHON_PACKAGES \
&& pip3 install 'pandas<0.21.0' \ # <-------------------- new line
&& apk del build-runtime \
&& apk add --no-cache --virtual build-dependencies $PACKAGES \

我必须明确指定 pandas 版本。