Python - 无法导入名称 - 3.4

Python - Cannot import name - 3.4

我正在使用 numpy 和 Pillow(3.4 中 PIL 的替代品),但在导入 Pillow 时遇到了问题。我在这里找到了类似的 post: ImportError: Cannot import name X

然而,这个 post 使用他自己创建的库,问题是他的模块相互导入,创建循环依赖导入。

然而我的代码不使用我自己的模块,这里是代码:

import PIL
from PIL import ImageGrab
import numpy

img = ImageGrab.grab()
imgLoad = img.load()

size = img.size()

然后 returns 错误:

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 中尝试了它并且 "from PIL import ImageGrab" 工作了。

此外,如果我重新启动 shell(关闭它并重新打开它),手动输入的命令也可以正常工作。这向我表明 python 出了问题,因为重新输入 "import PIL" 会抛出相同的错误消息 "cannot import name 'ImageGrab'".

感谢您的帮助

哈,这已经咬我好几次了。

您的回溯显示文件名:

E:/Family Documents/Matthew's Documents/Python/PIL.py

首先找到您的 PIL.py,因此您试图从正在执行的模块导入名称,而不是从您安装的实际库导入名称。