Thursday, October 6, 2022

[SOLVED] PIL Module not being seen

Issue

I'm using VSCode on a Mac. Python 3.9 In VSCode, Python 3.9.6 64-bit ('venv':venv)

I have successfully created a virtual environment and I'm trying to use the Image Library (PIL)

  • I've gotten Pillow installed in the venv and it shows up with Pip List
  • There is no error showing up (red squiggle) in the editor window when I use from PIL import Image
  • When I run the program, I get ImportError: No module named PIL

This is the code:

from PIL import Image
import matplotlib.pyplot as plt
import numpy as np

image = Image.open('church in munich.jpg')
image2 = Image.open('cool car in italy.jpg')

image.show()

plt.imshow(image)
plt.imshow(image2)

The output results:

Traceback (most recent call last):
  File "/Volumes/NewMacStorage/VSCode Work/PythonWork/main.py", line 1, in <module>
    from PIL import Image
ImportError: No module named PIL

I've included what VSCode looks like:

enter image description here

Anybody know what the problem could be? Thanks in advance.


Solution

After some pointers from people here (thank you), this problem lies in a combination of PATH confusion (i.e. where things are being read from), Virtual Environments being properly set up and active, and pointing VSCode to the right interpreter.

To solve this on my Mac:

  • Exit from VScode. Likely not required but I wanted to eliminate all variables of what was running, etc.
  • Go to a terminal window outside of VSC
  • Set up your virtual environment
  • Activate it
  • From within that space "code ." will launch VSCode
  • When you create a .py file, make sure you are using the proper interpreter on that code which you can see/set/change by going to the command pallet and using the command Python: Select interpreter that is associated with your virtual environment. I named my environment "codeland" so that when I looked at the list of interpreter selections, it was obvious to me. You can use the venv default as well.
  • I opened the terminal window inside VScode and, sure enough, I was in the virtual environment I had set up.

From there, installing and using modules like pillow (PIL) inside my code worked fine.



Answered By - Rick Segal
Answer Checked By - Willingham (WPSolving Volunteer)