尽管已安装 Django 找不到 Pillow

Django does not find Pillow though installed

Django 很难找到 Pillow,我不太清楚为什么。

环境

Linux 基于 Alpine 的 Docker 图像,Django 2.2。以下是相关部分:

Docker文件

RUN apk update \
    && apk add --virtual build-deps gcc python3-dev musl-dev jpeg-dev zlib-dev \
    && apk add --no-cache mariadb-dev mariadb-client

# install dependencies
RUN pip install --upgrade pip
RUN pip install pipenv
RUN pip install mysqlclient
COPY ./Pipfile /usr/src/cms/Pipfile
RUN pipenv install --skip-lock --system --dev
RUN apk del build-deps

Pipfile

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
django = "==2.2"
markdown = "==3.1.1"
pillow = "==5.0.0"

[requires]
python_version = "3.6"

问题

当我从容器中 运行 python manage.py runserver 0.0.0.0:8000 时,出现以下错误:

django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:
website.Photo.photo: (fields.E210) Cannot use ImageField because Pillow is not installed.
    HINT: Get Pillow at https://pypi.org/project/Pillow/ or run command "pip install Pillow".

很奇怪因为pip install Pillow给我

Requirement already satisfied: Pillow in /usr/local/lib/python3.7/site-packages (5.4.1)

关于Pillow与PIL的冲突

在查看 /usr/local/lib/python3.7/site-packages 时,我注意到我同时拥有 PILPillow。这是:

  1. 冲突 (Pillow's documentation) 的来源非常具体地说明了需要卸载 PIL
  2. pillow 使用 PIL 这个名字来保持 this discussion?
  3. 中建议的兼容性

根据 i) pip uninstall PIL -> not installed ii) print(PIL.PILLOW_VERSION) -> 5.0.0 在 python 的 shell 中的事实iii) Django 使用 from PIL import Image source,我会选择假设 2。那么如果 Pillow 安装在容器中,为什么 Django 找不到它?

当前路径

>>> from PIL import Image
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/PIL/Image.py", line 58, in <module>
    from . import _imaging as core
ImportError: Error loading shared library libjpeg.so.8: No such file or directory (needed by /usr/local/lib/python3.7/site-packages/PIL/_imaging.cpython-37m-x86_64-linux-gnu.so)

我在 Docker 文件中添加了 jpeg-dev,但不知何故,它似​​乎还不够。还在挖。感谢任何线索

事实证明,jpeg-dev(编译要求)不足以满足执行期间的所有依赖项。添加 libjpeg 解决了这个问题。更新了 Dockerfile

# install mysqlclient
RUN apk update \
    && apk add --virtual build-deps gcc python3-dev musl-dev jpeg-dev zlib-dev \
    && apk add --no-cache mariadb-dev mariadb-client

# install dependencies
RUN pip install --upgrade pip
RUN pip install pipenv
RUN pip install mysqlclient
RUN apk add libjpeg      -------------> et voila
COPY ./Pipfile /usr/src/cms/Pipfile
RUN pipenv install --skip-lock --system --dev
RUN apk del build-deps

就我而言,因为我使用的是 Docker,所以我必须添加 RUN python -m pip install PillowRUN pip install -r ./requirements/prod.txt 之前进入我的 Docker 文件。 只需在终端中 运行 python -m pip install Pillow 不行。