'No module named PIL' Visual Studio Code Error

I'm having a problem with PIL where vs code says there is no module named PIL when there is. If I run the file without vs code the module imports fine. In the vs code problem tab it says this:

import PIL could not be resolved from source. Pylance(reportmissingmodulesource) 

I know the library is installed because if I do pip install pillow, it says requirement already satisfied.

Things I've tried to fix it: reinstalling python, uninstalling and reinstalling pillow, upgrading pip, installing the PIL library(pip install Pillow-PIL).

None of these things worked so I am out of ideas for things to try. Could someone help me with this?


Solution 1:

This part is important:

If I run the file without vs code the module imports fine

If something like this happens, then you are not running the same python interpreter, because modules are always installed to specific installations of python that you have.

Do the following:

Add to your script the first two lines

import sys
print(sys.executable)

This will print the path to the python executable that is interpreting that script. If you now run this script with and without vs code, it should print two different python paths. Now you can install to the python interpreter that is being used by vs code specifically by typing

/path/to/python/used/by/vs/code/python -m pip install pillow