"找不到jpeg"的标题或库文件。在Alpine Linux上安装Pillow

时间:2017-05-18 09:31:38

标签: python docker dockerfile pillow alpine

我试图在基于python:alpine的Docker容器中运行Python的Scrapy。它以前工作过,但现在我想使用Scrapy的Image Pipeline,这需要我安装Pillow。

作为简化示例,我尝试了以下Dockerfile

FROM python:alpine
RUN apk --update add libxml2-dev libxslt-dev libffi-dev gcc musl-dev libgcc openssl-dev curl
RUN apk add libjpeg zlib tiff freetype lcms libwebp tcl openjpeg
RUN pip install Pillow

然而,当我尝试构建它时,我收到一个错误,其中包含以下内容:

Traceback (most recent call last):
  File "/tmp/pip-build-ft5yzzuv/Pillow/setup.py", line 744, in <module>
    zip_safe=not debug_build(), )
  File "/usr/local/lib/python3.6/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/usr/local/lib/python3.6/distutils/dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "/usr/local/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/usr/local/lib/python3.6/site-packages/setuptools/command/install.py", line 61, in run
    return orig.install.run(self)
  File "/usr/local/lib/python3.6/distutils/command/install.py", line 545, in run
    self.run_command('build')
  File "/usr/local/lib/python3.6/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/local/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/usr/local/lib/python3.6/distutils/command/build.py", line 135, in run
    self.run_command(cmd_name)
  File "/usr/local/lib/python3.6/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/local/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/usr/local/lib/python3.6/distutils/command/build_ext.py", line 339, in run
    self.build_extensions()
  File "/tmp/pip-build-ft5yzzuv/Pillow/setup.py", line 545, in build_extensions
    raise RequiredDependencyException(f)
__main__.RequiredDependencyException: jpeg

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/tmp/pip-build-ft5yzzuv/Pillow/setup.py", line 756, in <module>
    raise RequiredDependencyException(msg)
__main__.RequiredDependencyException:

The headers or library files could not be found for jpeg,
a required dependency when compiling Pillow from source.

Please see the install instructions at:
   https://pillow.readthedocs.io/en/latest/installation.html

我完成了对https://pillow.readthedocs.io/en/latest/installation.html的要求并尝试找到Alpine的相应软件包,虽然我找不到的是 libimagequant ,所以这可能是&# 39;罪魁祸首&#39 ;.尽管如此,追溯和错误消息似乎在说jpeg缺失,而我已经安装了openjpeg

如何修改Dockerfile以便pip install Pillow运行?

10 个答案:

答案 0 :(得分:38)

这对我有用

sudo apt-get install libjpeg-dev zlib1g-dev
pip install Pillow

答案 1 :(得分:27)

在稍后删除的评论中,有人将我指向https://github.com/python-pillow/Pillow/blob/c05099f45c0d94a2a98c3609a96bdb6cf7446627/depends/alpine_Dockerfile。根据Dockerfile我修改了我自己的如下:

FROM python:alpine
RUN apk --update add libxml2-dev libxslt-dev libffi-dev gcc musl-dev libgcc openssl-dev curl
RUN apk add jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev
RUN pip install Pillow

现在它成功构建。

答案 2 :(得分:5)

你看过这个QA吗? Fail during installation of Pillow (Python module) in Linux

他们说你必须更新你的点子并安装libjpeg-dev。

答案 3 :(得分:3)

我遇到了docker映像python:3.6-alpine的问题,我通过添加这些软件包apk add jpeg-dev zlib-dev解决了这个问题。

答案 4 :(得分:2)

简而言之,这会有所帮助:

RUN apt-get update -qq && apt-get install -y build-essential libsqlite3-dev \
  libpng-dev libjpeg-dev

详细信息:

我对python:3.8-slim-buster图片有相同的错误。 @ pierangelo-orizio提出的解决方案对我有用,但是我只是将其清理到所需的最低限度的包装清单中。这是我的Dockerfile

FROM python:3.8-slim-buster

RUN apt-get update -qq && apt-get install -y build-essential libsqlite3-dev \
    libpng-dev libjpeg-dev

COPY requirements.txt .
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

EXPOSE 8000
VOLUME /usr/src/app
WORKDIR /usr/src/app
CMD python manage.py runserver 0.0.0.0:8000

还有requirements.txt

Django>=2.1,<2.2
wagtail>=2.4,<2.5
django-cors-headers==2.5.3
python-dotenv==0.10.3

答案 5 :(得分:1)

如果在运行时缺少\*软件包,请尝试以下操作(高山linux):

libjpeg.so

答案 6 :(得分:1)

在 python 3.9 上遇到同样的问题。通过将 requirements.txt 中的枕头版本撞到 Pillow>=8.0 来修复它。有一个很好的矩阵,说明哪个枕头版本支持哪个 python 版本 here

enter image description here

答案 7 :(得分:0)

您是否尝试创建一个requirements.txt并将其插入Dockerfile:

    RUN apt-get update -qq && apt-get install build-essential g++ flex bison gperf ruby perl \
  mysql-client \
  libsqlite3-dev libmysqlclient-dev libfontconfig1-dev libicu-dev libfreetype6 libssl-dev \
  libpng-dev libjpeg-dev python libx11-dev libxext-dev -y
 ENV PYTHONUNBUFFERED 1
 RUN mkdir /code
 WORKDIR /code
 ADD requirements.txt /code/
 COPY . /code
 RUN pip install -r requirements.txt
 ADD . /code/

requirements.txt的例子

Django==1.9.7
django-appconf==1.0.1
django-filer==1.2.5
django-filter==0.15.0
django-grappelli==2.8.1
django-image-cropping==1.0.3
django-mptt==0.8.6
django-nested-admin==3.0.10
django-nested-inline==0.3.6
django-polymorphic==0.8.1
django-taggit==0.21.2
django-tinymce==2.4.0
dnspython==1.15.0
easy-thumbnails==2.3
enum34==1.1.2
funcsigs==0.4
idna==2.1
ipaddress==1.0.17
mercurial==3.7.3
mock==1.3.0
mysql-python
ndg-httpsclient==0.4.2
parsedatetime==2.1
pbr==1.8.0
Pillow==3.3.1
psutil==3.4.2
pyasn1==0.1.9
PyICU==1.9.2
pyOpenSSL==16.1.0
pyRFC3339==1.0
python-augeas==0.5.0
python-monkey-business==1.0.0
python2-pythondialog==3.3.0
pytz==2014.10
requests==2.11.1
six==1.10.0
Unidecode==0.4.19
urllib3==1.16
zope.component==4.2.2
zope.event==4.2.0
zope.hookable==4.0.4
zope.interface==4.1.3

答案 8 :(得分:0)

只需在Dockerfile中添加“ RUN apk add jpeg-dev”,就可以解决此问题。

答案 9 :(得分:0)

以防万一其他人仍然像我一样挣扎,您可以在此处查看Pillow的官方高山Dockerfile:https://github.com/python-pillow/docker-images/blob/master/alpine/Dockerfile#L20

它声明以下依赖项:

RUN apk --no-cache add python3 \

                   ...

                   # Pillow dependencies
                   jpeg-dev \
                   zlib-dev \
                   freetype-dev \
                   lcms2-dev \
                   openjpeg-dev \
                   tiff-dev \
                   tk-dev \
                   tcl-dev \
                   harfbuzz-dev \
                   fribidi-dev