4

In Eclipse with PyDev I get an Unresolved import: pilImage error while having this code.

The code works well when executed from inside PyDev or shell, but the IDE is high-lighting me this as an error.

from PIL import Image as pilImage
# do something with pilImage

How can I solve the problem?

sorin
  • 161,544
  • 178
  • 535
  • 806

4 Answers4

3

I think it may be a bit of a misunderstanding on how PIL should be used...

PIL has a rather uncommon packaging, in which the PIL library is added to the PYTHONPATH (and not the directory containing it), so, if you install with easy-install, it'll do something as:

/Lib
/Lib/site-packages
/Lib/site-packages/PIL-1.1.7-py2.6-win32.egg
/Lib/site-packages/PIL-1.1.7-py2.6-win32.egg/Image.py

So, the import that should actually be done is: import Image as pilImage (i.e.: no from PIL in the import).

A reference backing up that this is how the import should be: http://effbot.org/imagingbook/introduction.htm

And in this case, the directory added to the PYTHONPATH should be: "/Lib/site-packages/PIL-1.1.7-py2.6-win32.egg"

Note that your import could work if you renamed the directory /Lib/site-packages/PIL-1.1.7-py2.6-win32.egg to /Lib/site-packages/PIL and just left /Lib/site-packages/ in the PYTHONPATH (in which case you still would need to go to the PyDev interpreter configuration and just press apply so that it finds out that a new PIL package was added to the PYTHONPATH -- note that in this case /Lib/site-packages/PIL should NOT be added to the PYTHONPATH)

Jonathan Root
  • 535
  • 2
  • 14
  • 31
Fabio Zadrozny
  • 24,814
  • 4
  • 66
  • 78
1

Did you install PIL as an egg after installing PyDev? If so, PyDev won't know it's there. Remove and re-add the interpreter to fix this. See this SO question for more.

Community
  • 1
  • 1
elhefe
  • 3,404
  • 3
  • 31
  • 45
0

Sometimes PyDev requires you to restart Eclipse in order to correct the wrong error message. It's often caused when a user writes the import before adding the module.

Peter
  • 2,172
  • 1
  • 15
  • 11
0

Are you sure that your PyDev-configured interpreter knows the PIL-package and it's contents? If you configured your PyDev Python-interpreter before you installed the PIL-packages, it doesn't know anything about it.

  • I'm sure, if it wouldn't be it would fail to run. I suspect that this bug is the cause https://sourceforge.net/tracker/index.php?func=detail&aid=3439666&group_id=85796&atid=577329 – sorin Nov 21 '11 at 10:14
  • does the proposed solution work? because how the interpreter runs has nothing to do how it is configured for eclipse/pydev. pydev requires configuration only for itself. f.e. the autosuggestion-features. if you execute the current script through the pydev-ui, the real interpreter is used. – Thomas Sittig Nov 21 '11 at 12:13