简体   繁体   English

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

[英]Installing PIL to use with Django on Mac OS X

I'm really annoyed by installation of PIL (Python Imaging Library) on Mac OS X 10.6. 我对在Mac OS X 10.6上安装PIL(Python Imaging Library)感到非常恼火。 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... 我已经尝试了很多这些在这个网站上发布了很多来自谷歌,但总是缺少一些部分而且无法正常使用PIL ......

Thanks in advance. 提前致谢。 Ignas Ignas

Following steps worked for me: 以下步骤对我有用:

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

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 Homebrew for package management on Mac OS X. Secondly, I've switched from using my package manager to using pip and virtualenvwrapper to 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. 首先,只需几个Django项目,就可以很容易地使用MacPorts保持最新状态。 It was also fairly easy to have multiple versions of Python using python_select . 使用python_select多个版本的Python也相当容易。 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 really didn't want a project's Django version to change. 很明显,当我升级我的包时,有时候我真的不想让项目的Django版本改变。 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 1.1项目(现在运行Django 1.3)开始表现出奇怪的行为(由于CSRF中间件更改,Django库中的小差异,管理应用程序资产更改等等而无法提交表单)之后,很明显我应该看一下进入更好的解决方案。

What I do now: 我现在应该做什么:

On Mac OS X I'm moved over to using pip and virtualenvwrapper. 在Mac OS X上,我转而使用pip和virtualenvwrapper。 First off, I install virtualenvwrapper: 首先,我安装virtualenvwrapper:

pip install virtualenvwrapper

This will grab virtualenv and virtualenvwrapper. 这将抓住virtualenv和virtualenvwrapper。 You then need to add the following to your .bashrc or .profile and source it or open a new shell. 然后,您需要添加以下到您.bashrc.profilesource ,或打开新的shell。

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 workon needs to find its files. 第1行设置变量workon需要查找其文件。 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). 第2行指向主shell脚本(此处的路径是Homebrew放置文件的位置,如果您使用另一个包管理器,它可能会有所不同)。 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. 第3行是可选的,但我非常喜欢它:它确保“main”site-packages存储库中当前安装的库不会泄漏到新创建的虚拟环境中。 I find this keeps things clean and leads to fewer surprises down the road as things are upgraded. 我发现这样可以保持东西干净,并且随着事情的升级,可以减少意外情况。

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-packages flag, you can type pip freeze to see that your Python library slate is now blank. 如果保留了--no-site-packages标志,则可以键入pip freeze以查看您的Python库平板现在是空白的。 To escape from the virtual environment, use the deactivate command. 要退出虚拟环境,请使用deactivate命令。 To get into your virtualenv again, use workon testEnvironmentName . 要再次进入virtualenv,请使用workon testEnvironmentName Note that you can use tab completion on the name of the environment. 请注意,您可以在环境名称上使用Tab键完成。 Also note that typing workon by itself will give you a list of available environments. 另请注意, workon输入workon将为您提供可用环境的列表。 From here you can pip install any libraries you want, including PIL. 从这里你可以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 . 您还可以使用MacPorts安装PIL。 The package name is py-pil . 包名称是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而不是pip,因为我发现它在保持几个版本的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模块最兼容的方法是什么?

Yes, I have issues with PIL on 10.6.6 too, homebrew and easy_install . 是的,我在10.6.6也有PIL问题, 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-.....egg file 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. 编辑:这些天,我尝试使用Pillow,一个帮助安装PIL的库。 Try pip install pillow - can't hurt! 试试pip install pillow - 不能伤害!

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 Macports has 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". 在Mac OS X上,您通常会使用“fink”工具安装其他软件,如libjpeg或freetype,然后以“/ sw”结尾。 If you have installed the libraries elsewhere, you may have to tweak the "setup.py" file before building.* 如果您已在其他地方安装了库,则可能需要在构建之前调整“setup.py”文件。*

That could be your issue. 那可能是你的问题。 Good luck! 祝好运!

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. 仅使用32位arch从源代码构建python和libjpeg解决了它。 I installed all the other python libraries, Pillow included, just by using 我安装了所有其他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. 首先,我尝试了python.org站点的dmg和通过macports安装的通用构建。 I had not specified universal when installing but macports installed both i386 and ppc architectures anyways. 安装时没有指定通用,但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. 一切顺利,直到它建立了“_imaging”模块。 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. 在那里,它显示了一个警告,libjpeg.dylib不是正确的架构,但最后它显示JPEG可用相同。

After fresh OS installation tried these steps: 在新的操作系统安装后尝试以下步骤

  1. Installed homebrew 安装自制软件
  2. brew install libjpeg brew安装libjpeg
  3. brew install pil 酿造安装
  4. easy_install pil easy_install pil
  5. cd /Library/Python/2.6/site-packages cd /Library/Python/2.6/site-packages
  6. ln -s /usr/local/Cellar/pil/1.1.7/lib/python2.6/site-packages/PIL PIL 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%我的方式会为其他人工作,但如果你只是碰到头脑,那值得尝试在墙上:)

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). 您应该能够使用pip或easy_install在OS X上安装它(假设您有XCode)。 If you have any trouble, please open a ticket here: 如果您有任何问题,请在此打开机票:

Hey check out this article , worked wonders for me on Snow Leopard. 嘿看看这篇文章 ,在Snow Leopard上为我创造了奇迹。

I'd also recommend using libjpeg 0.6 instead of 0.7 noted in the article. 我还建议使用文章中提到的libjpeg 0.6而不是0.7。

Good luck. 祝好运。

I have issues installing PIL with brew and easy_install on my Mac too. 我在Mac上安装了带有brew和easy_install的PIL也遇到了问题。 My solution is to download source code from http://www.pythonware.com/products/pil/ , extract the tar ball and use 我的解决方案是从http://www.pythonware.com/products/pil/下载源代码,提取tar球并使用

python setup.py install

to compile and install the package. 编译和安装包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM