我正在关注文档: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
最佳答案
您可能在与您使用的 Python 安装不同的 Python 安装中安装了 Pillow。 pip
和 python
相互链接。让我们先确定您使用的是什么命令。
Q1 - 您使用什么命令来运行 Python 脚本?
您在启动 Python 脚本时使用什么命令?或者,如果您使用图形 IDE,例如VS Code、JetBeans、IDLE,它使用什么命令来启动 Python?
可能的答案:
- python
- python-2.7
- python3
所以,如果答案是 python3
,那么在答案的其余部分中,YOURPYTHON
将是 python3
。
Q2 - 你使用什么命令来安装 Python 包?
你使用什么命令来安装 Python 包?
可能的答案:
- 点
- pip-2.7
- pip3
因此,如果答案是 pip3
,那么在答案的其余部分中,YOURPIP
将是 pip3
。
您已将 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
关于尽管安装了 PIP,Python Pillow(或 PIL)仍无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69822301/