首页 > 解决方案 > 尽管安装了 PIP,Python Pillow(或 PIL)仍无法正常工作

问题描述

我正在关注文档:https ://pillow.readthedocs.io/en/stable/

我用 pip 成功安装了 Pillow。但是,当我尝试导入该Image函数时,我可以:

a) 只从 PIL 导入 b) 只得到没有模块PIL 的错误 c) 得到没有模块的错误Pillow

这是消息(当我尝试使用 Pillow 而不是 PIL 时会发生同样的事情):

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

这是我用 pip 安装它的证据:

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

我在 Stackoverflow 上读到 PIL 不再维护。但是,没有问题回答我的问题: ImportError: No module named PIL

标签: pythonpython-imaging-library

解决方案


您可能已经在与您正在使用的 Python 安装不同的 Python 安装中安装了 Pillow。pip并且python相互关联。让我们先确定您正在使用哪些命令。


Q1 - 你使用什么命令来运行 Python 脚本?

启动 Python 脚本时使用什么命令?或者,如果您使用图形 IDE,例如VS CodeJetBeansIDLE,它使用什么命令来启动 Python?

可能的答案:

  • Python
  • python-2.7
  • 蟒蛇3

所以,如果答案是python3,那么剩下的答案YOURPYTHON就是python3


Q2 - 你使用什么命令来安装 Python 包?

你使用什么命令来安装 Python 包?

可能的答案:

  • 点子
  • 点子2.7
  • 点子3

所以,如果答案是pip3,那么剩下的答案YOURPIP就是pip3


您已将 with 安装pip到 Python 310 安装中,您可以在“满足要求”消息中看到它,或者通过运行:

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

现在的问题是您使用的是哪个 Python?它在哪里寻找?

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?

一般来说,如果您使用python,您可能应该使用pip. 但是如果你使用python3,你可能应该使用它pip3来确保你安装到你的解释器正在寻找的同一个地方。

当您键入如下命令时,您可以获得有关实际运行的信息:

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

推荐阅读