导入错误:没有名为 PIL 的模块
我在 shell 中使用此命令来安装 PIL:
easy_install PIL
然后运行 python
并输入:import PIL
。但我得到这个错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: No module named PIL
我从来没有遇到过这样的问题,你觉得怎么样?
I use this command in the shell to install PIL:
easy_install PIL
then I run python
and type this: import PIL
. But I get this error:
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: No module named PIL
I've never had such problem, what do you think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
在 shell 中,运行:
注意:PIL 已弃用,pillow 是后继者。
In shell, run:
Attention: PIL is deprecated, and pillow is the successor.
在某些 PIL 安装中,您必须执行此操作
而不是
导入 PIL
(实际上 PIL 并不总是以这种方式导入)。由于import Image
适合您,这意味着您实际上已经安装了 PIL。库和 Python 模块使用不同的名称是不常见的,但这是为(某些版本的)PIL 选择的名称。
您可以从官方教程获取有关如何使用此模块的更多信息。
PS:事实上,在某些安装中,
导入 PIL
确实有效,这增加了混乱。 @JanneKarila 发现,文档中的示例证实了这一点,还有更多MacPorts PIL 包的最新版本 (1.1.7)。On some installs of PIL, you must do
instead of
import PIL
(PIL is in fact not always imported this way). Sinceimport Image
works for you, this means that you have in fact installed PIL.Having a different name for the library and the Python module is unusual, but this is what was chosen for (some versions of) PIL.
You can get more information about how to use this module from the official tutorial.
PS: In fact, on some installs,
import PIL
does work, which adds to the confusion. This is confirmed by an example from the documentation, as @JanneKarila found out, and also by some more recent versions of the MacPorts PIL package (1.1.7).首先使用
或 如下
安装 Pillow然后在 python 代码中您可以调用
“Pillow 是 PIL(Python 成像库)的一个分支,不再维护。但是,为了保持向后兼容性,使用旧的模块名称。”来自pillow已安装,但“没有模块”命名的枕头” - python2.7 - Windows 7 - python -m 安装枕头
At first install Pillow with
or as follows
Then in python code you may call
"Pillow is a fork of PIL, the Python Imaging Library, which is no longer maintained. However, to maintain backwards compatibility, the old module name is used." From pillow installed, but "no module named pillow" - python2.7 - Windows 7 - python -m install pillow
另一方面,我强烈建议使用 Pillow ,它向后兼容 PIL,并且维护得更好/将在较新的系统上工作。
安装完成后,您可以执行
或
等等操作。
On a different note, I can highly recommend the use of Pillow which is backwards compatible with PIL and is better maintained/will work on newer systems.
When that is installed you can do
or
etc..
有时我在 python 中运行 Unitest 时会遇到这种类型的错误。解决方案是在虚拟环境中卸载并安装相同的软件包。
使用此命令:
如果
由于任何原因出现错误,请在命令开头添加 sudo,然后按 Enter 输入密码。
Sometimes I get this type of error running a Unitest in python. The solution is to uninstall and install the same package on your virtual environment.
Using this commands:
and
If for any reason you get an error, add sudo at the beginning of the command and after hitting enter type your password.
这在 Ubuntu 16.04 上对我有用:
我在搜索了大约一半后在 Wikibooks 上找到了这个小时。
This worked for me on Ubuntu 16.04:
I found this on Wikibooks after searching for about half an hour.
你必须使用 python 包安装 Image 和pillow。
键入
或运行命令提示符(在 Windows 中),然后导航到脚本文件夹,
然后运行以下命令
you have to install Image and pillow with your python package.
type
or run command prompt (in windows), then navigate to the scripts folder
then run below command
在Windows 10上我设法到达那里:
之后在Python(在我的例子中是Python 3.7)这工作得很好......
On windows 10 I managed to get there with:
after which in python (python 3.7 in my case) this works fine...
我使用:
并 pip 在 Lib\site-packages 中安装了 PIL。当我将 PIL 移至 Lib 时,一切正常。我使用的是 Windows 10。
I used:
and pip installed PIL in Lib\site-packages. When I moved PIL to Lib everything worked fine. I'm on Windows 10.
安装特定版本:
升级 Pillow
在 Window 10 中出现依赖关系错误
使用代码:easy_install 而不是 pip install
使用 easy install 进行升级
在 OSX 系统上安装模块:
使用代码:brew install而不是pip install
不使用Pip:
在CentOS7或Linux Fedora上:
或者在Fedora上尝试
命令,如果Homebrew搞砸了macOS上的路径:
对于Python3 MacOs Homebrew螺丝
从列表MacOs中验证模块
,以便在Anaconda上作为您的Python执行包管理器
Install Specific Version:
Upgrade Pillow
Getting Dependency Error in Window 10
Use code: easy_install instead of pip install
Upgrade using easy install
On OSX System to install Module:
Use code: brew install instead of pip install
Without Using Pip :
On CentOS7 or Linux Fedora:
Or on Fedora try
Command if Homebrew screws up your path on macOS:
For Python3 MacOs Homebrew screws
Verify module from list MacOs
For Execute on Anaconda as your python package manager
在 Windows 上,尝试检查 PIL 库位置的路径。在我的系统上,我注意到将
pil
文件夹重命名为PIL
后的路径,我能够加载 PIL 模块。
On windows, try checking the path to the location of the PIL library. On my system, I noticed the path was
after renaming the
pil
folder toPIL
, I was able to load the PIL module.解决此问题的最简洁方法是执行以下步骤。
第 1 步:卸载 PIL 包。
步骤 2: 在 Windows 操作系统上使用 pip 安装 Pillow,如下所示。对于其他环境,请查看文章
Windows 上
没有名为 PIL 的模块 第3步: Python 成像Library 是 Image 类,您可以如下所示导入它。
如果成功,该函数返回一个 Image 对象。您现在可以使用实例属性来检查文件内容:
The cleanest way to resolve this issue is by following below steps.
Step 1: Uninstall the PIL package.
Step 2: Install the Pillow using pip as shown below on windows operating systems. For other environments checkout the article No module named PIL
On Windows
Step 3: The most crucial class in the Python Imaging Library is the Image class, and you can import this as shown below.
If successful, this function returns an Image object. You can now use instance attributes to examine the file contents:
如果你使用蟒蛇:
if you use anaconda:
我在 Windows 10 上遇到了同样的问题,这对我有用:
I had the same issue on windows 10 and this worked for me:
您需要使用 python 包安装 Image 和pillow。
请放心,命令行将为您处理一切。
点击
python -m pip install image
You will need to install Image and pillow with your python package.
Rest assured, the command line will take care of everything for you.
Hit
python -m pip install image
而不是 PIL 使用 Pillow 它可以工作
或
instead of PIL use Pillow it works
or
这对我在
Ubuntu 20.04
上有用:在脚本中:
This worked for me on
Ubuntu 20.04
:And in script:
使用 pil 代替 PIL
use pil instead of PIL
在 Windows 上,您需要下载并安装 .exe
https://pypi.python。 org/pypi/Pillow/2.7.0
On Windows, you need to download it and install the .exe
https://pypi.python.org/pypi/Pillow/2.7.0
我使用 conda-forge 安装 Pillow 版本 5,这似乎对我有用:
普通的 conda 安装 Pillow 对我不起作用。
I used conda-forge to install pillow version 5, and that seemed to work for me:
the normal conda install pillow did NOT work for me.
我遇到了同样的问题,我通过检查 pip (
pip3 --version
) 的版本来修复它,然后意识到我正在输入python<不正确的版本>; filename.py
而不是python<正确版本>文件名.py
I had the same problem and i fixed it by checking what version pip (
pip3 --version
) is, then realizing I'm typingpython<uncorrect version> filename.py
instead ofpython<correct version> filename.py
我用的是 :
而不是
,它对我很有用,
祝你一切顺利
I used :
instead of
and it worked for me fine
wish you bests
我找到了一个更简单的解决方案。使用虚拟环境。
这对我在 macOS 上有效
I found an easier solution. Use a virtual environment.
This works for me on a macOS
我通过使用 Python 3.11 的终端安装了 Pillow。我的 IDE (PyCharm) 设置为 Python 3.12 作为解释器。我改变了它并且它起作用了。
I installed Pillow through my terminal which used Python 3.11. My IDE (PyCharm) was set to Python 3.12 as an interpreter. I changed that and it worked.
我最近安装了 Leap。我尝试了 openshot 但它没有启动。所以来到这里,找到了一个建议,从终端开始,看看是否有任何错误。
我遇到的错误是
错误缺少mlt
。因此,我从 Yast 安装了python-mlt
模块并导入它,尝试启动,但下一个 openshot 说missing pil。
我按照 Pillow 建议进行安装,因为 Yast 无法安装找到任何 pil 和导入的 pil。一切顺利,但没有启动,并显示
错误缺少 goocanvas
。我使用 Yast 安装了 goocanvas,将其导入到 python 中,然后 Openshot 启动了!
终端中出现很多错误,例如
缺少 Vimeoclient
和大量attributeerrors
。好吧,看看它的工作是否有任何影响。I recently installed Leap. I Tried openshot and it didn't start. So came here and found a suggestion to start from the Terminal to see if there were any error.
The error I had was
error missing mlt
. So I installed thepython-mlt
module from Yast and imported it, tried to start but next openshot saidmissing pil.
I Followed the Pillow suggestion to install because Yast couldn't find any pil and imported pil. That went ok but did not start and showed
Error missing goocanvas
.The I installed
goocanvas
with Yast, imported it in python, and Openshot fired up !!With a lot of errors in the terminal like
missing Vimeoclient
and lots ofattributeerrors
. Well, will see if it is of any influence working with it.Windows 10 上的 Python 3.8。答案的组合对我有用。请参阅下面的独立工作示例。注释掉的行应该在命令行中执行。
Python 3.8 on Windows 10. A combination of the answers worked for me. See below for a standalone working example. The commented out lines should be executed in the command line.
我遇到了同样的问题并尝试了上面列出的许多解决方案。
然后我记得我安装了多个版本的 Python 并且我使用 PyCharm IDE(这是我收到此错误消息的地方),所以我的情况的解决方案是:
在 PyCharm 中:
转到 File> ;设置>项目>Python解释器
单击“+”(安装)
从列表中找到Pillow并安装它
希望这对可能遇到类似情况的人有所帮助!
I had the same issue and tried many of the solutions listed above.
I then remembered that I have multiple versions of Python installed AND I use the PyCharm IDE (which is where I was getting this error message), so the solution in my case was:
In PyCharm:
go to File>Settings>Project>Python Interpreter
click "+" (install)
locate Pillow from the list and install it
Hope this helps anyone who may be in a similar situation!
我在导入 PIL 并进一步导入 ImageTk 和 Image 模块时遇到了同样的问题。我也尝试过直接通过pip安装PIL。但未能取得成功。在这之间,有人建议 PIL 已被弃用,因此,尝试仅通过 pip 安装枕头。枕头安装成功,并且在路径:python27/Lib/site-packages/下制作了PIL包。
现在 Image 和 ImageTk 都可以导入。
I had the same issue while importing PIL and further importing the ImageTk and Image modules. I also tried installing PIL directly through pip. but not success could be achieved. As in between it has been suggested that PIL has been deprectaed, thus, tried to install pillow through pip only. pillow was successfully installed, further, the PIL package was made under the path : python27/Lib/site-packages/.
Now both Image and ImageTk could be imported.
根据官网安装Pillow,你可能想试试这个:
去到终端并运行:
python3 -m pip install --upgrade pip
上运行
source ~/.bash_profile
According to the official websiteInstall Pillow, you may want to try this:
go to Terminal and run:
python3 -m pip install --upgrade pip
Then Run on
source ~/.bash_profile