导入错误:没有名为 PIL 的模块

发布于 2024-12-26 19:40:26 字数 322 浏览 5 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

一生独一 2025-01-02 19:40:26

在 shell 中,运行:

pip install Pillow

注意:PIL 已弃用,pillow 是后继者。

In shell, run:

pip install Pillow

Attention: PIL is deprecated, and pillow is the successor.

多孤肩上扛 2025-01-02 19:40:26

在某些 PIL 安装中,您必须执行此操作

import Image

而不是导入 PIL(实际上 PIL 并不总是以这种方式导入)。由于 import Image 适合您,这意味着您实际上已经安装了 PIL。

库和 Python 模块使用不同的名称是不常见的,但这是为(某些版本的)PIL 选择的名称。

您可以从官方教程获取有关如何使用此模块的更多信息。

PS:事实上,在某些安装中,导入 PIL 确实有效,这增加了混乱。 @JanneKarila 发现,文档中的示例证实了这一点,还有更多MacPorts PIL 包的最新版本 (1.1.7)。

On some installs of PIL, you must do

import Image

instead of import PIL (PIL is in fact not always imported this way). Since import 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).

榕城若虚 2025-01-02 19:40:26

首先使用

pip install Pillow

或 如下

c:\Python35>python -m pip install Pillow

安装 Pillow然后在 python 代码中您可以调用

from PIL import Image

“Pillow 是 PIL(Python 成像库)的一个分支,不再维护。但是,为了保持向后兼容性,使用旧的模块名称。”来自pillow已安装,但“没有模块”命名的枕头” - python2.7 - Windows 7 - python -m 安装枕头

At first install Pillow with

pip install Pillow

or as follows

c:\Python35>python -m pip install Pillow

Then in python code you may call

from PIL import Image

"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

2025-01-02 19:40:26

另一方面,我强烈建议使用 Pillow ,它向后兼容 PIL,并且维护得更好/将在较新的系统上工作。

安装完成后,您可以执行

import PIL 

from PIL import Image

等等操作。

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

import PIL 

or

from PIL import Image

etc..

情愿 2025-01-02 19:40:26

有时我在 python 中运行 Unitest 时会遇到这种类型的错误。解决方案是在虚拟环境中卸载并安装相同的软件包。

使用此命令:

pip uninstall PIL

如果

pip install  PIL 

由于任何原因出现错误,请在命令开头添加 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:

pip uninstall PIL

and

pip install  PIL 

If for any reason you get an error, add sudo at the beginning of the command and after hitting enter type your password.

习ぎ惯性依靠 2025-01-02 19:40:26

这在 Ubuntu 16.04 上对我有用:

sudo apt-get install python-imaging

我在搜索了大约一半后在 Wikibooks 上找到了这个小时。

This worked for me on Ubuntu 16.04:

sudo apt-get install python-imaging

I found this on Wikibooks after searching for about half an hour.

牵强ㄟ 2025-01-02 19:40:26

你必须使用 python 包安装 Image 和pillow。

键入

python -m pip install image 

或运行命令提示符(在 Windows 中),然后导航到脚本文件夹,

cd C:\Python27\Scripts

然后运行以下命令

pip install image

you have to install Image and pillow with your python package.

type

python -m pip install image 

or run command prompt (in windows), then navigate to the scripts folder

cd C:\Python27\Scripts

then run below command

pip install image
静若繁花 2025-01-02 19:40:26

在Windows 10上我设法到达那里:

cd "C:\Users\<your username>\AppData\Local\Programs\Python\Python37-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

On windows 10 I managed to get there with:

cd "C:\Users\<your username>\AppData\Local\Programs\Python\Python37-32" 
python -m pip install --upgrade pip     <-- upgrading from 10.something to 19.2.2.
pip3 uninstall pillow
pip3 uninstall PIL
pip3 install image

after which in python (python 3.7 in my case) this works fine...

import PIL
from PIL import image
小情绪 2025-01-02 19:40:26

我使用:

pip install Pillow 

并 pip 在 Lib\site-packages 中安装了 PIL。当我将 PIL 移至 Lib 时,一切正常。我使用的是 Windows 10。

I used:

pip install Pillow 

and pip installed PIL in Lib\site-packages. When I moved PIL to Lib everything worked fine. I'm on Windows 10.

把梦留给海 2025-01-02 19:40:26

安装特定版本:

pip install Pillow

升级 Pillow

sudo pip3 install --upgrade Pillow

在 Window 10 中出现依赖关系错误
使用代码:easy_install 而不是 pip install

easy_install Pillow 

使用 easy install 进行升级

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 

Install Specific Version:

pip install Pillow

Upgrade Pillow

sudo pip3 install --upgrade Pillow

Getting Dependency Error in Window 10
Use code: easy_install instead of pip install

easy_install Pillow 

Upgrade using easy install

sudo easy_install --upgrade  Pillow

On OSX System to install Module:
Use code: brew install instead of pip install

brew install Pillow 

Without Using Pip :

 sudo apt-get install -y Pillow 

On CentOS7 or Linux Fedora:

yum -y install Pillow 

Or on Fedora try

sudo dnf install Pillow 

Command if Homebrew screws up your path on macOS:

python -m pip install Pillow 

For Python3 MacOs Homebrew screws

python3 -m pip install Pillow

Verify module from list MacOs

pip freeze | grep  Pillow

For Execute on Anaconda as your python package manager

 conda install -c anaconda Pillow 
岛歌少女 2025-01-02 19:40:26

在 Windows 上,尝试检查 PIL 库位置的路径。在我的系统上,我注意到将 pil 文件夹重命名为 PIL 后的路径

\Python26\Lib\site-packages\pil instead of \Python26\Lib\site-packages\PIL  

,我能够加载 PIL 模块。

On windows, try checking the path to the location of the PIL library. On my system, I noticed the path was

\Python26\Lib\site-packages\pil instead of \Python26\Lib\site-packages\PIL  

after renaming the pil folder to PIL, I was able to load the PIL module.

梦情居士 2025-01-02 19:40:26

解决此问题的最简洁方法是执行以下步骤。

第 1 步:卸载 PIL 包。

pip uninstall PIL

步骤 2: 在 Windows 操作系统上使用 pip 安装 Pillow,如下所示。对于其他环境,请查看文章

Windows 上

python3 -m pip install --upgrade pip
python3 -m pip install --upgrade Pillow

没有名为 PIL 的模块 第3步: Python 成像Library 是 Image 类,您可以如下所示导入它。

from PIL import Image
im = Image.open("myimage.jpg")

如果成功,该函数返回一个 Image 对象。您现在可以使用实例属性来检查文件内容:

print(im.format, im.size, im.mode)

#Output: PPM (512, 512) RGB

The cleanest way to resolve this issue is by following below steps.

Step 1: Uninstall the PIL package.

pip uninstall PIL

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

python3 -m pip install --upgrade pip
python3 -m pip install --upgrade Pillow

Step 3: The most crucial class in the Python Imaging Library is the Image class, and you can import this as shown below.

from PIL import Image
im = Image.open("myimage.jpg")

If successful, this function returns an Image object. You can now use instance attributes to examine the file contents:

print(im.format, im.size, im.mode)

#Output: PPM (512, 512) RGB
挥剑断情 2025-01-02 19:40:26

如果你使用蟒蛇:

conda install pillow

if you use anaconda:

conda install pillow
老旧海报 2025-01-02 19:40:26

我在 Windows 10 上遇到了同样的问题,这对我有用:

pip 安装 Pillow

I had the same issue on windows 10 and this worked for me:

pip install Pillow

我是男神闪亮亮 2025-01-02 19:40:26

您需要使用 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

怀里藏娇 2025-01-02 19:40:26

而不是 PIL 使用 Pillow 它可以工作

easy_install Pillow

pip install Pillow

instead of PIL use Pillow it works

easy_install Pillow

or

pip install Pillow
只想待在家 2025-01-02 19:40:26
pip(3) uninstall Pillow
pip(3) uninstall PIL
pip(3) install Pillow
pip(3) uninstall Pillow
pip(3) uninstall PIL
pip(3) install Pillow
鼻尖触碰 2025-01-02 19:40:26

这对我在 Ubuntu 20.04 上有用:

pip install image

在脚本中:

import image

This worked for me on Ubuntu 20.04:

pip install image

And in script:

import image
╰◇生如夏花灿烂 2025-01-02 19:40:26

使用 pil 代替 PIL

from pil import Image

use pil instead of PIL

from pil import Image
无畏 2025-01-02 19:40:26

在 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

噩梦成真你也成魔 2025-01-02 19:40:26

我使用 conda-forge 安装 Pillow 版本 5,这似乎对我有用:

conda install --channel conda-forge pillow=5

普通的 conda 安装 Pillow 对我不起作用。

I used conda-forge to install pillow version 5, and that seemed to work for me:

conda install --channel conda-forge pillow=5

the normal conda install pillow did NOT work for me.

冷情 2025-01-02 19:40:26

我遇到了同样的问题,我通过检查 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 typing python<uncorrect version> filename.py instead of python<correct version> filename.py

心病无药医 2025-01-02 19:40:26

我用的是 :

from pil import Image

而不是

from PIL import Image

,它对我很有用,

祝你一切顺利

I used :

from pil import Image

instead of

from PIL import Image

and it worked for me fine

wish you bests

笨笨の傻瓜 2025-01-02 19:40:26

我找到了一个更简单的解决方案。使用虚拟环境。

pip install Pillow
from PIL import Image

这对我在 macOS 上有效

I found an easier solution. Use a virtual environment.

pip install Pillow
from PIL import Image

This works for me on a macOS

夜灵血窟げ 2025-01-02 19:40:26

我通过使用 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.

橪书 2025-01-02 19:40:26

我最近安装了 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 the python-mlt module from Yast and imported it, tried to start but next openshot said missing 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 of attributeerrors. Well, will see if it is of any influence working with it.

知足的幸福 2025-01-02 19:40:26

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()

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.

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()
归途 2025-01-02 19:40:26

我遇到了同样的问题并尝试了上面列出的许多解决方案。

然后我记得我安装了多个版本的 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!

白云不回头 2025-01-02 19:40:26

我在导入 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.

段念尘 2025-01-02 19:40:26

根据官网安装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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文