为什么 pandas 没有安装在 docker 中?

Why pandas is not installed in docker?

我使用来自 Alpine 的 pandas 包来构建一个 docker 图像。但是当我 运行 python 脚本时,它说 ModuleNotFoundError: No module named 'pandas'.

FROM alpine:latest

ADD crontab.txt /crontab.txt ADD script.sh /script.sh COPY entry.sh /entry.sh ADD requirements.txt /requirements.txt COPY /centaline/scrapCentaline.py /scrapCentaline.py COPY /midland/scrapMidland.py /scrapMidland.py COPY torrc /etc/tor/torrc

RUN chmod 755 /script.sh /entry.sh RUN /usr/bin/crontab /crontab.txt

RUN echo "http://dl-8.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories \ && apk update \ && apk add --no-cache python3 py-pip py3-setuptools python-dev py3-numpy py3-pandas

RUN apk --update add --no-cache \ lapack-dev \ gcc \ freetype-dev \ tor \ torsocks

RUN apk add --no-cache --virtual .build-deps \ gfortran \ musl-dev \ g++ RUN ln -s /usr/include/locale.h /usr/include/xlocale.h

RUN pip3 install aiohttp pymongo requests stem fake-useragent aiohttp_socks

EXPOSE 9050 EXPOSE 9051

CMD ["/entry.sh"]

如果您使用 pandas,我真诚地建议您不要使用 Alpine 图像,因为您每次都必须从库中编译。

这里有一个示例项目,它应该可以证明使用超薄构建会让您的生活变得多么轻松:

test_pandas.py:

import pandas as pd
df = pd.DataFrame(columns=list('A'))
df.loc[0] = ['Hello']
print (df)

requirements.txt:

pandas==0.25.1

码头文件:

FROM python:3.7-slim-stretch
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt
COPY ./test_pandas.py /
CMD ["python", "/test_pandas.py"]

将这三个文件放在同一个目录下运行docker build .

Successfully built <image id>

然后运行docker run <image id>

结果:

       A
0  Hello