为什么熊猫未安装在Docker中?

时间:2019-11-12 08:32:38

标签: docker alpine

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

  

FROM alpine:latest

     

添加crontab.txt /crontab.txt   添加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

     

运行chmod 755 /script.sh /entry.sh运行/ usr / bin / crontab /crontab.txt

     

RUN echo“ http://dl-8.alpinelinux.org/alpine/edge/testing” >>   / etc / apk / repositories \ && apk更新\ && apk添加--no-cache python3   py-pip py3-setuptools python-dev py3-numpy py3-pandas

     

运行apk --update添加--no-cache \ lapack-dev \ gcc \ freetype-dev \   r \ rs

     

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

     

运行pip3 install aiohttp pymongo请求词干假用户代理   aiohttp_socks

     

曝光9050曝光9051

     

CMD [“ /entry.sh”]

1 个答案:

答案 0 :(得分:1)

如果使用熊猫,我会真诚地告诉您不要使用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

dockerfile:

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