To answer my own question (now that I understand properly).
In Python, you can import:
- modules - single files e.g. something.py; or
- packages - directories containing one or more .py files, and always one file called __init__.py which designates the directory as a package
In this case, the statement:
import PIL
Is really the same as saying "import the __init__.py file in the PIL directory". In this specific case, that __init__.py file does not import or otherwise make available any class or other module called "Image", so my subsequent reference to it in example 2 in my initial post import fails.
In contrast, the statement:
from PIL import Image
is interpreted a bit differently. This is really the same as saying "look in the package directory PIL for a module called Image and import it".
So you can see that the import statement is actually a bit context dependent. It can mean different things in different circumstances.
This is an excellent resource that explains the different ways in which the import statement can function depending on context.
Answer from Lewis on Stack Overflow
» pip install pillow
Import PIL does not work when Pillow is installed via pip
Probelms with Pillow: from PIL import image // modulenotfounderror no module named 'PIL'
How do I install Pillow?
python - ImportError: No module named PIL - Stack Overflow
Videos
I'm coding a music player in python for learning and I see that PIL needs to be used for the "Logo.png" error (which I have the .png file for it). But the "Import PIL" doesn't work and spits out the "couldn't be resolved" error. Even though I have installed Pillow (and upgraded it). I'm using the Tkinter module for this project btw. Another thought I had was bypassing the PIL/Pillow module entirely and creating a more primitive version just to import images. But I haven't found any related instructions on getting python to recognize image files, especially for importing to projects.
In shell, run:
pip install Pillow
Attention: PIL is deprecated, and pillow is the successor.
On some installs of PIL, you must do
import Image
instead of import PIL (PIL is in fact not always imported this way). Since import Image works for you, this means that you have in fact installed PIL.
Having a different name for the library and the Python module is unusual, but this is what was chosen for (some versions of) PIL.
You can get more information about how to use this module from the official tutorial.
PS: In fact, on some installs, import PIL does work, which adds to the confusion. This is confirmed by an example from the documentation, as @JanneKarila found out, and also by some more recent versions of the MacPorts PIL package (1.1.7).