Django - 找不到PIL模块

6 投票
5 回答
16651 浏览
提问于 2025-04-16 20:51

我需要在我的模型中使用 ImageField,但是好像没有安装 PIL

>>> from PIL import Image
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: No module named PIL

不过我在 Synaptic 中看到安装了 python-imaging。当我尝试用 easy-install 安装时,出现了:

$ sudo easy_install-2.7 PIL
[sudo] password for glide: 
Searching for PIL
Reading http://pypi.python.org/simple/PIL/
Reading http://www.pythonware.com/products/pil
Reading http://effbot.org/zone/pil-changes-115.htm
Reading http://effbot.org/downloads/#Imaging
Best match: PIL 1.1.7
Downloading http://effbot.org/media/downloads/PIL-1.1.7.tar.gz
Processing PIL-1.1.7.tar.gz
Running PIL-1.1.7/setup.py -q bdist_egg --dist-dir /tmp/easy_install-HvRSQr/PIL-1.1.7/egg-dist-tmp-nDJHmU
WARNING: '' not a valid package name; please use only.-separated package names in setup.py
_imaging.c:3017: warning: initialization from incompatible pointer type
_imaging.c:3077: warning: initialization from incompatible pointer type
libImaging/Quant.c: In function ‘rehash_collide’:
libImaging/Quant.c:154: warning: cast to pointer from integer of different size
--------------------------------------------------------------------
PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version       1.1.7
platform      linux2 2.7.1 (r271:86832, May  2 2011, 14:22:06)
              [GCC 4.4.3]
--------------------------------------------------------------------
*** TKINTER support not available
*** JPEG support not available
--- ZLIB (PNG/ZIP) support available
*** FREETYPE2 support not available
*** LITTLECMS support not available
--------------------------------------------------------------------
To add a missing option, make sure you have the required
library, and set the corresponding ROOT variable in the
setup.py script.

To check the build, run the selftest.py script.
zip_safe flag not set; analyzing archive contents...
Image: module references __file__
Adding PIL 1.1.7 to easy-install.pth file
Installing pilconvert.py script to /usr/local/bin
Installing pilprint.py script to /usr/local/bin
Installing pildriver.py script to /usr/local/bin
Installing pilfont.py script to /usr/local/bin
Installing pilfile.py script to /usr/local/bin

Installed /usr/local/lib/python2.7/site-packages/PIL-1.1.7-py2.7-linux-x86_64.egg
Processing dependencies for PIL
Finished processing dependencies for PIL

但是这仍然无法使用。我还需要做些什么才能使用 ImageField 这个字段类型呢?

5 个回答

0

如果你在问“为什么不支持JPEG、FREETYPE2等格式?”

PIL在安装时,会根据你系统中已经存在的包来提供支持。

要支持JPEG和FREETYPE,你需要安装以下这些包:

sudo aptitude install libjpeg libjpeg-dev
sudo aptitude install libfreetype6 libfreetype6-dev

如果你刚在OS X Lion上安装了它,而某个第三方库无法使用from PIL import image,你可以执行ln -s PIL-OSX... PIL这个命令。

1

有时候,这个方法可能不管用:

pip install pil 

你可以看看这个链接:在Python2.6的虚拟环境中安装PIL时遇到的gcc错误

6

Samuele Mattiuzzo 给了我一个建议:

你试过用 import Image 而不是 from PIL import Image 吗?有时候这样做能解决问题。

撰写回答