【问题标题】:Django does not find Pillow though installedDjango 虽然安装了但找不到 Pillow
【发布时间】:2019-10-17 19:40:09
【问题描述】:

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

环境

基于 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 时,我注意到我同时拥有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 的外壳中以及 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)

我在 Dockerfile 中添加了jpeg-dev,但不知何故,这似乎还不够。还在挖。感谢您提供任何线索

【问题讨论】:

  • 有什么理由将 Pillow 5.0.0 与 Python 3.7 一起使用?
  • 根据我的阅读,Django 不支持 5.0.0 以上的 Pillow,我使用 Python 3.7 开始了我的项目。哦,刚刚看到Python 3.7 -> Pillow&gt;5.2
  • 我刚刚回滚到python 3.6,同样的问题。我编辑帖子

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    事实证明,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
    

    【讨论】:

      猜你喜欢
      • 2015-12-06
      • 2021-01-11
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多