尽管安装了Django,但找不到Pillow

时间:2019-06-01 13:32:07

标签: python django docker python-imaging-library alpine

Django很难找到枕头,但我不确定为什么。

环境

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

Dockerfile

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时,我注意到我同时拥有 PIL Pillow 。是这样:

  1. 冲突Pillow's documentation)的来源非常具体,说明需要卸载PIL
  2. 枕头是否使用{em> PIL 这个名字来维护兼容性,如this discussion中所建议?

基于以下事实:i)pip uninstall PIL-> not installed ii)print(PIL.PILLOW_VERSION)-> 5.0.0在python的外壳中,并且iii)Django使用from PIL import Image { {3}},我会假设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)

我在Dockerfile中添加了jpeg-dev,但是,以某种方式,这似乎还不够。还在挖。感谢您提供任何线索

1 个答案:

答案 0 :(得分:1)

Turns out, jpeg-dev (required by the compilation) was not enough to satisfy all dependencies during execution. Adding libjpeg solved the issue. Updated 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