Unresolved import error with pydev while using from-import-as

喜你入骨 提交于 2020-01-02 05:30:35

问题


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?


回答1:


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)




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/8209819/unresolved-import-error-with-pydev-while-using-from-import-as

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!