ImportError:没有名为PIL的模块

问题:ImportError:没有名为PIL的模块

我在外壳中使用以下命令来安装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?


回答 0

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

import Image

而不是import PIL(实际上并非总是以这种方式导入PIL)。由于import Image对您有用,因此实际上您已经安装了PIL。

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

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

PS:实际上,在某些安装上import 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).


回答 1

在外壳中,运行:

pip install Pillow

注意:已弃用PIL,后继枕头

In shell, run:

pip install Pillow

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


回答 2

另一方面,我强烈建议您使用与PIL向后兼容并且可以更好地维护/在较新的系统上使用的Pillow

安装后即可

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..


回答 3

首先安装枕头

pip install Pillow

或如下

c:\Python35>python -m pip install Pillow

然后在python代码中,您可以调用

from PIL import Image

“ Pillow是PIL(Python映像库)的一个分支,不再维护。但是,为了保持向后兼容性,将使用旧的模块名称。” 从枕头安装,但“没有模块命名为枕头”-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


回答 4

有时我会在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.


回答 5

这在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.


回答 6

我用了:

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.


回答 7

在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

回答 8

您必须使用python软件包安装Image和pillow。

类型

python -m pip install image 

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

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

回答 9

如果您使用水蟒:

conda install pillow

if you use anaconda:

conda install pillow

回答 10

在Windows上,尝试检查PIL库位置的路径。在我的系统上,我注意到路径是

\Python26\Lib\site-packages\pil instead of \Python26\Lib\site-packages\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

\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.


回答 11

您将需要使用python软件包安装Image和pillow。放心,命令行将为您处理一切。

击中

python -m pip安装映像

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


回答 12

而不是PIL使用枕头它有效

easy_install Pillow

要么

pip install Pillow

instead of PIL use Pillow it works

easy_install Pillow

or

pip install Pillow

回答 13

在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


回答 14

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

conda install --channel conda-forge pillow=5

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

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.


回答 15

导入PIL并进一步导入ImageTk和Image模块时,我遇到了相同的问题。我也尝试直接通过pip安装PIL。但无法成功。由于介于两者之间,因此已建议不要使用PIL,因此,尝试仅通过点子安装枕头。枕头已成功安装,此外,PIL软件包位于以下路径中:python27 / Lib / site-packages /。

现在可以导入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.


回答 16

我最近安装了Leap。我尝试了openshot,但没有开始。于是来到这里,发现了一个建议从终端开始,看看是否有任何错误。

我的错误是error missing mlt。所以我python-mlt从Yast 安装了模块并导入了它,尝试启动,但是下一个openshot说missing pil.

我按照枕头建议安装,因为Yast找不到任何pil和导入的pil。没关系,但没有开始显示Error missing goocanvas

我安装goocanvas了Yast,用python导入了它,然后Openshot启动了!

随着大量的终端这样的错误的missing 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.


回答 17

我遇到了同样的问题,我通过检查pip(pip3 --version)是什么版本来解决了这个问题,然后意识到我输入的python<uncorrect version> filename.py不是python<correct version> filename.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


回答 18

您可能缺少构建pil的python标头。如果您使用的是ubuntu或类似功能,它将类似于

apt-get install python-dev

You are probably missing the python headers to build pil. If you’re using ubuntu or the likes it’ll be something like

apt-get install python-dev