使用 from-import-as 时未解决的 pydev 导入错误
在使用 PyDev 的 Eclipse 中,在使用此代码时出现 Unresolved import: pilImage
错误。
当从 PyDev 或 shell 内部执行时,代码运行良好,但 IDE 将其高亮显示为错误。
from PIL import Image as pilImage
# do something with pilImage
我该如何解决这个问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为这可能对如何使用 PIL 有点误解...
PIL 有一个相当不常见的包装,其中 PIL 库被添加到 PYTHONPATH (而不是包含它的目录),所以,如果你安装使用 easy-install,它将执行以下操作:
因此,实际应该完成的导入是:将 Image 作为 pilImage 导入(即:导入中没有来自 PIL)。
支持导入应该如何进行的参考:http://effbot.org/imagingbook/introduction。 htm
在这种情况下,添加到 PYTHONPATH 的目录应该是:“/Lib/site-packages/PIL-1.1.7-py2.6-win32.egg”
请注意,您的如果将目录 /Lib/site-packages/PIL-1.1.7-py2.6-win32.egg 重命名为 /Lib/site-packages/PIL,则导入可以工作> 并将 /Lib/site-packages/ 留在 PYTHONPATH 中(在这种情况下,您仍然需要转到 PyDev 解释器配置,然后按“应用”,以便发现新的 PIL 包已添加到 PYTHONPATH -- 请注意,在这种情况下 /Lib/site-packages/PIL 不应添加到 PYTHONPATH)
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:
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)
你安装PyDev后是否安装了PIL作为鸡蛋?如果是这样,PyDev 将不知道它的存在。删除并重新添加解释器以解决此问题。有关更多信息,请参阅此SO问题。
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.
您确定您的 PyDev 配置解释器知道 PIL 包及其内容吗?
如果您在安装 PIL 包之前配置了 PyDev Python 解释器,它对此一无所知。
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.
有时 PyDev 要求您重新启动 Eclipse 才能更正错误的错误消息。这通常是由于用户在添加模块之前编写导入而引起的。
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.