开发者

Django - No module named PIL

开发者 https://www.devze.com 2023-03-17 02:53 出处:网络
I need to use ImageField in my models and PIL seems not to be installed: >>> from PIL import Image

I need to use ImageField in my models and PIL seems not to be installed:

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

However I can see python-imaging installed in Synaptic. When I try to easy-install it, I get:

$ 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

And this is still not usable. Do I have to do something else to be able to use the ImageField field type ?


Samuele Mattiuzzo gave me the answer:

have you tried with import Image instead of from PIL import Image? sometimes this solves the issue


Also sometimes this may not work:

pip install pil 

Check this out : gcc error trying to install PIL in a Python2.6 virtualenv


I know this is an old question already answered, but I found another way to solve it that corrected the problems in my server. The PIL package was installed by easy_install and stored in "/usr/local/lib/python2.6/site-packages/PIL-1.1.7-py2.6-linux-x86_64.egg". Looking on its contents resulted on:

ArgImagePlugin.py        FpxImagePlugin.py        ImageColor.pyc    ImagePalette.py     _imagingmath.so        PaletteFile.pyc        SunImagePlugin.py
ArgImagePlugin.pyc       FpxImagePlugin.pyc       ImageDraw2.py     ImagePalette.pyc    _imaging.py            PalmImagePlugin.py     SunImagePlugin.pyc
BdfFontFile.py           GbrImagePlugin.py        ImageDraw2.pyc    ImagePath.py        _imaging.pyc           PalmImagePlugin.pyc    TarIO.py
BdfFontFile.pyc          GbrImagePlugin.pyc       ImageDraw.py      ImagePath.pyc       _imaging.so            PcdImagePlugin.py      TarIO.pyc
BmpImagePlugin.py        GdImageFile.py           ImageDraw.pyc     Image.py            ImImagePlugin.py       PcdImagePlugin.pyc     TgaImagePlugin.py
BmpImagePlugin.pyc       GdImageFile.pyc          ImageEnhance.py   Image.pyc           ImImagePlugin.pyc      PcfFontFile.py         TgaImagePlugin.pyc
BufrStubImagePlugin.py   GifImagePlugin.py        ImageEnhance.pyc  ImageQt.py          ImtImagePlugin.py      PcfFontFile.pyc        TiffImagePlugin.py
BufrStubImagePlugin.pyc  GifImagePlugin.pyc       ImageFileIO.py    ImageQt.pyc         ImtImagePlugin.pyc     PcxImagePlugin.py      TiffImagePlugin.pyc
ContainerIO.py           GimpGradientFile.py      ImageFileIO.pyc   ImageSequence.py    __init__.py            PcxImagePlugin.pyc     TiffTags.py
ContainerIO.pyc          GimpGradientFile.pyc     ImageFile.py      ImageSequence.pyc   __init__.pyc           PdfImagePlugin.py      TiffTags.pyc
CurImagePlugin.py        GimpPaletteFile.py       ImageFile.pyc     ImageShow.py        IptcImagePlugin.py     PdfImagePlugin.pyc     WalImageFile.py
CurImagePlugin.pyc       GimpPaletteFile.pyc      ImageFilter.py    ImageShow.pyc       IptcImagePlugin.pyc    PixarImagePlugin.py    WalImageFile.pyc
DcxImagePlugin.py        GribStubImagePlugin.py   ImageFilter.pyc   ImageStat.py        JpegImagePlugin.py     PixarImagePlugin.pyc   WmfImagePlugin.py
DcxImagePlugin.pyc       GribStubImagePlugin.pyc  ImageFont.py      ImageStat.pyc       JpegImagePlugin.pyc    PngImagePlugin.py      WmfImagePlugin.pyc
EGG-INFO                 Hdf5StubImagePlugin.py   ImageFont.pyc     ImageTk.py          McIdasImagePlugin.py   PngImagePlugin.pyc     XbmImagePlugin.py
EpsImagePlugin.py        Hdf5StubImagePlugin.pyc  ImageGL.py        ImageTk.pyc         McIdasImagePlugin.pyc  PpmImagePlugin.py      XbmImagePlugin.pyc
EpsImagePlugin.pyc       IcnsImagePlugin.py       ImageGL.pyc       ImageTransform.py   MicImagePlugin.py      PpmImagePlugin.pyc     XpmImagePlugin.py
ExifTags.py              IcnsImagePlugin.pyc      ImageGrab.py      ImageTransform.pyc  MicImagePlugin.pyc     PsdImagePlugin.py      XpmImagePlugin.pyc
ExifTags.pyc             IcoImagePlugin.py        ImageGrab.pyc     ImageWin.py         MpegImagePlugin.py     PsdImagePlugin.pyc     XVThumbImagePlugin.py
FitsStubImagePlugin.py   IcoImagePlugin.pyc       ImageMath.py      ImageWin.pyc        MpegImagePlugin.pyc    PSDraw.py              XVThumbImagePlugin.pyc
FitsStubImagePlugin.pyc  ImageChops.py            ImageMath.pyc     _imagingft.py       MspImagePlugin.py      PSDraw.pyc
FliImagePlugin.py        ImageChops.pyc           ImageMode.py      _imagingft.pyc      MspImagePlugin.pyc     SgiImagePlugin.py
FliImagePlugin.pyc       ImageCms.py              ImageMode.pyc     _imagingft.so       OleFileIO.py           SgiImagePlugin.pyc
FontFile.py              ImageCms.pyc             ImageOps.py       _imagingmath.py     OleFileIO.pyc          SpiderImagePlugin.py
FontFile.pyc             ImageColor.py            ImageOps.pyc      _imagingmath.pyc    PaletteFile.py         SpiderImagePlugin.pyc

Well. No module PIL to import from. So, I did a dirty trick. Made a folder called PIL and copied all this files inside it.

mkdir PIL
cp * PIL/

So, everything started to work fine. I think this is the problem, depending on the instalation package you use, it will be deployed in a different way. ;)


If your question is "why is the support for JPEG, FREETYPE2 etc, not available?",

PIL installs with the support for the packages that are already present in your system while you install PIL.

For support of JPEG and FREETYPE install following packages:

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

If you have just installed it on OS X Lion and a third party lib cannot from PIL import image, then you can just do a ln -s PIL-OSX... PIL


The problem comes from how package managers setup(.py) PIL:

https://mail.python.org/pipermail/image-sig/2010-August/006480.html

Possible solutions:

  • Rename the EGG to "PIL."
  • Adjust your code to your installed package (not always possible.)
  • Perform a hack like Jayme did (elsewhere on this page.)
  • Use a different package for installation.
  • Manually configure setup.py for your installation.

"Rename the EGG" is not described elsewhere on this page, so I'll describe it here:

cd /usr/local/lib/python2.6/site-packages/
mv PIL-1.1.7-py2.6-linux-i686.egg PIL

It may be surprising, but this actually works in many situations.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号