1

I am working with numpy and Pillow (replacement for PIL in 3.4), but have been having problems with the importing of Pillow. I found a similar post here: ImportError: Cannot import name X

However, this post was using his own created libraries and the problem was that his modules imported each other, creating circular dependent imports.

My code however does not use my own module, here is the code:

import PIL
from PIL import ImageGrab
import numpy

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

size = img.size()

And this then returns the error:

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'

One other interesting thing about this is that when I first installed Pillow (PIL), I tried it in the shell and the "from PIL import ImageGrab" worked.

Also, if I restart the shell (close it and re-open it) the commands, being manually typed in, work as well. This suggests to me that something is bugging out with python, since retyping "import PIL" throws the same error message "cannot import name 'ImageGrab'".

Thanks for any help

Community
  • 1
  • 1
Matthew Winfield
  • 827
  • 4
  • 10
  • 25

1 Answers1

1

Ha, this already bit me multiple times.

Your traceback shows the file name:

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

Your PIL.py is found first, so you're trying to import names from the module that's just executing, not from the actual library you have installed.

Kos
  • 70,399
  • 25
  • 169
  • 233
  • Sp how do I get it to import the actual module instead? Rename the file? – Matthew Winfield Mar 27 '15 at 21:08
  • Rename the file if it's a script, or put it in a subpackage like `myproject.PIL`. Or you could [mess with `sys.path`](https://docs.python.org/2/tutorial/modules.html#the-module-search-path) – Kos Mar 27 '15 at 21:11