Python 没有名为 PIL 的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38134362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No Module named PIL
提问by Fisher Coder
I know PIL is deprecated, so I installed Pillow instead, and for backward compatibility reason, Pillow is still using PIL as its module name. Here's my pip freezelook like for Pillow: Pillow=3.2.0
我知道 PIL 已被弃用,所以我安装了 Pillow,出于向后兼容性的原因,Pillow 仍然使用 PIL 作为其模块名称。这是我的 Pillow冻结状态: Pillow=3.2.0
Here's how I use it in my code:
这是我在代码中使用它的方式:
import PIL as pillow
from PIL import Image
As someone suggested in a similar post, also, I tried
正如有人在类似帖子中建议的那样,我也尝试过
import PIL
from PIL import Image
Neither works. Both give me this error:
两者都不起作用。两者都给我这个错误:
ImportError: No module named PIL
Also, I tried:
另外,我试过:
import Image
it gives me:
它给了我:
ImportError: No module named Image
Help is really appreciated!
非常感谢帮助!
采纳答案by Signus
As per my comment since it helped you out and answered your problem:
根据我的评论,因为它帮助了您并回答了您的问题:
The issue that you were seeing is that you had pip version 1.5.6, and the version of pip does dictate how packages are unzipped, which ultimately determines whether or not modules are loaded properly.
您看到的问题是您使用的是 pip 版本 1.5.6,而 pip 的版本确实决定了包的解压缩方式,这最终决定了模块是否正确加载。
All that is needed is:
所需要的只是:
pip install --upgrade pip
Which allows pip to upgrade itself.
这允许 pip 自行升级。
Use sudo
if you're on Mac/Linux, otherwise you'll likely need to 'Run as Administrator' on Windows.
sudo
如果您使用的是 Mac/Linux,请使用,否则您可能需要在 Windows 上“以管理员身份运行”。
And voila, you can now properly import the PIL modules:
瞧,您现在可以正确导入 PIL 模块:
Python 2.7.12 (default, Jun 29 2016, 13:16:51)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL
>>> from PIL import Image
>>> Image
<module 'PIL.Image' from '/usr/local/lib/python2.7/site-packages/PIL/Image.pyc'>
回答by Arthur
In my case, on Windows, all I needed to do is to run:
就我而言,在 Windows 上,我需要做的就是运行:
pip install pillow
pip install pillow
回答by Kris Roofe
You can use python -m pip install Pillow
or pip install Pillow
您可以使用python -m pip install Pillow
或pip install Pillow
回答by Nicholas Lie
I'm having this trouble too recently, so let me share my take on this. I'm on Mac OS X Mojave 10.14. I tried running the pip install pillow
so many times blindly without finding deeper understanding as to where my python
searches for its packages.
我最近也遇到了这个麻烦,所以让我分享一下我对此的看法。我在 Mac OS X Mojave 10.14 上。我尝试pip install pillow
盲目地运行了这么多次,却没有更深入地了解我在哪里python
搜索它的包。
I encountered this error when I upgraded from OS X High Sierra to Mojave. My previously working Scrapy project just stopped running properly.
我从 OS X High Sierra 升级到 Mojave 时遇到了这个错误。我以前工作的 Scrapy 项目刚刚停止正常运行。
Note that I didn't use virtualenv
.
请注意,我没有使用virtualenv
.
First, you need to check which python is used by the system. In the Terminal, run
首先,您需要检查系统使用的是哪个python。在终端中,运行
which -a python
You'll get something similar to this:
你会得到类似的东西:
nicholass-mbp:~ nicholaslie$ which -a python
/usr/local/bin/python
/usr/local/bin/python
/usr/bin/python
nicholass-mbp:~ nicholaslie$ which -a python
/usr/local/bin/python
/usr/local/bin/python
/usr/bin/python
This leads me to searching inside the /usr/local/
folder. Turns out there's a folder to where my python
is searching its packages, and that is inside /usr/local/lib/python2.7
(python
) or /usr/local/lib/python3.7
(python3
)
这导致我在/usr/local/
文件夹内进行搜索。原来有一个文件夹,我python
正在搜索它的包,它在/usr/local/lib/python2.7
( python
) 或/usr/local/lib/python3.7
( python3
)里面
What my current pip install pillow
do is installing the PIL
package inside the /usr/local/lib/python2.7/site-packages
directory, but not to /usr/local/lib/python3.7/site-packages
我目前pip install pillow
所做的是PIL
在/usr/local/lib/python2.7/site-packages
目录中安装包,但不是/usr/local/lib/python3.7/site-packages
However, since my project uses Python 3, No module named PIL
error simply tells you that the python3
instance you're using cannot find the PIL
library.
但是,由于我的项目使用 Python 3,No module named PIL
错误只是告诉python3
您正在使用的实例找不到PIL
库。
I used Homebrew for managing my system packages such as node
, python
, etc. So what I did was reinstall python3
with Homebrew with:
我用自制软件管理我的系统软件包,如node
,python
等,所以我所做的就是重新安装python3
与家酿搭配:
brew reinstall python
, then run pip3 install pillow
.
brew reinstall python
,然后运行pip3 install pillow
。
Just ensure that the PIL
package is installed properly in /usr/local/lib/python3.7/site-packages
and you should be good to go. Hope this helps someone.
只需确保该PIL
软件包已正确安装/usr/local/lib/python3.7/site-packages
,您就可以开始使用了。希望这可以帮助某人。
回答by Arkadiusz Cie?liński
OSX
操作系统
sudo easy_install pip
pip install Pillow --user
回答by Baba.S
For me, when I was trying to use PIL in PyCharm, initially the Project Interpreter was not looking at the correct location for Python 3.7. This has been corrected and using PIL works for me now (corrected Interpreter shown in image attached).
对我来说,当我尝试在 PyCharm 中使用 PIL 时,最初项目解释器没有查看 Python 3.7 的正确位置。这已经得到纠正,现在使用 PIL 对我有用(修正后的解释器显示在附图中)。
回答by user3551863
This solution is for pip version 20 and using PyCharm. I'd upgrade pip to version 20.0.2 as pip suggested but I had to get back to version 19
此解决方案适用于 pip 版本 20 并使用 PyCharm。我会按照 pip 的建议将 pip 升级到 20.0.2 版,但我不得不回到 19 版
python -m pip install pip==19.3.1
I also installed Pillow from command line
我还从命令行安装了 Pillow
pip install Pillow
And finally installed Pillow inside PyCharm on Settings -> Project:your-project-name -> Project Interpreter, clicked on + button and installed Pillow.
最后在 PyCharm 中的 Settings -> Project:your-project-name -> Project Interpreter 中安装 Pillow,点击 + 按钮并安装 Pillow。