【发布时间】:2015-03-28 03:45:33
【问题描述】:
我正在使用 numpy 和 Pillow(在 3.4 中替代 PIL),但在导入 Pillow 时遇到了问题。我在这里找到了一个类似的帖子: ImportError: Cannot import name X
然而,这篇文章使用的是他自己创建的库,问题是他的模块相互导入,创建循环依赖导入。
但是我的代码没有使用我自己的模块,这里是代码:
import PIL
from PIL import ImageGrab
import numpy
img = ImageGrab.grab()
imgLoad = img.load()
size = img.size()
然后返回错误:
Traceback (most recent call last):
File "E:/Family Documents/Matthew's Documents/Python/PIL.py", line 1, in <module>
import PIL
File "E:/Family Documents/Matthew's Documents/Python\PIL.py", line 2, in <module>
from PIL import ImageGrab
ImportError: cannot import name 'ImageGrab'
另外一个有趣的事情是,当我第一次安装 Pillow (PIL) 时,我在 shell 中尝试了它,并且“从 PIL 导入 ImageGrab”起作用了。
另外,如果我重新启动 shell(关闭它并重新打开它),手动输入的命令也可以正常工作。这向我表明,python 出了问题,因为重新键入“import PIL”会引发相同的错误消息“无法导入名称 'ImageGrab'”。
感谢您的帮助
【问题讨论】:
-
您自己的代码是否在名为“PIL.py”的文件中?您应该选择一个不同的名称。