导入错误:没有名为 PIL 的模块
- 2025-01-13 08:53:00
- admin 原创
- 121
问题描述:
我在 shell 中使用这个命令来安装 PIL:
easy_install PIL
然后我运行python
并输入:import PIL
。但我收到此错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: No module named PIL
我从来没有遇到过这样的问题,您觉得呢?
解决方案 1:
在 shell 中运行:
pip install Pillow
注意:PIL 已被弃用,pillow是其继任者。
解决方案 2:
在某些 PIL 安装中,你必须执行
import Image
而不是import PIL
(实际上,PIL 并不总是以这种方式导入)。由于import Image
对您有用,这意味着您实际上已经安装了 PIL。
对库和 Python 模块使用不同的名称是不寻常的,但这是(某些版本的)PIL 所选择的名称。
您可以从官方教程中获得有关如何使用该模块的更多信息。
PS:事实上,在某些安装中,import PIL
确实有效,这增加了混乱。正如@JanneKarila发现的那样,文档中的示例证实了这一点,并且一些较新版本的MacPorts PIL包(1.1.7)也证实了这一点。
解决方案 3:
首先安装 Pillow
pip install Pillow
或如下
c:Python35>python -m pip install Pillow
然后在 Python 代码中你可以调用
from PIL import Image
“Pillow 是 PIL(Python 图像处理库)的一个分支,不再维护。但是,为了保持向后兼容性,仍使用旧的模块名称。”从pillow 安装,但“没有名为 pillow 的模块” - python2.7 - Windows 7 - python -m install pillow
解决方案 4:
另一方面,我强烈推荐使用Pillow,它与 PIL 向后兼容,并且维护得更好/可以在较新的系统上运行。
安装完成后你可以
import PIL
或者
from PIL import Image
ETC..
解决方案 5:
有时我在用 Python 运行 Unitest 时会遇到此类错误。解决方案是卸载并在虚拟环境中安装相同的包。
使用此命令:
pip uninstall PIL
和
pip install PIL
如果由于任何原因您收到错误,请在命令开头添加 sudo,然后在按回车键后输入您的密码。
解决方案 6:
这对我而言在 Ubuntu 16.04 上有效:
sudo apt-get install python-imaging
我在Wikibooks上搜索了大约半个小时后找到了这个。
解决方案 7:
您必须使用您的 python 包安装 Image 和 Pillow。
类型
python -m pip install image
或运行命令提示符(在 Windows 中),然后导航到脚本文件夹
cd C:Python27Scripts
然后运行以下命令
pip install image
解决方案 8:
在 Windows 10 上我设法通过以下方式实现:
cd "C:Users<your username>AppDataLocalProgramsPythonPython37-32"
python -m pip install --upgrade pip <-- upgrading from 10.something to 19.2.2.
pip3 uninstall pillow
pip3 uninstall PIL
pip3 install image
之后在 python 中(在我的情况下是 python 3.7)这可以正常工作...
import PIL
from PIL import image
解决方案 9:
我用过:
pip install Pillow
pip 在 Lib\site-packages 中安装了 PIL。当我将 PIL 移至 Lib 时,一切正常。我在 Windows 10 上。
解决方案 10:
安装特定版本:
pip install Pillow
升级枕头
sudo pip3 install --upgrade Pillow
在 Windows 10 中获取依赖项错误使用代码:easy_install 而不是 pip install
easy_install Pillow
使用简易安装进行升级
sudo easy_install --upgrade Pillow
在 OSX 系统上安装模块:使用代码:brew install 而不是 pip install
brew install Pillow
不使用Pip:
sudo apt-get install -y Pillow
在 CentOS7 或 Linux Fedora 上:
yum -y install Pillow
或者在 Fedora 上尝试
sudo dnf install Pillow
如果 Homebrew 在 macOS 上搞砸了你的路径,则命令:
python -m pip install Pillow
适用于 Python3 MacOs Homebrew 螺丝
python3 -m pip install Pillow
从列表中验证模块 MacOs
pip freeze | grep Pillow
在 Anaconda 上执行作为你的 Python 包管理器
conda install -c anaconda Pillow
解决方案 11:
在 Windows 上,尝试检查 PIL 库位置的路径。在我的系统上,我注意到路径是
Python26Libsite-packagespil instead of Python26Libsite-packagesPIL
pil
将文件夹重命名为后PIL
,我就可以加载 PIL 模块了。
解决方案 12:
解决此问题的最干净的方法是按照以下步骤操作。
步骤 1:卸载 PIL 包。
pip uninstall PIL
步骤 2:在 Windows 操作系统上,使用 pip 安装 Pillow,如下所示。对于其他环境,请查看文章No module named PIL
在 Windows 上
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade Pillow
步骤 3:Python 图像库中最重要的类是 Image 类,您可以按照如下所示导入它。
from PIL import Image
im = Image.open("myimage.jpg")
如果成功,此函数将返回一个 Image 对象。现在您可以使用实例属性来检查文件内容:
print(im.format, im.size, im.mode)
#Output: PPM (512, 512) RGB
解决方案 13:
如果你使用 anaconda:
conda install pillow
解决方案 14:
我在 Windows 10 上遇到了同样的问题,这对我有用:
pip 安装 Pillow
解决方案 15:
您需要使用 Python 包安装 Image 和 Pillow。请放心,命令行会为您处理一切。
打
python -m pip 安装镜像
解决方案 16:
使用Pillow代替PIL效果很好
easy_install Pillow
或者
pip install Pillow
解决方案 17:
pip(3) uninstall Pillow
pip(3) uninstall PIL
pip(3) install Pillow
解决方案 18:
这对我有用Ubuntu 20.04
:
pip install image
脚本如下:
import image
解决方案 19:
使用pil代替PIL
from pil import Image
解决方案 20:
在 Windows 上,你需要下载并安装 .exe
https://pypi.python.org/pypi/Pillow/2.7.0
解决方案 21:
我使用 conda-forge 安装了枕头版本 5,这对我来说似乎有效:
conda install --channel conda-forge pillow=5
正常的 conda install pillow 对我来说不起作用。
解决方案 22:
我遇到了同样的问题,我通过检查 pip( pip3 --version
) 的版本解决了这个问题,然后意识到我输入python<uncorrect version> filename.py
的是python<correct version> filename.py
解决方案 23:
我用过:
from pil import Image
而不是
from PIL import Image
对我来说效果很好
祝你一切顺利
解决方案 24:
我找到了一个更简单的解决方案。使用虚拟环境。
pip install Pillow
from PIL import Image
这在 macOS 上对我有用
解决方案 25:
我通过使用 Python 3.11 的终端安装了 Pillow。我的 IDE(PyCharm)设置为 Python 3.12 作为解释器。我更改了它,它就起作用了。
解决方案 26:
我最近安装了 Leap。我试过 openshot,但它没有启动。所以我来到这里,发现有一个建议从终端启动,看看是否有任何错误。
我遇到的错误是error missing mlt
。所以我python-mlt
从 Yast 安装了模块并导入了它,尝试启动,但接下来 openshot 说missing pil.
我按照 Pillow 的建议进行安装,因为 Yast 找不到任何 pil,因此导入了 pil。一切正常,但没有启动并显示Error missing goocanvas
。
我goocanvas
使用 Yast 进行安装,将其导入 python,然后 Openshot 就启动了!!
终端中有很多错误,例如missing Vimeoclient
和很多attributeerrors
。好吧,看看它是否对使用它有任何影响。
解决方案 27:
Windows 10 上的 Python 3.8。这些答案的组合对我有用。请参阅下面的独立工作示例。注释掉的行应该在命令行中执行。
import requests
import matplotlib.pyplot as plt
# pip uninstall pillow
# pip uninstall PIL
# pip install image
from PIL import Image
url = "https://images.homedepot-static.com/productImages/007164ea-d47e-4f66-8d8c-fd9f621984a2/svn/architectural-mailboxes-house-letters-numbers-3585b-5-64_1000.jpg"
response = requests.get(url, stream=True)
img = Image.open(response.raw)
plt.imshow(img)
plt.show()
解决方案 28:
我遇到了同样的问题并尝试了上面列出的许多解决方案。
然后我记得我安装了多个版本的 Python,并且我使用 PyCharm IDE(这就是我收到此错误消息的地方),所以对我来说解决方案是:
在 PyCharm 中:
转到文件>设置>项目>Python 解释器
点击“+”(安装)
从列表中找到 Pillow 并安装它
希望这对处于类似情况的任何人有所帮助!
解决方案 29:
我在导入 PIL 并进一步导入 ImageTk 和 Image 模块时遇到了同样的问题。我还尝试直接通过 pip 安装 PIL。但没有成功。由于有人建议 PIL 已被弃用,因此,我尝试仅通过 pip 安装枕头。枕头已成功安装,此外,PIL 包是在路径下生成的:python27/Lib/site-packages/。
现在 Image 和 ImageTk 都可以导入了。
解决方案 30:
根据官方网站Install Pillow 的说明,你可能想尝试一下:
进入终端并运行:
python3 -m pip install --upgrade pip
然后运行
source ~/.bash_profile