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 。这是:基于以下事实:i)
pip uninstall PIL
-> not installed
ii)print(PIL.PILLOW_VERSION)
-> pythont shell 中的5.0.0
,并且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
,但是,以某种方式,这似乎还不够。还在挖。感谢您提供任何线索 最佳答案
事实证明,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
关于python - 尽管安装了Django,但没有找到Pillow,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56407216/