12

I am not able to install python's PIL module in docker for some reason. Here's a description of what I have:

requirements.txt

Pillow
flask
redis

Dockerfile

FROM python:2.7
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD python app.py

app.py

import PIL

Commands

$ sudo docker build -t web .
Installing collected packages: Pillow, Werkzeug, MarkupSafe, Jinja2, itsdangerous, flask, redis
Successfully installed Jinja2-2.8 MarkupSafe-0.23 Pillow-2.9.0  Werkzeug-0.10.4 flask-0.10.1 itsdangerous-0.24 redis-2.10.3
---> 91dfb38bd480
Removing intermediate container 4e4ca5801814
Step 4 : CMD python app.py
 ---> Running in e71453f2fab6
 ---> d62996658bd6
 Removing intermediate container e71453f2fab6
 Successfully built d62996658bd6

$ sudo docker-compose up

Here's what I get: Output

web_1   |   File "app.py", line 1, in <module>
web_1   |     import PIL
web_1   | ImportError: No module named PIL

I thought maybe adding PIL in requirements.txt would work, but here's what happens when I build

$ sudo docker build -t web .
....
Collecting PIL (from -r requirements.txt (line 1))
Could not find a version that satisfies the requirement PIL (from -r    requirements.txt (line 1)) (from versions: )
Some externally hosted files were ignored as access to them may be  unreliable (use --allow-external PIL to allow).
No matching distribution found for PIL (from -r requirements.txt (line 1))

Any idea what should be done from here?

arbazkhan002
  • 1,083
  • 2
  • 10
  • 18

4 Answers4

10

PIL would be the Python Imaging Library (PIL)
(sometimes, you would need import Image instead of import PIL)

According to "How do I install python imaging library (PIL)?", you would need to install others components as well

sudo apt-get build-dep python-imaging
sudo apt-get install libjpeg62 libjpeg62-dev
pip install PIL

See also a5huynh/scrapyd-playground/Dockerfile for an example using Pillow (Python Imaging Library) dependencies.

(But be aware, as Hugo comments below, that this mixes two modules: PIL and Pillow.
Pillow is a maintained fork and a drop-in replacement of the original, unmaintained PIL, so you shouldn't have both installed at the same time)

RUN apt-get update && apt-get install -y \
        python-dev python-pip python-setuptools \
        libffi-dev libxml2-dev libxslt1-dev \
        libtiff4-dev libjpeg8-dev zlib1g-dev libfreetype6-dev \
        liblcms2-dev libwebp-dev tcl8.5-dev tk8.5-dev python-tk
# Add the dependencies to the container and install the python dependencies
ADD requirements.txt /tmp/requirements.txt
RUN pip install -r /tmp/requirements.txt && rm /tmp/requirements.txt
RUN pip install Pillow

with requirements:

Pillow==2.6.1
Scrapy==0.24.4
Twisted==14.0.2
boto==2.36.0
cffi==0.8.6
characteristic==14.2.0
cryptography==0.6.1
cssselect==0.9.1
lxml==3.4.0
pyOpenSSL==0.14
pyasn1==0.1.7
pyasn1-modules==0.0.5
pycparser==2.10
pymongo==2.8
queuelib==1.2.2
scrapy-mongodb==0.8.0
scrapyd==1.0.1
service-identity==14.0.0
six==1.8.0
w3lib==1.10.0
zope.interface==4.1.1

In 2019 (4 years later), Daniel W. complains that:

the decoders / image processors are still missing which results in error like OSError: decoder tiff_lzw not available

He adds however:

I found out my problem originated from a buggy Pillow version (5.0), it complained about missing tiff stuff but in fact it was not missing.

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • Perfect! Used the configs from a5huynh/scrapyd-playground/Dockerfile. But it worked on a fresh container, not the existing one. Don't know why. – arbazkhan002 Sep 28 '15 at 01:30
  • 1
    A word of warning, this mixes two modules: `PIL` and `Pillow`. Pillow is a maintained fork and a drop-in replacement of the original, unmaintained PIL, so you shouldn't have both installed at the same time. – Hugo Sep 28 '15 at 12:24
  • 1
    @Hugo Good point: I have included your comment in the answer for more visibility. – VonC Sep 28 '15 at 13:13
  • Not helpful in 2019 with `python:3.6.8-slim-jessie`. Everything that is already in the python image (like python itself) is getting reinstalled from Debian repositories but the decoders / image processors are still missing which results in error like `OSError: decoder tiff_lzw not available`. – Daniel W. Jun 15 '19 at 13:22
  • @DanielW. OK. What is missing? What could I add (4 years later) that would improve that answer? – VonC Jun 15 '19 at 13:46
  • @VonC I found out my problem originated from a [buggy Pillow version (5.0)](https://github.com/python-pillow/Pillow/issues/3905), it complained about missing tiff stuff but in fact it was not missing. – Daniel W. Jun 15 '19 at 15:29
  • @DanielW. Well done! I have included your comment at the end of this answer for more visibility. – VonC Jun 15 '19 at 19:35
10

Add RUN apk add zlib-dev jpeg-dev gcc musl-dev in the Dockerfile and then add Pillow in the requirements.txt

0

The docs say "Pillow and PIL cannot co-exist in the same environment. Before installing Pillow, please uninstall PIL." Once that is taken care of, the answer by Nooras Fatima Ansari above solved the issue for me. Based on which docker image you use, you can check the pillow docker images for exact dependencies for your docker image.

Sandeep Patil
  • 872
  • 2
  • 7
  • 12
-1

if you're using docker compose as me just run docker-compose up --build after of add the pillow into requirements.txt