macos 在 Mac OS X 上安装 PIL 以与 Django 一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5075620/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:44:19  来源:igfitidea点击:

Installing PIL to use with Django on Mac OS X

pythondjangomacospython-imaging-library

提问by Ignas But?nas

I'm really annoyed by installation of PIL (Python Imaging Library) on Mac OS X 10.6. Does anyone have it installed and could post the recipe here? I've tried a lot of them posted here on this site and a lot from google, but always anding with missing some part and can't work normally with PIL...

在 Mac OS X 10.6 上安装 PIL(Python 成像库)真的让我很恼火。有没有人安装过它并且可以在这里发布食谱?我已经尝试了很多在本网站上发布的内容以及从谷歌发布的很多内容,但总是遗漏某些部分并且无法正常使用 PIL ......

Thanks in advance. Ignas

提前致谢。伊格纳斯

采纳答案by Gavin Anderegg

EDIT:This answer has been getting voted up recently, and I want to modify it to reflect what I'm doing now.

编辑:这个答案最近得到了投票,我想修改它以反映我现在正在做的事情。

Firstly, I've switched from MacPorts to Homebrewfor package management on Mac OS X. Secondly, I've switched from using my package manager to using pipand virtualenvwrapperto manage my Python libraries.

首先,我已经从 MacPorts 切换到Homebrew以在 Mac OS X 上进行包管理。其次,我已经从使用包管理器切换到使用pipvirtualenvwrapper来管理我的 Python 库。

Why I switched:

为什么我换了:

At first, with just a few Django projects, it was very easy to keep everything up to date using MacPorts. It was also fairly easy to have multiple versions of Python using python_select. What I didn't realize was that I was doing a pretty terrible job of keeping multiple libraries working side-by-side. It became obvious as I upgraded my packages that sometimes I reallydidn't want a project's Django version to change. After a couple of Django 1.1 projects (now running Django 1.3) started exhibiting weird behaviour (forms failing to submit because of CSRF middleware changes, small differences in Django libraries, admin app assets changing, and so on) it became clear that I should look into a better solution.

起初,只有几个 Django 项目,使用 MacPorts 可以很容易地使所有内容保持最新。使用python_select. 我没有意识到的是,我在保持多个库并排工作方面做得非常糟糕。很明显,当我升级我的包时,有时我真的不想改变项目的 Django 版本。在几个 Django 1.1 项目(现在运行 Django 1.3)开始表现出奇怪的行为(由于 CSRF 中间件更改而导致表单无法提交、Django 库中的细微差异、管理应用程序资产更改等)之后,很明显我应该看看成更好的解决方案。

What I do now:

我现在应该做什么:

On Mac OS X I'm moved over to using pip and virtualenvwrapper. First off, I install virtualenvwrapper:

在 Mac OS X 上,我转而使用 pip 和 virtualenvwrapper。首先,我安装 virtualenvwrapper:

pip install virtualenvwrapper

This will grab virtualenv and virtualenvwrapper. You then need to add the following to your .bashrcor .profileand sourceit or open a new shell.

这将获取 virtualenv 和 virtualenvwrapper。然后,您需要将以下内容添加到您的.bashrc.profilesource它或打开一个新的外壳。

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh # where Homebrew places it
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages' # optional

Line 1 sets up the variable workonneeds to find its files. Line 2 points to the main shell script (the path here is where Homebrew places the file, it might be different if you're using another package manager). Line 3 is optional, but I really like it: it makes sure that no currently installed libraries in the "main" site-packages repository will leak into your newly created virtual environment. I find this keeps things clean and leads to fewer surprises down the road as things are upgraded.

第 1 行设置变量workon需要查找其文件。第 2 行指向主 shell 脚本(这里的路径是 Homebrew 放置文件的位置,如果您使用其他包管理器,它可能会有所不同)。第 3 行是可选的,但我真的很喜欢它:它确保“主”站点包存储库中当前安装的库不会泄漏到您新创建的虚拟环境中。我发现这可以保持事物的清洁,并且随着事物的升级而导致更少的惊喜。

The next step is to create a new virtual environment:

下一步是创建一个新的虚拟环境:

mkvirtualenv testEnvironmentName

After making the environment, you'll be placed into it. If you kept the --no-site-packagesflag, you can type pip freezeto see that your Python library slate is now blank. To escape from the virtual environment, use the deactivatecommand. To get into your virtualenv again, use workon testEnvironmentName. Note that you can use tab completion on the name of the environment. Also note that typing workonby itself will give you a list of available environments. From here you can pip installany libraries you want, including PIL.

制作环境后,您将被放入其中。如果您保留了该--no-site-packages标志,则可以键入pip freeze以查看您的 Python 库 slate 现在是空白的。要退出虚拟环境,请使用该deactivate命令。要再次进入您的 virtualenv,请使用workon testEnvironmentName. 请注意,您可以对环境名称使用制表符完成。另请注意,键入workon本身将为您提供可用环境的列表。从这里你可以使用pip install任何你想要的库,包括 PIL。

To learn more about virtualenvwrapper, I recommend checking out the documentation.

要了解有关 virtualenvwrapper 的更多信息,我建议查看文档

Here's another great resource which taught me a lot about using virtualenvwrapper(or just view the screencast)

这是另一个很棒的资源,它教会了我很多关于使用 virtualenvwrapper 的知识或者只是查看截屏视频



ORIGINAL:

原来的:

You can also instal PIL using MacPorts. The package name is py-pil. Here's more information on the package. I'm pretty fond of MacPorts over pip, as I find it gives me a bit more configurability when it comes to keeping several versions of python and several libraries installed.

您还可以使用MacPorts安装 PIL 。包名是py-pil. 这里有关于包的更多信息。我非常喜欢 pip 上的 MacPorts,因为我发现在安装多个版本的 python 和多个库时,它给了我更多的可配置性。

Here are the installation instructions for MacPorts: http://www.macports.org/install.php

以下是 MacPorts 的安装说明:http: //www.macports.org/install.php

See also: What is the most compatible way to install python modules on a Mac?

另请参阅:在 Mac 上安装 python 模块的最兼容方式是什么?

回答by petermanser

Following steps worked for me:

以下步骤对我有用:

$ brew install pip
$ export ARCHFLAGS="-arch i386 -arch x86_64"
$ pip install pil

回答by Yuji 'Tomita' Tomita

Yes, I have issues with PILon 10.6.6too, homebrewand easy_install.

是的,我有一个问题PIL10.6.6也是如此,homebreweasy_install

The easiest solution for me was to easy_install PIL/ pip install PIL, navigate to /Library/Python/2.6/site-packages/, and symlink the PIL-1.1.7-.....eggfile to PIL

对我来说最简单的解决方案是easy_install PIL/ pip install PIL,导航到/Library/Python/2.6/site-packages/,并将PIL-1.1.7-.....egg文件符号链接到PIL

ln -s PIL-................egg PIL


>>> import PIL
>>> from PIL import Image
# no more problems.

Edit: These days, I try to use Pillow, a library built to help install PIL. Try pip install pillow- can't hurt!

编辑:最近,我尝试使用 Pillow,这是一个用于帮助安装 PIL 的库。试试pip install pillow- 不会受伤!

回答by Kenny Shen

It might be easier to pinpoint your issue if you can elaborate on what you tried and what error messages were generated with those attempts. Here's some probable solutions that you may or may not have attempted:

如果您可以详细说明您的尝试以及这些尝试生成了哪些错误消息,则可能更容易查明您的问题。以下是您可能尝试过或未尝试过的一些可能的解决方案:

pip install pil

if you dont have pip, try

如果您没有pip,请尝试

easy_install pil

Since you're on the Mac, you can also get HomeBrew (a package manager) and then try

由于您使用的是 Mac,您还可以获取 HomeBrew(一个包管理器),然后尝试

brew install pil

or Macportshas a PIL package here.

或者Macports在这里有一个PIL 包

Or build from source?

还是从源代码构建?

More importantly, the PIL README states,

更重要的是,PIL README 指出,

*--------------------------------------------------------------------

*------------------------------------------------- -------------------

Additional notes for Mac OS X

Mac OS X 的附加说明

On Mac OS X you will usually install additional software such as libjpeg or freetype with the "fink" tool, and then it ends up in "/sw". If you have installed the libraries elsewhere, you may have to tweak the "setup.py" file before building.*

在 Mac OS X 上,您通常会使用“fink”工具安装额外的软件,例如 libjpeg 或 freetype,然后它会以“/sw”结尾。如果你已经在别处安装了这些库,你可能需要在构建之前调整“setup.py”文件。*

That could be your issue. Good luck!

那可能是你的问题。祝你好运!

回答by mondi

I'm using OS X 10.5.8, gcc 4.2.1, python 2.7.5, libjpegv9 and Pillow 2.1.0 (which is based on PIL).

我使用的是 OS X 10.5.8、gcc 4.2.1、python 2.7.5、libjpegv9 和 Pillow 2.1.0(基于 PIL)。

My problem apparently (but it's just a guess) was caused by architecture incompatibilities of the libjpeg and python (and Pillow) builds.

我的问题显然(但这只是一个猜测)是由 libjpeg 和 python(和 Pillow)构建的体系结构不兼容引起的。

Building python and libjpeg from source using only 32bit arch solved it. I installed all the other python libraries, Pillow included, just by using

仅使用 32 位架构从源代码构建 python 和 libjpeg 解决了它。我安装了所有其他 python 库,包括 Pillow,只是通过使用

pip install xxxxx

and it worked fine.

它工作正常。

Details

细节

First I tried both a dmg from the python.org site and a universal build installed via macports. I had not specified universal when installing but macports installed both i386 and ppc architectures anyways.

首先,我尝试了来自 python.org 站点的 dmg 和通过 macports 安装的通用构建。我在安装时没有指定通用,但 macports 无论如何都安装了 i386 和 ppc 架构。

That caused me problems because libjpeg compiles only to i386 by default.

这给我带来了问题,因为 libjpeg 默认只编译为 i386。

To check the build of the binaries, I did:

为了检查二进制文件的构建,我做了:

file /usr/bin/python

/usr/bin/python: Mach-O universal binary with 2 architectures

/usr/bin/python:具有 2 种架构的 Mach-O 通用二进制文件

/usr/bin/python: Mach-O executable i386

/usr/bin/python: Mach-O 可执行文件 i386

/usr/bin/python: Mach-O executable ppc

/usr/bin/python: Mach-O 可执行 ppc

file /usr/local/lib/libjpeg.dylib 

/usr/local/lib/libjpeg.dylib: Mach-O dynamically linked shared library i386

/usr/local/lib/libjpeg.dylib: Mach-O 动态链接共享库 i386

When building PIL (or Pillow), it seemed to use the same build options as python "-gcc i386 -gcc ppc" (which seems logical).

在构建 PIL(或 Pillow)时,它似乎使用与 python "-gcc i386 -gcc ppc" 相同的构建选项(这似乎合乎逻辑)。

All went well until it built the "_imaging" module. There, it showed a warning that libjpeg.dylib was not of the right architecture but in the end it showed that JPEG was available just the same.

一切都很顺利,直到它构建了“_imaging”模块。在那里,它显示了一个警告,指出 libjpeg.dylib 的架构不正确,但最终它表明 JPEG 也可用。

回答by Ignas But?nas

After fresh OS installation tried these steps:

全新安装操作系统后,尝试以下步骤:

  1. Installed homebrew
  2. brew install libjpeg
  3. brew install pil
  4. easy_install pil
  5. cd /Library/Python/2.6/site-packages
  6. ln -s /usr/local/Cellar/pil/1.1.7/lib/python2.6/site-packages/PIL PIL
  1. 安装自制软件
  2. brew 安装 libjpeg
  3. 酿造安装pil
  4. easy_install pil
  5. cd /Library/Python/2.6/site-packages
  6. ln -s /usr/local/Cellar/pil/1.1.7/lib/python2.6/site-packages/PIL PIL

After that I was able to load and save jpeg files... I know that this issue has a lot of different solutions and I'm not 100% my way will work for other, but it's worth to try if you just bumping your head in to the wall :)

之后我能够加载和保存 jpeg 文件...我知道这个问题有很多不同的解决方案,我不是 100% 我的方式适用于其他人,但如果你只是撞到你的头,值得一试在墙上:)

回答by aclark

There is a packaging fork of PIL that attempts to make it easier to install and support on many modern platforms, including OS X:

有一个 PIL 的打包分支,它试图让它更容易在许多现代平台上安装和支持,包括 OS X:

You should be able to install it on OS X with pip or easy_install (assuming you have XCode). If you have any trouble, please open a ticket here:

您应该能够使用 pip 或 easy_install 在 OS X 上安装它(假设您有 XCode)。如果您有任何问题,请在此处开票:

回答by Ademuk

Hey check out this article, worked wonders for me on Snow Leopard.

嘿,看看这篇文章,在雪豹上为我创造了奇迹。

I'd also recommend using libjpeg 0.6instead of 0.7 noted in the article.

我还建议使用libjpeg 0.6而不是文章中提到的 0.7。

Good luck.

祝你好运。

回答by ZelluX

I have issues installing PIL with brew and easy_install on my Mac too. My solution is to download source code from http://www.pythonware.com/products/pil/, extract the tar ball and use

我在 Mac 上也有使用 brew 和 easy_install 安装 PIL 的问题。我的解决方案是从http://www.pythonware.com/products/pil/下载源代码,解压 tar ball 并使用

python setup.py install

to compile and install the package.

编译和安装包。