70

When I install PIL using easy_install or buildout it installs in such way, that I must do 'import Image', not 'from PIL import Image'.

However, if I do "apt-get install python-imaging" or use "pip -E test_pil install PIL", all work fine.

Here are examples of how I trying to install PIL using virtualenv:

# virtualenv --no-site-packages test_pil
# test_pil/bin/easy_install PIL
# test_pil/bin/python
Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named PIL

I see, that easy_install pack PIL into the Egg, and PIP does not. Same thing with buildbot, it uses eggs.

How could I install PIL properly, using easy_install or buildout?

Alexander Artemenko
  • 21,378
  • 8
  • 39
  • 36

4 Answers4

95

The PIL version packaged on pypi (by the author) is incompatible with setuptools and thus not easy_installable. People have created easy_installable versions elsewhere. Currently, you need to specify a find-links URL and use pip get a good package:

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

By using pip install with the --no-index you avoid running the risk of finding the PyPI (non-fixed) original of PIL. If you were to use easy_install, you must use a direct link to the source tarball of a corrected version; easy_install stubbornly still uses the PyPI link over the find-links URL:

easy_install http://dist.plone.org/thirdparty/PIL-1.1.7.tar.gz

To include PIL in a buildout, either specify the egg with the same version pin or use a versions section:

[buildout]
parts =
find-links =
    http://dist.plone.org/thirdparty/
eggs =
    PIL
versions = versions

[versions]
PIL = 1.1.7

Edit March 2011: Fixes to address the packaging issues have been merged into PIL's development tree now, so this workaround may soon be obsolete.

Edit February 2013: Just use Pillow and be done with it. :-) Clearly waiting for the original package to be fixed has not paid off.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Is the author being notified about this, so that it gets fixed on pypi, too? – blueyed Mar 29 '10 at 23:20
  • I've understood that the author isn't interested in fixing this on pypi. – Martijn Pieters Apr 05 '10 at 15:52
  • 1
    I tried the version shown in your example but I still had the same problem. I ended up using the installer from the pythonware website, then copied the PIL directory and PIL.pth file to the virtualenv, and it solved the problem for me. – tponthieux Feb 07 '11 at 06:45
  • This worked just fine, but had to manually rm -rf ./eggs/PIL* and then re-run ./bin/buildout. YMMV etc – David Miller Feb 07 '11 at 14:47
  • 14
    [Pillow](http://pypi.python.org/pypi/Pillow/1.6) is a fork of PIL made with the goal of fixing the packaging, it seems to be a drop-in replacement. – tvon Mar 11 '11 at 17:19
  • I am not sure how much of a following Pillow will garner. The author of PIL has now committed changes that fix this particular issue, and in what will be 2.0 the non-namespaced imports are even removed. No need to follow a fork if the original project fixed things! – Martijn Pieters Mar 18 '11 at 21:08
  • 1
    Solved the problem for me. Just make sure you remove the broken PIL first. – Dan Abramov Jun 14 '11 at 17:16
  • I have the above problem on Windows 7, but I still get a "package not found" error when I try to import PIL after using the above easy_install command. Any ideas as to why this is happening? – jmite Jun 30 '11 at 20:23
  • @jmite: The Plone.org PIL package is a source distribution only; no Windows egg (compiled package) is provided, which is probably why. I believe pre-built Plone installers do include the egg though, so you could copy it from there perhaps. – Martijn Pieters Jun 30 '11 at 21:57
  • 4
    @MartijnPieters And six months later that change has still not been released, which is a pretty good reason to support the fork IMHO. Further, Pillow basically accomplishes the same thing as the accepted answer above, without having to specify the alternate index. – aclark Oct 14 '11 at 16:09
  • on Windows, the pip install needs Visual Studio to compile for the install. Any solution for installing PIL on Windows in a virtualenv? – TaiwanGrapefruitTea Nov 30 '12 at 04:38
  • @user1023033: Use a pre-compiled version; someone maintains several [here](http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil). – Martijn Pieters Nov 30 '12 at 08:40
  • @MartijnPieters Thanks! Still need to get from .exe to "it works". I'll add some simple instructions in a separate answer below. – TaiwanGrapefruitTea Nov 30 '12 at 09:57
  • @MartijnPieters I'm on Mavericks and getting this error with PNGs using Pillow 2.4.0 in a virtualenv. Any tricks for getting PNGs to save without the zip encoder error in that situation? – foresmac May 29 '14 at 20:53
  • @foresmac: *What* error are you getting? Perhaps you need to post a new question? I'm on OS X 10.9 as well, if you have an easy way to reproduce the error we can take a look. – Martijn Pieters May 29 '14 at 20:57
  • @MartijnPieters I've been in touch with @ aclark about it, and he is helping me sort it out. If anything useful comes of it, I'll be sure to post here or post a new question and answer. – foresmac May 30 '14 at 14:02
79

Use Pillow: the "friendly" PIL fork :-) It offers:

  • Full setuptools compatibility
  • Faster release cycle
  • No image code changes that differ from PIL (i.e. it aims to track all PIL image code changes, and make none of its own changes without reporting them upstream.)
  • Windows binaries

If PIL ever does exactly what Pillow does, then the fork will die. Until that happens, we have Pillow.

DISCLAIMER: I am the fork author, and Pillow was created mainly to make my job easier (although it's great to see other folks using it too).

EDIT: Pillow 2.0.0 was released on March 15, 2013. It offers Python 3 support and many bug fixes/enhancements. While we still attempt to track changes with upstream PIL, (unfortunately or fortunately depending on how you look at it) Pillow has begun to drift away from PIL.

aclark
  • 4,345
  • 1
  • 19
  • 31
  • Thank you for your work! I also tried to get my pyramid buildout working with PIL but now I discovered this discussion and I replaced it with Pillow and it worked. :-) – therealmarv Jan 08 '12 at 18:30
  • 1
    Thanks for Pillow @aclark ! After at least a year of struggling with PIL everytime I build new machines and always having problems with JPEG and PNG support on Ubuntu with virtualenv, buildout and setuptools, I found Pillow! Now, just a setuptools definition and it just works. A thousand thanks! – JeromeParadis Jun 06 '12 at 22:18
  • I don't think this is necessary anymore though -- it works with setup tools now right? – Randall Hunt Jul 24 '12 at 22:09
  • @Ranman That depends on what you mean by "works with setuptools". There hasn't been a new PIL release since the fork was made in 2010, if that helps. – aclark Jul 26 '12 at 00:14
  • The standard way of accessing the `Image` module is to `from PIL import Image`. But in PIL there was also `import Image`, and in many places I find just that. I have Pillow 3.0.0 on Windows and it doesn't support bare `import Image`. How to solve it without hacking the dependent code? – Tomasz Gandor Jan 15 '16 at 10:56
  • You don't solve it, it's intentional; use PIL (or older PIllow) if you need `import Image` else change code to `from PIL import Image`. – aclark Jan 16 '16 at 13:10
8

For Ubuntu I found I needed to to install the C headers package for my python version (2.7)

sudo apt-get install python2.7-dev

Afterwards, pip install pil worked.

yuvilio
  • 3,795
  • 32
  • 35
  • I also needed the Python.h file while trying to install PIL in an Ubuntu virtualenv, this fixed it! – Deep-B Feb 20 '14 at 23:09
6

On Windows, I installed PIL in a virtualenv as follows:

Install PIL in your global python site-packages by executing the .exe from: http://www.pythonware.com/products/pil/

Then, as a "do it yourself-er", copy the PIL.pth file and PIL directory in C:\Python25\Lib\site-packages to your virtualenv site-packages directory. Yeah, python is still a "get your hands dirty" environment...

TaiwanGrapefruitTea
  • 1,045
  • 2
  • 14
  • 25