0

I am trying to open image with explicit package referncing

import PIL
image = PIL.Image.open(file_path)

Unfortunately, SOMETIMES it complains

AttributeError: module 'PIL' has no attribute 'Image'

The point is in other script it worked for me this way.

UPDATE

Printing PIL.__file__ causes the same exception (but for attribute __file__).

Seeing PIL in debugger shows

enter image description here

Dims
  • 47,675
  • 117
  • 331
  • 600
  • 1
    If this 'sometimes' complains, you almost certainly have a local module or package named PIL that shadows the one you wanted. Catch the exception and print `PIL.__file__` to track it down. – Martijn Pieters Jul 10 '17 at 14:39
  • Do you have any indication as to when it has been successful and when it hasn't? Under what circumstances have these "Sometimes" occurred? – Davy M Jul 10 '17 at 14:39
  • I'm duping you to the canonical 'accidentally masked a library name' post; without further evidence as to when you get this error and the circumstances, we can't otherwise offer any help here. Please try to narrow down the differences between what works and what doesn't, and include the full traceback of your exceptions. – Martijn Pieters Jul 10 '17 at 14:40
  • @MartijnPieters unprobable that is it shadowing, see my update please; so, the syntax itself is correct? – Dims Jul 10 '17 at 14:47
  • @Dims: the syntax is fine. It is *highly* probable it is shadowing, as it depends on your PYTHONPATH (which is partly dynamic) for when what module is loaded. You could also be re-binding the `PIL` name in your code, replacing the reference with something that doesn't have an `Image` attribute, but we can't see this without your code and a proper [MCVE]. I asked you to debug this *when you actually had the exception* (put a `try:..except AttributeError:` around it and inspect the object). – Martijn Pieters Jul 10 '17 at 14:49
  • If your `PIL` object doesn't have a `__file__` attribute you shadowed the name (assigned something else to it). – Martijn Pieters Jul 10 '17 at 14:50
  • Ah, I see that `PIL/__init__.py` doesn't import the submodules, in which case you must do `import PIL.Image`. Your description of 'when it doesn't work' is rather vague, did you mean *sometimes for the same script* or *sometimes in different code*? – Martijn Pieters Jul 10 '17 at 14:52
  • another possible duplicate in that case: [Why does this AttributeError in python occur?](//stackoverflow.com/q/8696322) – Martijn Pieters Jul 10 '17 at 14:52

0 Answers0