Opened 8 years ago

Closed 5 years ago

Last modified 5 years ago

#6054 closed (fixed)

PIL import error

Reported by: nesh Owned by: nobody
Component: Validators Version: master
Severity: Keywords: PIL
Cc: nesh@… Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: UI/UX:

Description

I tried to use ImageField but I got a error that PIL is not installed.

Problem is -- I have PIL installed (easy_instal PIL) and it is working ... and after some hunting I found this

try:
  from PIL import Image
except ImportError:
  e.add(opts, '"%s": To use ImageFields, you need to install the Python Imaging Library. Get it at http://www.pythonware.com/products/pil/ .' % f.name)

this assume that PIL is in PIL package but, at least in my system, PIL stuff is in site-packages so import Imaging works.

Temporary I fixed it like this:

try:
  from PIL import Image
except ImportError:
  try:
    import Image
  except ImportError:
     e.add(opts, '"%s": To use ImageFields, you need to install the Python Imaging Library. Get it at http://www.pythonware.com/products/pil/ .' % f.name)

so it works now.

Is this my system weirdness or something else?

Change History (11)

comment:1 Changed 8 years ago by brosner

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

This isn't a Django problem. The PIL documentation shows the use of Image being in PIL so the bug is either with your system or elsewhere.

comment:2 Changed 8 years ago by nesh

It seems that there is something wrong with easy_install which installs PIL in the root namespace, installing from sources fixes this, strange.

comment:3 Changed 6 years ago by haloween

  • Needs tests set
  • Resolution invalid deleted
  • Status changed from closed to reopened
  • Triage Stage changed from Unreviewed to Design decision needed

This isn't a Django problem.

In my opinion, this is a django issue. If there are two options of accessing a module both of them should be supported.
On a shared host if the "non PIL" option is only one avaliable you can't do anything. The fix requires ONLY 3 places updated.

core\files\image.py , core\management\validation.py , forms\fields.py

 try: import Image
 except ImportError: 
 from PIL import Image

comment:4 Changed 6 years ago by russellm

  • Resolution set to invalid
  • Status changed from reopened to closed

from PIL import Image is a completely legal (and documented) way of importing PIL. If this doesn't work, then your version of PIL isn't installed correctly. If PIL isn't installed correctly, Django can't be expected to fix up the mess. The fix here is for you to fix your PIL install.

comment:5 Changed 6 years ago by rbanffy

  • Keywords PIL added

The PIL docs are ambiguous. In the tutorial, it uses "import Image". I would guess this is a bug in the tutorial, anyway, but I guess we should warn them.

comment:6 Changed 6 years ago by rbanffy

Or, perhaps, the error message given on validate could warn about this issue until it's solved.

comment:7 Changed 6 years ago by junkafarian

In the mean time, a quick fix which saves you screwing around with multiple core django files and will allow both recognised PIL import behaviours:

>>> import Image

and

>>> from PIL import Image

is to write a PIL.py file in the egg directory which imports the necessary PIL modules. For Django which requires "Image" and "ImageFile" as pointed out by haloween the following command should suffice:

junkafarian:~ junkafarian$ echo "import Image, ImageFile" >> /path/to/env/site-packages/PIL-[version].egg/PIL.py

The major problem with this hack is that you will need to include all the modules you require for the project in the import statement (which may be more than just "Image" and "ImageFile")

comment:8 Changed 5 years ago by hozn

  • Resolution invalid deleted
  • Status changed from closed to reopened

(As other have noted) installing the latest (1.1.7) version of PIL using easy_install results in the from PIL import Image failing (import Image works).

While I agree that this is probably a PIL packaging problem (or ambiguous documentation on their part), from the perspective of making Django "just work", it (Django) should really try both imports. This is a trivial change and will make people who want to use setuptools to install Django and dependencies far less frustrated. The current limitation means that you can't just create a setup.py for your project with deps on Django and PIL and expect it to actually work. For me that's enough to prohibit use of ImageField.

comment:9 Changed 5 years ago by Keryn Knight

Confirming that, under OSX 10.5.8, using easy_install to install either 1.16 or the more recent 1.1.7 release of PIL, django breaks in a couple of ways. Specifically, I've been unable to use syncdb if I have an ImageField present in any INSTALLED_APPS model. This is because, as others have stated, Django only looks for a PIL package from which it can import the Image module.

This is obviously, in itself, not a Django problem, but a easy_install/PIL combination problem (its worth noting that using pip to install PIL results in a PIL package in site-packages), but it is somewhat of a show stopper for people who might be beginning development, as, to their awareness, they have installed PIL entirely correctly. Whether or not this is likely to affect production scenarios, I don't know, but I support the suggestions & attempts to patch the imports to look for Image et al in both locations outlined in the original ticket.

comment:10 Changed 5 years ago by jacob

  • Resolution set to fixed
  • Status changed from reopened to closed

(In [12429]) Fixed #6054: work around PIL's installation brokeness by detecting either of the two ways it can end up being installed.

comment:11 Changed 5 years ago by jacob

(In [12430]) [1.1.X] Fixed #6054: work around PIL's installation brokeness by detecting either of the two ways it can end up being installed.

Backport of [12429] from trunk.

Note: See TracTickets for help on using tickets.
Back to Top