python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.17k stars 2.22k forks source link

Import "pillow" could not be resolvedPylancereportMissingImports #7237

Closed chuckuntulis closed 1 year ago

chuckuntulis commented 1 year ago

I am sure this is a trivial problem, but I have not been able to figure it out. I have asked around but I must not being a good job of describing the problem.

What did you do? Tried running code in Visual Studio Code

from Pillow import Image 

and I get Import "pillow" could not be resolvedPylancereportMissingImports. I have seen similar errors when trying to use icecream and PySimpleGUI

In Python in Windows PowerShell, I did -

from Pillow import Image
Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'Pillow'

What did you expect to happen? no errors in VSC or running python in the shell

chuckuntulis commented 1 year ago

I then asked ChatGPT to use pillow in a python program (code below). I dropped the code in python in a PowerShell, and it ran as expected and generated images.

When I tried running it in Visual Studio Code, I get the error

Exception has occurred: ModuleNotFoundError
No module named 'PIL'
File "E:\Stuff\PythonStuff\pillowTest2.py", line 2, in <module> from PIL import Image, ImageFilter ModuleNotFoundError: No module named 'PIL'
from PIL import Image, ImageFilter

# Open an image file
image = Image.open('example.jpg')

# Display basic information about the image
print('Image format:', image.format)
print('Image size:', image.size)
print('Image mode:', image.mode)

# Resize the image
resized_image = image.resize((800, 600))

# Convert the image to grayscale
grayscale_image = image.convert('L')

# Apply a blur filter to the image
blurred_image = image.filter(ImageFilter.BLUR)

# Save the modified images
resized_image.save('resized_image.jpg')
grayscale_image.save('grayscale_image.jpg')
blurred_image.save('blurred_image.jpg')

# Show the original and modified images
image.show()
resized_image.show()
grayscale_image.show()
blurred_image.show()
radarhere commented 1 year ago

from Pillow import Image

While this project is called 'Pillow', you should import 'PIL' not 'Pillow'. So from PIL import Image, as ChatGPT has done. See https://pillow.readthedocs.io/en/stable/handbook/tutorial.html for more examples.

Regarding your problem with Visual Studio, https://stackoverflow.com/questions/70795319/no-module-named-pil-visual-studio-code-error looks interesting.

chuckuntulis commented 1 year ago

After trying various things including the stack overflow comment,

If I drop the code in python in the PowerShell, I get

import sys
print(sys.executable)

C:\Program Files\Python311\python.exe

execute only it in VSC I get C:\Users\Charles\AppData\Local\Programs\Python\Python39\python.exe

I keep coming back to the less than useful PS E:\Stuff\PythonStuff> pip install Pillow Defaulting to user installation because normal site-packages is not writeable Requirement already satisfied: Pillow in c:\users\charles\appdata\roaming\python\python311\site-packages (9.5.0)

Why not say what the "normal site-packages" and where to find it, how to change it or how to work around the issue.

I keep running into this dead end. How do I resolve this issue of three different paths? What is the right one?

Thanks Chuck

hugovk commented 1 year ago

See https://pillow.readthedocs.io/en/stable/installation.html#basic-installation and https://snarky.ca/why-you-should-use-python-m-pip/

You have two different Python versions installed, 3.11 and 3.9.

Don't use plain pip install pillow, because you don't know which Python version that is using.

Use the same Python you would use when running a script.

For example, if you want to run:

python3 pillowTest2.py 

Then install with:

python3 -m pip install --upgrade Pillow
chuckuntulis commented 1 year ago

I still must be missing something because I tried that by running python3 -m pip install --upgrade Pillow in the Terminal of VSC but when I try to rerun the code, I get Exception has occurred: ModuleNotFoundError No module named 'PIL' File "E:\Stuff\PythonStuff\pillowTest2.py", line 5, in from PIL import Image, ImageFilter ModuleNotFoundError: No module named 'PIL'

The code continues to run correctly in python in the PowerShell but not in VSC.

chuckuntulis commented 1 year ago

Runnng python3 pillowTest2.py in the VSC Terminal and in the PowerShell work fine, but trying to run the pillowTest2.py code from inside VSC with Start Debugging or Run without Debugging both fail with Exception has occurred: ModuleNotFoundError No module named 'PIL' File "E:\Stuff\PythonStuff\pillowTest2.py", line 5, in from PIL import Image, ImageFilter ModuleNotFoundError: No module named 'PIL'

radarhere commented 1 year ago

Try this - C:\Users\Charles\AppData\Local\Programs\Python\Python39\python.exe -m pip install Pillow

chuckuntulis commented 1 year ago

Great thanks. That worked, If I understand, then using

import sys
print(sys.executable)

tells me where the executable is and where the libs are relative to that.

so the -m pip install Pillow installs Pillow relative to the exe.

Do I have it correct? So it wasn't a VSC issue but an issue with my understanding of how to find the exe and related libraries and pip variations.

Thanks again.

radarhere commented 1 year ago

Sort of. It tells you where the version of Python that you're running is located.

Each version of Python on your computer will have its own libraries, and running -m pip install Pillow on that Python executable will ensure that the library is installed for that Python version.

It was just an issue installing Pillow in a location corresponding to your current copy of Python, yes.

radarhere commented 1 year ago

Was there anything else, or can this issue be closed?

chuckuntulis commented 1 year ago

Please close it.

On Thu, Jun 29, 2023, 1:44 AM Andrew Murray @.***> wrote:

Was there anything else, or can this issue be closed?

— Reply to this email directly, view it on GitHub https://github.com/python-pillow/Pillow/issues/7237#issuecomment-1612645112, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABOKBZHFCNJSFSCWEHJ3MXTXNU56RANCNFSM6AAAAAAZX6DYBA . You are receiving this because you authored the thread.Message ID: @.***>

chuckuntulis commented 1 year ago

Yes. Thanks.

On Thu, Jun 29, 2023, 12:41 AM Andrew Murray @.***> wrote:

Sort of. It tells you where the version of Python that you're running is located.

Each version of Python on your computer will have its own libraries, and running -m pip install Pillow on that Python executable will ensure that the library is installed for that Python version.

It was just an issue installing Pillow in a location corresponding to your current copy of Python, yes.

— Reply to this email directly, view it on GitHub https://github.com/python-pillow/Pillow/issues/7237#issuecomment-1612567502, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABOKBZFLPMQYKD3PSMSWZ6TXNUWRTANCNFSM6AAAAAAZX6DYBA . You are receiving this because you authored the thread.Message ID: @.***>

chuckuntulis commented 1 year ago

I have one suggestion - add more information in the pylance error message to add more information to help the next person.

On Thu, Jun 29, 2023, 9:13 AM Hugo van Kemenade @.***> wrote:

Closed #7237 https://github.com/python-pillow/Pillow/issues/7237 as not planned.

— Reply to this email directly, view it on GitHub https://github.com/python-pillow/Pillow/issues/7237#event-9679122818, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABOKBZCGIJ5I4T6F52ZSMILXNWSRDANCNFSM6AAAAAAZX6DYBA . You are receiving this because you authored the thread.Message ID: @.***>

radarhere commented 1 year ago

If you're talking about

Defaulting to user installation because normal site-packages is not writeable

that error message comes from pip - https://github.com/pypa/pip

If you're talking about

No module named

that comes from Python itself.

Either way, it's not in our power to change those messages.

pylance - https://pypi.org/project/pylance/ - is also not part of Pillow.

chuckuntulis commented 1 year ago

Thanks for the clarification.

On Thu, Jun 29, 2023, 3:15 PM Andrew Murray @.***> wrote:

If you're talking about

Defaulting to user installation because normal site-packages is not writeable

that error message comes from pip - https://github.com/pypa/pip

If you're talking about

No module named

that comes from Python itself.

Either way, it's not in our power to change those messages.

— Reply to this email directly, view it on GitHub https://github.com/python-pillow/Pillow/issues/7237#issuecomment-1613870057, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABOKBZAQ6BGTV2BBICAN3FDXNX47ZANCNFSM6AAAAAAZX6DYBA . You are receiving this because you authored the thread.Message ID: @.***>