"The headers or library files could not be found for zlib, a required dependency when compiling Pillow from source"
Asked Answered
S

4

11

I am trying to run a Python program with pyautogui but there's a problem because it is telling me that Pillow is not installed. I tried to install it or upgrade it with commands such as

pip install Pillow --upgrade 

but nothing worked.

On the documentation they say that Pillow doesn't coexist with PIL so we need to uninstall it but I can't because it says that it is not installed.

The error is:

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

and also

Collecting Pillow
  Using cached Pillow-9.2.0.tar.gz (50.0 MB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: Pillow
  Building wheel for Pillow (setup.py) ... error
  error: subprocess-exited-with-error

  × python setup.py bdist_wheel did not run successfully.
Sparoid answered 4/9, 2022 at 10:21 Comment(1)
The error is not exactly about that Pillow is missing, but about the dependencies of zlib (The headers or library files could not be found for zlib), which seems to be one of Pillow's dependencies. You need to be able to install zlib first, to install Pillow.Duffy
S
11

You need zlib-dev. This installs the headers that you need to compile parts of Pillow.

You will likely come across the same message for jpeg; for this, you need the libjpeg-turbo-dev package.

Skirting answered 19/9, 2022 at 21:20 Comment(4)
Good point, but I am not sure which distribution and version you are referring to. For instance, Debian wheezy seems to have different packages.Wigfall
@Wigfall Indeed. These are the package names for much of the debian family. Usually, RPM distros use -devel instead of -dev. Click the links in my answer; pkgs.org shows you the package name across over 50 different distros, which is why I only put in the names for Debian.Skirting
Thanks for clarifying that @vpseg. I hadn’t properly noticed the links.Wigfall
zlib1g-dev and libjpeg-dev on Debian Buster worked - thanksVoracity
S
3

I was facing the same issue on my mac. For me, after running the following commands, it worked:

brew install zlib
brew install jpeg
export LDFLAGS="-L/usr/local/opt/zlib/lib"
export CPPFLAGS="-I/usr/local/opt/zlib/include"
Sled answered 1/4, 2023 at 19:57 Comment(2)
My homebrew used a different folder. I needed export LDFLAGS="-L/opt/homebrew/opt/zlib/lib" and export CPPFLAGS="-I/opt/homebrew/opt/zlib/include". The relevant folder can be found using brew --prefix zlibKinsfolk
This gets things working for me, but with this caveat from brew: zlib is keg-only, which means it was not symlinked into /opt/homebrew, because macOS already provides this software and installing another version in parallel can cause all kinds of trouble.. If macOS already provides the same software, why can't the compiler use it by default?Lili
D
1

While @vpseg answer is 100% correct, I would like to add that if you are using a Docker image, for example python:3.x-slim or python:3.x-alpine, the solution may be to add the following lines to the Dockerfile.

For alpine image:

FROM python:3.12-alpine
RUN apk add build-base zlib-dev jpeg-dev
RUN pip install Pillow~=9.5

For slim image:

FROM python:3.12-slim
RUN apt-get update
RUN apt-get install -y gcc zlib1g-dev libjpeg-dev
RUN pip install Pillow~=9.5
Digitize answered 7/6 at 19:37 Comment(1)
This is great, except it doesn't give you the latest zlib for some reason. It's pinned to the distro. ATM there's a vulnerability in 1.2. I want to upgrade to 1.3 but it just won't...Arbuthnot
F
-4

if you are linux user you can do this

pip install pillow

or

sudo apt install python3-pil
Furey answered 4/9, 2022 at 10:33 Comment(7)
Hello, thank you for your answer, sadly I am on windows and the pip install pillow doesn't work and give me this error... could it be because of my python version ?Sparoid
no its not about your python version, what about pip ? did you installed it, and work fine?Furey
whiche version of python your are using ? 2 or 3Furey
I am using python Python 3.9.13 and the pip version is the most recentSparoid
try this pip install wheelFurey
it is already installed...Sparoid
I'm in the same place! trying to make this project run: github.com/ronheywood/opencv The headers or library files could not be found for zlib, a required dependency when compiling Pillow from source.Hynda

© 2022 - 2024 — McMap. All rights reserved.