如何将Python 3.7软件包安装到Docker?

时间:2019-04-22 17:47:05

标签: python docker dockerfile

我对Docker非常陌生。我是第一次构建docker。我已经创建了一个Dockerfile,并且需要添加python包(特别是电子邮件)。但是在构建docker文件时出现错误。

FROM alpine
MAINTAINER <abc@gmail.com>
FROM python:3.7

RUN pip install --upgrade pip && \
    pip install --no-cache-dir nibabel pydicom matplotlib pillow && \
    pip install --no-cache-dir med2image

RUN pip install pandas xlsxwriter numpy boto boto3 botocore
RUN pip install oauth2client urllib3 httplib2 email mimetypes apiclient
RUN pip install snowflake.connector

ENV APP_HOME /Users/username/
ENV TZ=America/Los_Angeles

RUN mkdir -p $APP_HOME/code/
WORKDIR $APP_HOME
ENTRYPOINT [ "python"]

我遇到以下错误:

Downloading https://files.pythonhosted.org/packages/71/e7/816030d3b0426c130040bd068be62b9213357ed02896f5d9badcf46d1b5f/email-4.0.2.tar.gz (1.2MB)
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/usr/local/lib/python3.7/site-packages/setuptools/__init__.py", line 18, in <module>
        import setuptools.version
      File "/usr/local/lib/python3.7/site-packages/setuptools/version.py", line 1, in <module>
        import pkg_resources
      File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 36, in <module>
        import email.parser
      File "/tmp/pip-install-1m3cdjov/email/email/parser.py", line 10, in <module>
        from cStringIO import StringIO
    ModuleNotFoundError: No module named 'cStringIO'

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-1m3cdjov/email/
The command '/bin/sh -c pip install oauth2client urllib3 httplib2 email mimetypes apiclient' returned a non-zero code: 1

我已经在Pycharm中成功安装了这些软件包,并且没有任何问题。我不确定如何在docker中修复此问题。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

您没弄错。 cStringIO模块不再存在。只需从pip安装中删除电子邮件模块,因为它已经是python base的一部分,您应该可以使用。

答案 1 :(得分:1)

“ mimetypes”模块也将面临类似的问题。即使这是python base的一部分,您也无需手动安装。另外,您可以将所有必需的模块包含在requirements.txt文件中,并立即安装它们。为此,您需要在运行安装之前将需求文件复制到Docker映像中。

requirements.txt

nibabel
pydicom
matplotlib
pillow
med2image
pandas
xlsxwriter
numpy
boto
boto3
botocore
oauth2client
urllib3
httplib2
apiclient

Dockerfile

FROM alpine
MAINTAINER <abc@gmail.com>
FROM python:3.7

COPY requirements.txt /tmp
WORKDIR /tmp
RUN pip install --upgrade pip && \
    pip install -r requirements.txt