The problem with installing PIL using virtualenv or buildout

42,059

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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...

Share:
42,059
Alexander Artemenko
Author by

Alexander Artemenko

I like python, django and linux. Also, I'm a big fan of TDD. Lead software developer, Yandex.

Updated on July 05, 2022

Comments

  • Alexander Artemenko
    Alexander Artemenko almost 2 years

    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?

  • blueyed
    blueyed about 14 years
    Is the author being notified about this, so that it gets fixed on pypi, too?
  • Martijn Pieters
    Martijn Pieters about 14 years
    I've understood that the author isn't interested in fixing this on pypi.
  • tponthieux
    tponthieux over 13 years
    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.
  • David Miller
    David Miller over 13 years
    This worked just fine, but had to manually rm -rf ./eggs/PIL* and then re-run ./bin/buildout. YMMV etc
  • tvon
    tvon over 13 years
    Pillow is a fork of PIL made with the goal of fixing the packaging, it seems to be a drop-in replacement.
  • Martijn Pieters
    Martijn Pieters over 13 years
    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!
  • Dan Abramov
    Dan Abramov about 13 years
    Solved the problem for me. Just make sure you remove the broken PIL first.
  • jmite
    jmite almost 13 years
    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?
  • Martijn Pieters
    Martijn Pieters almost 13 years
    @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.
  • aclark
    aclark over 12 years
    @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.
  • therealmarv
    therealmarv over 12 years
    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. :-)
  • JeromeParadis
    JeromeParadis about 12 years
    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!
  • Randall Hunt
    Randall Hunt almost 12 years
    I don't think this is necessary anymore though -- it works with setup tools now right?
  • aclark
    aclark almost 12 years
    @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.
  • TaiwanGrapefruitTea
    TaiwanGrapefruitTea over 11 years
    on Windows, the pip install needs Visual Studio to compile for the install. Any solution for installing PIL on Windows in a virtualenv?
  • Martijn Pieters
    Martijn Pieters over 11 years
    @user1023033: Use a pre-compiled version; someone maintains several here.
  • TaiwanGrapefruitTea
    TaiwanGrapefruitTea over 11 years
    @MartijnPieters Thanks! Still need to get from .exe to "it works". I'll add some simple instructions in a separate answer below.
  • Deep-B
    Deep-B over 10 years
    I also needed the Python.h file while trying to install PIL in an Ubuntu virtualenv, this fixed it!
  • foresmac
    foresmac about 10 years
    @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?
  • Martijn Pieters
    Martijn Pieters about 10 years
    @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.
  • foresmac
    foresmac about 10 years
    @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.
  • Tomasz Gandor
    Tomasz Gandor over 8 years
    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?
  • aclark
    aclark over 8 years
    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.