ChatGPT解决这个技术问题 Extra ChatGPT

如何在 Mac OS 上使用 pip 安装 PIL?

我正在尝试使用以下命令安装 PIL(Python 成像库):

sudo pip install pil

但我收到以下消息:

Downloading/unpacking PIL
  You are installing a potentially insecure and unverifiable file. Future versions of pip will default to disallowing insecure files.
  Downloading PIL-1.1.7.tar.gz (506kB): 506kB downloaded
  Running setup.py egg_info for package PIL
    WARNING: '' not a valid package name; please use only.-separated package names in setup.py
    
Installing collected packages: PIL
  Running setup.py install for PIL
    WARNING: '' not a valid package name; please use only.-separated package names in setup.py
    --- using frameworks at /System/Library/Frameworks
    building '_imaging' extension
    clang -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 -pipe -IlibImaging -I/System/Library/Frameworks/Python.framework/Versions/2.7/include -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _imaging.c -o build/temp.macosx-10.8-intel-2.7/_imaging.o
    unable to execute clang: No such file or directory
    error: command 'clang' failed with exit status 1
    Complete output from command /usr/bin/python -c "import setuptools;__file__='/private/tmp/pip_build_root/PIL/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-AYrxVD-record/install-record.txt --single-version-externally-managed:
    WARNING: '' not a valid package name; please use only.-separated package names in setup.py

running install

running build

.
.
.
.

copying PIL/XVThumbImagePlugin.py -> build/lib.macosx-10.8-intel-2.7

running build_ext

--- using frameworks at /System/Library/Frameworks

building '_imaging' extension

creating build/temp.macosx-10.8-intel-2.7

creating build/temp.macosx-10.8-intel-2.7/libImaging

clang -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 -pipe -IlibImaging -I/System/Library/Frameworks/Python.framework/Versions/2.7/include -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _imaging.c -o build/temp.macosx-10.8-intel-2.7/_imaging.o

unable to execute clang: No such file or directory

error: command 'clang' failed with exit status 1

----------------------------------------
Cleaning up…

你能帮我安装PIL吗?

你有没有安装 Xcode,然后安装 Xcode 的命令行工具?看起来你没有 clang 编译器。
PIL 存储库不在 pip 上

A
Alan W. Smith

如前所述,安装 Xcode 和 Xcode 命令行工具。改用 Pillow,因为 PIL 基本上已经死了。 Pillow 是 PIL 的一个维护分支。

https://pypi.org/project/Pillow/

pip install Pillow

如果您同时安装了两个 Python 并希望为 Python3 安装它:

python3 -m pip install Pillow

尝试此操作时,我在 OSX Mavericks 上遇到 clang 错误,但我发现此答案对此有所帮助:stackoverflow.com/a/22322645/16959
虽然这是一个有效且有用的答案,但它并没有真正回答这个问题。对于我们这些在修复旧项目的过程中可能需要填充 PIL 依赖项以便我们可以找到下一个将破坏的项目的人,我们实际上需要获取实际的依赖项(有时甚至需要获取特定版本有人建立了一个黑客)。
@Namey 将现有 PIL 代码移植到 Pillow 可以轻松完成。 Port PIL to Pillow 。 Pillow 是基于 PIL 代码,并且已经演变成一个更好、更现代、更友好的 PIL 版本。
@GiriB 感谢您的更新。我的主要观点是,在某些情况下,您通常会开始处理一些遗留的生产代码,其中有 20 个古老的依赖项,其中 PIL 可能是其中之一,并且您需要调试一些东西来修复生产代码。在这种情况下,您肯定不想为了开始调试生产环境中发生的单行错误而在您的开发构建中为连接到大约 10 个不同库的代码编写端口。希望这可以澄清。
我正在尝试使用使用 import Image 的库。有没有办法让 Pillow 提供图像?
P
Peter Mortensen

这对我有用:

apt-get install python-dev
apt-get install libjpeg-dev
apt-get install libjpeg8-dev
apt-get install libpng3
apt-get install libfreetype6-dev
ln -s /usr/lib/i386-linux-gnu/libfreetype.so /usr/lib
ln -s /usr/lib/i386-linux-gnu/libjpeg.so /usr/lib
ln -s /usr/lib/i386-linux-gnu/libz.so /usr/lib

pip install PIL  --allow-unverified PIL --allow-all-external

在 64 位平台上,链接路径需要不同——例如 /usr/lib/x86_64-linux-gnu/libfreetype.so 。要获得更独立于架构的解决方案,请创建类似 # ln -s /usr/lib/`uname -i`-linux-gnu/libfreetype.so /usr/lib/ 的链接
成像标题总是同样的问题:_imagingft.c:73:31: fatal error: freetype/fterrors.h: No such file or directory #include 所以你必须通过 pip 安装 python-imaging
像这里提到的那样 [askubuntu.com/questions/507459/…?解决方案:cd /usr/include ln -s freetype2 freetype
在 ubuntu 14.04 下还需要 1 个符号链接 sudo ln -s /usr/include/freetype2 /usr/local/include/freetype
对我来说效果很好,秘诀是 --allow-unverified PIL --allow-all-external
r
redchief

使用 apt install 非常简单,使用此命令即可完成

sudo apt-get install python-PIL

或者

sudo pip install pillow

或者

sudo easy_install pillow

如果您正在运行 ipython 笔记本服务器,请不要忘记重新启动它。
sudo pip install枕头为我工作。 sudo apt-get install python-PIL 没有。
我发现它区分大小写,所以: sudo apt-get install python-PIL => Failed (no package found) sudo apt-get install python-pil => Worked
C
Chandan Sharma

安装

pip install Pillow

然后,只需导入您的文件,例如,

from PIL import Image

我正在使用窗户。它对我有用。

注意:Pillow 是 Python Imaging Library 的功能性替代品。要使用 Pillow 运行现有的 PIL 兼容代码,需要对其进行修改以从 PIL 命名空间而不是全局命名空间导入 Imaging 模块。

即改变:

import Image

至:

from PIL import Image

https://pypi.org/project/Pillow/2.2.1/


谢谢!在 from PIL import Image 中阅读名称“PIL”时,我永远猜不到这个库的名称是“Pillow”。我也在 Windows 上,你的解决方案对我有用。
Pillow 是 Python Imaging Library 的功能性替代品。要使用 Pillow 运行现有的 PIL 兼容代码,需要对其进行修改以从 PIL 命名空间而不是全局命名空间导入 Imaging 模块。即更改:import Imagefrom PIL import Image pypi.org/project/Pillow/2.2.1
感谢您提供额外的背景信息!
g
gtangil

您应该按照 here 的说明进行安装:

pip install image

m
m01

在 Mac OS X 上,使用以下命令:

sudo pip install https://effbot.org/media/downloads/Imaging-1.1.7.tar.gz

也在 debian jessy 上工作过!,只需要预先安装 python-dev :)
我必须先安装 libjpeg:brew install libjpeg
最后是一个实际的解决方案。我的 Mac 没有 apt-get :p,无论如何我都想安装在 virtualenv 中。
使用 sudo pip install Pillow - 如下。
i
ismailsunni

我从讨论 here 中得到了答案:

我试过了

pip install --no-index -f http://dist.plone.org/thirdparty/ -U PIL

它奏效了。


考虑使用这个:pip install --no-index -f http://effbot.org/downloads/ -U PIL --trusted-host effbot.org 我使用这个的原因是因为它是包列出的 URL,并且在 pypi.python.org/pypi/PIL
我收到使用 HTTPS 而不是 HTTP 的警告。更改网址后效果很好。
@TomMyddeltyn 这几乎可以工作,只是我遇到了一个无法找到 X11/Xlib.h 的编译器错误
@Uncommon 我不确定问题出在哪里,抱歉。
G
Glorfindel

我认为你在 Mac 上。请参阅How can I install PIL on mac os x 10.7.2 Lion

如果您使用自制软件,您可以使用 brew install pil 安装 PIL。然后,您可能需要将安装目录 ($(brew --prefix)/lib/python2.7/site-packages) 添加到 PYTHONPATH,或者将 PIL 目录本身的位置添加到名为 PIL.pth 的文件中您的站点包目录,内容为:/usr/local/lib/python2.7/site-packages/PIL(假设 brew --prefix 是 /usr/local)。或者,您可以从源代码下载/构建/安装它: # download curl -O -L http://effbot.org/media/downloads/Imaging-1.1.7.tar.gz # extract tar -xzf Imaging-1.1 .7.tar.gz cd Imaging-1.1.7 # 构建和安装 python setup.py build sudo python setup.py install # 或者为你安装它而不需要管理员权限:# python setup.py install --user 我跑了上面刚刚(在 OSX 10.7.2 上,使用 XCode 4.2.1 和 System Python 2.7.1)并且它构建得很好,尽管我的环境中的某些东西可能是非默认的。


虽然这解释了如何安装 pil,但它并没有像 OP 最初尝试的那样通过 pip 进行安装。我发现这些天在网络上大量使用 brew 并不理想。 brew 将安装相当多的开销,具体取决于您希望它执行的操作。所以 pip 可能仍然是 python 模块的最佳选择。
D
Dmitry Pleshkov

这些天来,每个人都使用 Pillow,一个友好的 PIL 分支,而不是 PIL。

而不是:sudo pip install pil

做:sudo pip install pillow

$ sudo apt-get install python-imaging
$ sudo -H pip install pillow

我建议不要做sudo pip install ...。这最终可能会破坏您系统的 Python 安装,这可能会影响您系统的应用程序。尝试使用 Anaconda virtual environmentsvenv 或 Docker。
P
Peter Mortensen

对于 Ubuntu,PIL 不再工作。我总是得到:

没有找到 PIL 的匹配分布

所以安装python-imaging:

sudo apt-get install python-imaging

P
Peter Mortensen

我有同样的问题,但安装 python-dev 解决了。

在安装 PIL 之前,运行以下命令:

sudo apt-get install python-dev

然后安装 PIL:

pip install PIL

这对我在 Ubuntu 14 上不起作用。无论我是否先安装 python-dev,pip install pil 都会导致 No distributions at all found for PIL
D
Denis Kutlubaev

我在安装过程中遇到了一些错误。以防万一有人也有这个。尽管我已经坐在管理员用户下,但不是 root。

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/PIL'

Storing debug log for failure in /Users/wzbozon/Library/Logs/pip.log

添加“sudo”解决了这个问题,使用 sudo 它可以工作:

~/Documents/mv-server: $ sudo pip install Pillow

P
Peter Mortensen

对于 CentOS:

yum install python-imaging

G
Gryu

我尝试了所有答案,但都失败了。直接从官网获取源码然后build安装成功。

去网站http://www.pythonware.com/products/pil/#pil117点击“Python Imaging Library 1.1.7 Source Kit”下载源码tar xf Imaging-1.1.7.tar.gz cd Imaging-1.1 .7 sudo python setup.py 安装


A
Alexandre Simões

我用 sudo port install py27-Pillow 搞定了


a
alfonso

尝试这个:

sudo pip install PIL --allow-external PIL --allow-unverified PIL

虽然这个答案可能是正确的,但请添加一些解释。传递底层逻辑比仅仅提供代码更重要,因为它可以帮助 OP 和其他读者自己解决这个问题和类似问题。
请注意,此命令对我来说已超时。无论如何,它将在几周内被弃用。 sudo pip install --allow-external --allow-unverified PIL PIL 目前也可以使用。
--allow-external--allow-unverified 均已弃用。 pip install pillow 是正确答案。
我发布的答案是 stderr 向我建议的。因此,虽然我的答案现在已经过时,但不是在我写它的时候。另外,我为我发给 JasonMc92 的信息道歉(该信息已被删除)。我的语气不合时宜,但我的情绪仍然存在。拖钓首页并复制/粘贴您关于“赋予基础逻辑”的注释有些令人反感。也许你在追逐徽章,我想这很好,但为了保持一致性,请对这个问题的所有其他类似答案发表评论,这些答案不符合逻辑。
n
nguyên

(窗口)如果 Pilow 不起作用,请尝试在 http://www.pythonware.com/products/pil/ 下载 pil


T
Ted Taylor of Life

首先,您应该运行此 sudo apt-get build-dep python-imaging 它将为您提供您可能需要的所有依赖项

然后运行 sudo apt-get update && sudo apt-get -y upgrade

其次是 sudo apt-get install python-pip

然后最后安装 Pil pip install枕头


k
khanhtc1202

在使用 pip 之前搜索包管理器。在 Arch linux 上,您可以通过 pacman -S python2-pillow 获得 PIL


R
Ricoter

对于 Ubuntu,您可以使用 apt install 安装 PIL:

对于 Python 3 使用:

sudo apt install python3-pil

对于 Python 2 使用:

sudo apt install python-pil

正如 Clarkey252 指出的那样,pil 应该是小写的


apt 是否作为 macOS 的包管理器存在?
抱歉,这是针对 ubuntu 的
标签、标题和示例显示“macos”。你为什么要发布关于 Ubuntu 的答案?
我理解你的沮丧,你是对的,这个问题页面搞砸了。在这个问题上,“Ubuntu”、“Windows”、“Linux”的答案比 MacO 还多。 “枕头”甚至“图像”的回答比 PIL 更频繁。没有解决方案实际上确实回答了这个问题!然而,所有解决方案都基于 bash,并且通常是可转移的。无论如何,PIL 存储库都不在 pip 上,所以唯一真正的答案是“你不能”
@Ricoter 好吧,为什么不将此消息集成到您的 aswer 中,作为“您不能但在 ubuntu 上可以做到这一点......,所以在 macos 环境中,您可以将其翻译成这个......” .答案将更具描述性和全面性,作为一个正在寻找正确答案的人,我将实现我的目的。
P
Peter Mortensen

还有另一个名为 conda 的 Python 包工具。当有库需要安装 C++ 和其他不是纯 Python 的绑定时,Conda 比 pip 更受欢迎(我相信)。 Conda 在其安装中也包含 pip,因此您仍然可以使用 pip,但您也可以获得 conda 的好处。

默认情况下,Conda 还安装 IPython、pil 和许多其他库。我想你会喜欢的。