简体   繁体   中英

Python Pillow (or PIL) not working despite PIP installation

I am following the documentation: https://pillow.readthedocs.io/en/stable/

I successfully installed Pillow with pip. However, when I try to import the Image function I can:

a) Only import it from PIL b) Only get an error that there is no module PIL c) Get an error that there is no module Pillow

Here's the message (the same thing happens when I try it with Pillow instead of PIL):

from PIL import Image
ModuleNotFoundError: No module named 'PIL'

And here's proof that I have installed it with pip:

Requirement already satisfied: Pillow in c:\python310\lib\site-packages (8.4.0)

I read on Stackoverflow that PIL is no longer maintained. However, no question answered my problem: ImportError: No module named PIL

You have probably installed Pillow in a different Python installation from the one you are using. pip and python are linked to each other. Let's establish what commands you are using first.


Q1 - What command do you use to run Python scripts?

What command do you use when you start Python scripts? Or, if you use a graphical IDE, eg VS Code , JetBeans , IDLE , what command does it use to start Python?

Likely answers:

  • python
  • python-2.7
  • python3

So, if the answer is python3 , then in the remainder of the answer YOURPYTHON will be python3 .


Q2 - What command do you use to install Python packages?

What command do you use to install Python packages?

Likely answers:

  • pip
  • pip-2.7
  • pip3

So, if the answer is pip3 , then in the remainder of the answer YOURPIP will be pip3 .


You have installed with pip into your Python 310 installation, you can see that in your "Requirements satisfied" message, or by running:

YOURPIP -V                # quick version check
YOURPIP show pillow       # more detail as to location
YOURPIP debug             # lots of detail about how pip is set up

Now the question is which Python are you using? And where is it looking?

YOURPYTHON -V                                            # quick version check
YOURPYTHON -c "import sys; print(sys.executable)"        # where is my Python interpreter?
YOURPYTHON -c "import sys; print('\n'.join(sys.path))"   # where does it look for packages?

In general, if you use python , you should probably use pip . But if you use python3 , you should probably use pip3 to ensure that you are installing into the same place that your interpreter is looking.

You can get information about what is actually running when you type a command like this:

which python    # works on macOS, Linux and Windows
type python     # works on macOS and Linux and is preferable

运行pip install image --user然后它应该可以工作。

import PIL
from PIL import Image

or

import PIL
from PIL import *

This should solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM