391

I am trying to install PIL (the Python Imaging Library) using the command:

sudo pip install pil

but I get the following message:

Downloading/unpacking PIL
  You are installing a potentially insecure and unverifiable file. Future versions of pip will default to disallowing insecure files.
  Downloading PIL-1.1.7.tar.gz (506kB): 506kB downloaded
  Running setup.py egg_info for package PIL
    WARNING: '' not a valid package name; please use only.-separated package names in setup.py
    
Installing collected packages: PIL
  Running setup.py install for PIL
    WARNING: '' not a valid package name; please use only.-separated package names in setup.py
    --- using frameworks at /System/Library/Frameworks
    building '_imaging' extension
    clang -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 -pipe -IlibImaging -I/System/Library/Frameworks/Python.framework/Versions/2.7/include -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _imaging.c -o build/temp.macosx-10.8-intel-2.7/_imaging.o
    unable to execute clang: No such file or directory
    error: command 'clang' failed with exit status 1
    Complete output from command /usr/bin/python -c "import setuptools;__file__='/private/tmp/pip_build_root/PIL/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-AYrxVD-record/install-record.txt --single-version-externally-managed:
    WARNING: '' not a valid package name; please use only.-separated package names in setup.py

running install

running build

.
.
.
.

copying PIL/XVThumbImagePlugin.py -> build/lib.macosx-10.8-intel-2.7

running build_ext

--- using frameworks at /System/Library/Frameworks

building '_imaging' extension

creating build/temp.macosx-10.8-intel-2.7

creating build/temp.macosx-10.8-intel-2.7/libImaging

clang -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 -pipe -IlibImaging -I/System/Library/Frameworks/Python.framework/Versions/2.7/include -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _imaging.c -o build/temp.macosx-10.8-intel-2.7/_imaging.o

unable to execute clang: No such file or directory

error: command 'clang' failed with exit status 1

----------------------------------------
Cleaning up…

Could you please help me to install PIL?

martineau
  • 119,623
  • 25
  • 170
  • 301
user3006710
  • 3,947
  • 2
  • 12
  • 3

21 Answers21

672
  1. Install Xcode and Xcode Command Line Tools as mentioned.
  2. Use Pillow instead, as PIL is basically dead. Pillow is a maintained fork of PIL.

https://pypi.org/project/Pillow/

pip install Pillow

If you have both Pythons installed and want to install this for Python3:

python3 -m pip install Pillow
Alan W. Smith
  • 24,647
  • 4
  • 70
  • 96
FogleBird
  • 74,300
  • 25
  • 125
  • 131
  • 5
    I got a `clang` error on OSX Mavericks when I tried this, but I found that this answer helped with that: http://stackoverflow.com/a/22322645/16959 – Jason Sperske Apr 25 '14 at 21:38
  • 41
    While this is a valid and useful answer, it really doesn't answer the question. For those of us in the process of rehabbing old projects which may require filling a PIL dependency just so we can find the next thing that will break, we actually kind of need to get the actual dependency (and sometimes even need to get a specific version that someone built a hack on). – Namey Dec 02 '14 at 09:34
  • 3
    @Namey Porting existing PIL code to Pillow can be done **easily**. [Port PIL to Pillow](http://pillow.readthedocs.org/en/latest/porting-pil-to-pillow.html) . Pillow is based on the PIL code, and has evolved into a better, modern and more friendly version of PIL. – GiriB Sep 15 '15 at 14:15
  • 10
    @GiriB Thanks for the update. My main point was that there are often cases where you are starting up work on some legacy production code where there are 20 ancient dependencies, of which PIL might be one, and you need to debug something to make a fix in the production code. In that case, you most assuredly *do not* want to have to write ports for code connected to ~10 different libraries on your dev build just to start debugging the one-line error that is occurring in the production environment. Hope this clarifies. – Namey Sep 17 '15 at 23:32
  • I'm trying to use a library which uses `import Image`. Is there a way to get Pillow to provide Image? – AnnanFay Nov 13 '17 at 20:46
63

This works for me:

apt-get install python-dev
apt-get install libjpeg-dev
apt-get install libjpeg8-dev
apt-get install libpng3
apt-get install libfreetype6-dev
ln -s /usr/lib/i386-linux-gnu/libfreetype.so /usr/lib
ln -s /usr/lib/i386-linux-gnu/libjpeg.so /usr/lib
ln -s /usr/lib/i386-linux-gnu/libz.so /usr/lib

pip install PIL  --allow-unverified PIL --allow-all-external
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
madjardi
  • 5,649
  • 2
  • 37
  • 37
  • 9
    On 64 bit platforms, the linked path needs to be different -- e.g. /usr/lib/x86_64-linux-gnu/libfreetype.so . For a more architecture independent solution, create the links like `# ln -s /usr/lib/\`uname -i\`-linux-gnu/libfreetype.so /usr/lib/` – Mark Chackerian May 12 '14 at 14:26
  • Always the same problem for headers of imaging: _imagingft.c:73:31: fatal error: freetype/fterrors.h: No such file or directory #include So you have to install python-imaging throught pip – c24b Nov 09 '14 at 16:53
  • Always a problem in freetype headers like mentionned here [http://askubuntu.com/questions/507459/pil-install-in-ubuntu-14-04-1-lts]? SOlution : cd /usr/include ln -s freetype2 freetype – c24b Nov 09 '14 at 17:03
  • 1
    under ubuntu 14.04 1 more symlink is necessary `sudo ln -s /usr/include/freetype2 /usr/local/include/freetype` – Akasha Dec 18 '14 at 17:18
  • 1
    worked well for me the secret was the --allow-unverified PIL --allow-all-external – imbr May 17 '15 at 17:13
57

It is very simple using apt install use this command to get it done

sudo apt-get install python-PIL

or

sudo pip install pillow

or

sudo easy_install pillow
Jaysheel Utekar
  • 1,171
  • 1
  • 19
  • 37
36

Install

pip install Pillow

Then, Just import in your file like,

from PIL import Image

I am using windows. It is working for me.

NOTE:

Pillow is a functional drop-in replacement for the Python Imaging Library. To run your existing PIL-compatible code with Pillow, it needs to be modified to import the Imaging module from the PIL namespace instead of the global namespace.

i.e. change:

import Image

to:

from PIL import Image

https://pypi.org/project/Pillow/2.2.1/

Chandan Sharma
  • 2,321
  • 22
  • 22
  • 1
    Thanks! When reading the name 'PIL' in `from PIL import Image` I could never guess the name of this library was 'Pillow'. I am also on Windows and your solution worked for me. – Marcos Buarque Jan 14 '20 at 11:28
  • Pillow is a functional drop-in replacement for the Python Imaging Library. To run your existing PIL-compatible code with Pillow, it needs to be modified to import the Imaging module from the PIL namespace instead of the global namespace. I.e. change: ```import Image``` to ```from PIL import Image``` https://pypi.org/project/Pillow/2.2.1/ – Chandan Sharma Jan 14 '20 at 13:04
  • Thank you for the additional bckground information! – Marcos Buarque Jan 15 '20 at 18:52
35

On Mac OS X, use this command:

sudo pip install https://effbot.org/media/downloads/Imaging-1.1.7.tar.gz
m01
  • 9,033
  • 6
  • 32
  • 58
Ryan Lee
  • 383
  • 2
  • 2
35

You should install as described here:

pip install image
gtangil
  • 717
  • 7
  • 7
26

I got the answer from a discussion here:

I tried

pip install --no-index -f http://dist.plone.org/thirdparty/ -U PIL

and it worked.

ismailsunni
  • 1,458
  • 1
  • 24
  • 32
  • 8
    Consider using this: `pip install --no-index -f http://effbot.org/downloads/ -U PIL --trusted-host effbot.org` The reason I used this one is because it is the URL listed by the package and on https://pypi.python.org/pypi/PIL – Tom Myddeltyn May 16 '16 at 12:42
  • 1
    I got a warning to use HTTPS instead of HTTP. After changing the URL it worked great. – AnnanFay Nov 13 '17 at 20:49
  • @TomMyddeltyn That almost worked except I got a compiler error where it can't find X11/Xlib.h – Uncommon Dec 09 '17 at 13:06
  • @Uncommon I'm not sure what the issue is there, sorry. – Tom Myddeltyn Dec 21 '17 at 13:53
12

I take it you're on Mac. See How can I install PIL on mac os x 10.7.2 Lion

If you use homebrew, you can install the PIL with just brew install pil. You may then need to add the install directory ($(brew --prefix)/lib/python2.7/site-packages) to your PYTHONPATH, or add the location of PIL directory itself in a file called PIL.pth file in any of your site-packages directories, with the contents:

/usr/local/lib/python2.7/site-packages/PIL

(assuming brew --prefix is /usr/local).

Alternatively, you can just download/build/install it from source:

# download
curl -O -L http://effbot.org/media/downloads/Imaging-1.1.7.tar.gz
# extract
tar -xzf Imaging-1.1.7.tar.gz
cd Imaging-1.1.7
# build and install
python setup.py build
sudo python setup.py install
# or install it for just you without requiring admin permissions:
# python setup.py install --user

I ran the above just now (on OSX 10.7.2, with XCode 4.2.1 and System Python 2.7.1) and it built just fine, though there is a possibility that something in my environment is non-default.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
KGo
  • 18,536
  • 11
  • 31
  • 47
  • While this explains how to install `pil`, it doesn't do it via `pip` as OP tried initially. I find the abundant use of `brew` around the web these days suboptimal. `brew` will install quite a bit of overhead depending on what you want it to do. so `pip` would probably still be the best choice for python modules. – mknaf Aug 03 '15 at 18:33
11

For Ubuntu, PIL is not working any more. I always get:

No matching distribution found for PIL

So install python-imaging:

sudo apt-get install python-imaging
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aneesh R S
  • 3,807
  • 4
  • 23
  • 35
11

These days, everyone uses Pillow, a friendly PIL fork, over PIL.

Instead of: sudo pip install pil

Do: sudo pip install pillow

$ sudo apt-get install python-imaging
$ sudo -H pip install pillow
Dmitry Pleshkov
  • 1,147
  • 1
  • 13
  • 24
  • I would recommend against ever doing `sudo pip install ...`. That could end up breaking your system's installation of Python, which may affect applications of your system. Try using [Anaconda virtual environments](https://docs.conda.io/projects/conda/en/latest/user-guide/getting-started.html#managing-environments), [venv](https://docs.python.org/3/library/venv.html) or Docker. – n1k31t4 Feb 18 '20 at 16:19
6

I'm having the same problem, but it gets solved with installation of python-dev.

Before installing PIL, run following command:

sudo apt-get install python-dev

Then install PIL:

pip install PIL
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kedar.Aitawdekar
  • 2,364
  • 1
  • 23
  • 25
  • 11
    This did not work for me on Ubuntu 14. `pip install pil` results in `No distributions at all found for PIL`, whether I install python-dev first or not. – aliteralmind Aug 25 '14 at 21:05
5

For CentOS:

yum install python-imaging
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
iimos
  • 4,767
  • 2
  • 33
  • 35
5

I had some errors during installation. Just in case somebody has this too. Despite that I already was sitting under admin user, but not root.

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/PIL'

Storing debug log for failure in /Users/wzbozon/Library/Logs/pip.log

Adding "sudo" solved the problem, with sudo it worked:

~/Documents/mv-server: $ sudo pip install Pillow
Denis Kutlubaev
  • 15,320
  • 6
  • 84
  • 70
5

I tried all the answers, but failed. Directly get the source from the official site and then build install success.

  1. Go to the site http://www.pythonware.com/products/pil/#pil117
  2. Click "Python Imaging Library 1.1.7 Source Kit" to download the source
  3. tar xf Imaging-1.1.7.tar.gz
  4. cd Imaging-1.1.7
  5. sudo python setup.py install
Gryu
  • 2,102
  • 2
  • 16
  • 29
ZhouPeng
  • 59
  • 1
  • 3
3

I nailed it by using sudo port install py27-Pillow

Alexandre Simões
  • 1,229
  • 1
  • 10
  • 12
3

Try this:

sudo pip install PIL --allow-external PIL --allow-unverified PIL
alfonso
  • 884
  • 17
  • 31
  • 2
    While this answer may be correct, please add some explanation. Imparting the underlying logic is more important than just giving the code, because it helps the OP and other readers fix this and similar issues themselves. – CodeMouse92 Oct 10 '15 at 01:04
  • Note that this command timed out for me. Anyway, it will be deprecated in a few weeks. `sudo pip install --allow-external --allow-unverified PIL PIL` may also work for now. – Union find Dec 24 '15 at 01:26
  • 3
    `--allow-external` and `--allow-unverified` are both deprecated. `pip install pillow` is the correct answer. – Dorian Jan 22 '16 at 16:07
  • 1
    The answer I posted was suggested to me by the stderr. So while my answer is now out of date, it wasn't at the time I wrote it. Also, I apologize for my message directed to JasonMc92 (which has since been removed). My tone was out of line, but my sentiment remains. There's something distasteful about trolling the front page and copy/pasting your note about "imparting underlying logic". Perhaps you're badge chasing, and I guess that's fine, but for the sake of consistency, please leave a comment on all the other similar answers to this question that fail to underly logic. – alfonso Jan 28 '16 at 16:39
3

(Window) If Pilow not work try download pil at http://www.pythonware.com/products/pil/

nguyên
  • 5,156
  • 5
  • 43
  • 45
3
  • First you should run this sudo apt-get build-dep python-imaging which will give you all the dependencies that you might need

  • Then run sudo apt-get update && sudo apt-get -y upgrade

  • Followed by sudo apt-get install python-pip

  • And then finally install Pil pip install pillow

2

Search on package manager before using pip. On Arch linux you can get PIL by pacman -S python2-pillow

2

For Ubuntu, you can install PIL using apt install:

For Python 3 use:

sudo apt install python3-pil

For Python 2 use:

sudo apt install python-pil

Where pil should be lowercase as Clarkey252 points out

Ricoter
  • 665
  • 5
  • 17
  • 1
    Does `apt` exist as package manager for macOS? – msoutopico May 11 '21 at 07:17
  • Sorry this is for ubuntu – Ricoter May 11 '21 at 07:24
  • 1
    The tag, the title and the example says "macos". Why are you posting answer about Ubuntu? – Matteo Gaggiano Jun 23 '21 at 07:35
  • 2
    I understand your frustration and you are right, this question page is messed up. There are more “Ubuntu”, “Windows”, “Linux” answers than MacOs on this question. “pillow” and even “image” are answered more often than PIL. NO solution actually does answer this question! However all solutions are based on bash and are often transferrable. The PIL repository is not on pip anyway so the only true answer is “you can’t” – Ricoter Jun 24 '21 at 13:01
  • 1
    @Ricoter ok so, why don't you integrate this message into your aswer as a "you can't BUT on ubuntu you can do this ...., so on a macos environment you can translate it into this... ". It will be much more descriptive and comprehensive as answer the I, as a _guy that's searching for the right answer_, will have achieved my purpose. – Matteo Gaggiano Jul 05 '21 at 13:17
0

There's another Python package tool called conda. Conda is preferred (I believe) over pip when there are libraries that need to install C++ and other bindings that aren't pure Python. Conda includes pip in its installation as well so you can still use pip, but you also get the benefits of conda.

Conda also installs IPython, pil, and many other libraries by default. I think you'll like it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chet
  • 18,421
  • 15
  • 69
  • 113