gpt4 book ai didi

docker - docker 容器中 "No module named PIL"之后的 "RUN pip3 install Pillow"; dist-packages 目录中既不存在 PIL 也不存在 Pillow

转载 作者:行者123 更新时间:2023-12-02 19:23:12 25 4
gpt4 key购买 nike

我正在遵循这个 SageMaker 指南并使用 1.12 cpu docker 文件。
https://github.com/aws/sagemaker-tensorflow-serving-container

如果我使用 requirements.txt 文件安装 Pillow,我的容器在本地运行良好,但是当我部署到 SageMaker 时,“pip3 安装”失败并出现错误,表明我的容器无法访问互联网。

为了解决这个问题,我尝试在部署到 SageMaker 之前在我的容器中安装 Pillow。

当我在 docker 文件中包含“RUN pip3 install Pillow”和“RUN pip3 show Pillow”行时,在构建时,我看到输出显示“Successfully installed Pillow-6.2.0”,并且 show 命令指示 lib 安装在/usr/local/lib/python3.5/dist-packages。同样在 docker 文件中运行“RUN ls/usr/local/lib/python3.5/dist-packages”会在 dist-packages 中显示“PIL”和“Pillow-6.2.0.dist-info”,并且 PIL 目录包括许多代码文件。

但是,当我在本地运行容器时,尝试使用“from PIL import Image”在 python 中导入会导致错误“No module named PIL”。我已经尝试过像“import Image”这样的变体,但是当我启动容器时,PIL 似乎没有安装在代码运行的上下文中。

在“from PIL import Image”行之前,我添加了“import subprocess”和“print(subprocess.check_output("ls/usr/local/lib/python3.5/dist-packages".split()))”

这个 ls 输出与我在 docker 文件中运行它时得到的结果相匹配,除了缺少“PIL”和“Pillow-6.2.0.dist-info”。为什么当我运行 docker 文件时这两个在/usr/local/lib/python3.5/dist-packages 中,但在本地启动容器时却没有?

有没有更好的方法将 Pillow 包含在我的容器中?引用的 Github 页面还显示我可以通过包含文件(在模型包的代码/lib 中)来部署库,但要获得与 Ubuntu 16.04 兼容的文件(docker 容器使用;我在 Mac 上),我'在我的 docker 文件中运行“RUN pip3 install Pillow”之后,d 可能会从 docker 容器中复制它们,而且我需要从 docker 容器中获取文件以部署到 docker 容器似乎很奇怪。

我的 docker 文件看起来像:

ARG TFS_VERSION

FROM tensorflow/serving:${TFS_VERSION} as tfs
FROM ubuntu:16.04
LABEL com.amazonaws.sagemaker.capabilities.accept-bind-to-port=true

COPY --from=tfs /usr/bin/tensorflow_model_server /usr/bin/tensorflow_model_server

# nginx + njs
RUN \
apt-get update && \
apt-get -y install --no-install-recommends curl && \
curl -s http://nginx.org/keys/nginx_signing.key | apt-key add - && \
echo 'deb http://nginx.org/packages/ubuntu/ xenial nginx' >> /etc/apt/sources.list && \
apt-get update && \
apt-get -y install --no-install-recommends nginx nginx-module-njs python3 python3-pip python3-setuptools && \
apt-get clean

RUN pip3 install Pillow

# cython, falcon, gunicorn, tensorflow-serving
RUN \
pip3 install --no-cache-dir cython falcon gunicorn gevent requests grpcio protobuf tensorflow && \
pip3 install --no-dependencies --no-cache-dir tensorflow-serving-api

COPY ./ /

ARG TFS_SHORT_VERSION
ENV SAGEMAKER_TFS_VERSION "${TFS_SHORT_VERSION}"
ENV PATH "$PATH:/sagemaker"

RUN pip3 show Pillow
RUN ls /usr/local/lib/python3.5/dist-packages

我已经尝试将 Pillow 安装在与 cython 和其他依赖项相同的行上,但结果是相同的......这些依赖项在容器时都在/usr/local/lib/python3.5/dist-packages 中构建和本地启动容器时,而“PIL”和“Pillow-6.2.0.dist-info”仅在构建容器时出现。

最佳答案

为迟到的回应道歉。

If I use the requirements.txt file to install Pillow, my container works great locally, but when I deploy to SageMaker, 'pip3 install' fails with an error indicating my container doesn't have internet access.



如果不需要限制互联网访问,那么您应该能够通过在 SageMaker Python SDK 中实例化模型类时设置 enable_network_isolation=False 来启用互联网访问,如下所示: https://github.com/aws/sagemaker-python-sdk/blob/master/src/sagemaker/model.py#L85

如果需要限制互联网访问,这意味着您需要事先将依赖项安装在自己的容器中,或者使用您在信件中提到的包装。

我已经复制了您提供的 Dockerfile 并创建了一个图像以作为图像运行,以重现您看到的错误。我无法重现以下引用的错误:

However, when I run my container locally, trying to import in python using "from PIL import Image" results in error "No module named PIL". I've tried variations like "import Image", but PIL doesn't seem to be installed in the context in which the code is running when I start the container.



我创建了一个类似的 Docker 镜像并使用以下命令将其作为容器运行:
docker run -it --entrypoint bash <DOCKER_IMAGE>

从容器中,我启动了一个 Python3 session 并在本地运行以下命令而没有错误:
root@13eab4c6e8ab:/# python3 -s
Python 3.5.2 (default, Oct 8 2019, 13:06:37)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image

您能否提供有关您如何开始 SageMaker 工作的代码?

请仔细检查您创建的 Docker 镜像是否是启动 SageMaker 作业时引用的镜像。

如果有什么我可以澄清的,请告诉我。

谢谢!

关于docker - docker 容器中 "No module named PIL"之后的 "RUN pip3 install Pillow"; dist-packages 目录中既不存在 PIL 也不存在 Pillow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58260409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com