Python 在 Alpine Linux 上安装 Pillow 时“找不到 jpeg 的头文件或库文件”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44043906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:40:00  来源:igfitidea点击:

"The headers or library files could not be found for jpeg" installing Pillow on Alpine Linux

pythondockerdockerfilepillowalpine

提问by Kurt Peek

I'm trying to run Python's Scrapy in a Docker container based on python:alpine. It was working before, but now I'd like to use Scrapy's Image Pipelinewhich requires me to install Pillow.

我正在尝试在基于python:alpine的 Docker 容器中运行 Python 的 Scrapy 。它以前可以工作,但现在我想使用 Scrapy 的图像管道,它需要我安装 Pillow。

As a simplified example, I tried the following 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 libjpeg zlib tiff freetype lcms libwebp tcl openjpeg
RUN pip install Pillow

However, when I try to build this I get an error which contains the following:

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

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

I went through the requirements on https://pillow.readthedocs.io/en/latest/installation.htmland tried to find the corresponding packages for Alpine, although one I couldn't find was libimagequant, so this might be the 'culprit'. Nonetheless, it the traceback and error message seem to be saying that jpegis missing, whereas I have installed openjpeg.

我浏览了https://pillow.readthedocs.io/en/latest/installation.html上的要求,并试图为 Alpine 找到相应的软件包,虽然我找不到一个是libimagequant,所以这可能是“罪魁祸首” '。尽管如此,回溯和错误消息似乎在说jpeg丢失了,而我已经安装了openjpeg.

How can I modify the Dockerfileso that pip install Pillowruns?

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

采纳答案by Kurt Peek

In a comment that appears to have been deleted later, someone pointed me to https://github.com/python-pillow/Pillow/blob/c05099f45c0d94a2a98c3609a96bdb6cf7446627/depends/alpine_Dockerfile. Based on that Dockerfile I modified my own as follows:

在后来似乎已被删除的评论中,有人将我指向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

Now it builds successfully.

现在它构建成功。

回答by rogoro

This worked for me

这对我有用

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

回答by JohnnyQ

I ran into this problem with docker image python:3.6-alpineI solved it by adding these packages apk add jpeg-dev zlib-dev.

我在使用 docker image 时遇到了这个问题,python:3.6-alpine我通过添加这些包解决了这个问题apk add jpeg-dev zlib-dev

回答by Gabbax0r

have you seen this QA? Fail during installation of Pillow (Python module) in Linux

你见过这个QA吗?在 Linux 中安装 Pillow(Python 模块)时失败

they say you have to update your pip and install libjpeg-dev.

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

回答by Will

Just in case anyone else is still struggling like I was you can see the official alpine Dockerfile for Pillow here: https://github.com/python-pillow/docker-images/blob/master/alpine/Dockerfile#L20

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

It states the following dependencies:

它声明了以下依赖项:

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

回答by Sriram

If you're missing libjpeg.sopackage in runtime, try this (alpine linux):

如果您libjpeg.so在运行时缺少软件包,请尝试此操作(alpine linux):

apk add --no-cache jpeg

回答by Pierangelo Orizio

Have you try to create a requirements.txt and insert this in Dockerfile:

您是否尝试创建一个 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/

example of requirements.txt

需求示例.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

回答by Arash Khoeini

Just adding "RUN apk add jpeg-dev" to Dockerfile fixed the problem for me.

只需在 Dockerfile 中添加“RUN apk add jpeg-dev”即可解决我的问题。