'import Image' instead of 'from PIL import Image'

Bug #930550 reported by Mohammad Alhashash
16
This bug affects 3 people
Affects Status Importance Assigned to Milestone
LiveWallpaper
Fix Released
Undecided
Unassigned
Odoo Addons (MOVED TO GITHUB)
Fix Released
Low
Unassigned
QR Tools
Fix Released
Undecided
Ramiro Algozino

Bug Description

While 'import Image' works with the standard PIL installation, it is not supported and not compatible Pillow, the friendly PIL fork.

PIL is imported correctly in all other places:
find -name "*.py" -print0 |xargs -0 grep -e '\bimport\b.*\bImage\b'

*** IMPORTANT ***
python-imaging package depends on python-imaging-compat which provides "import Image".
However it will be dropped when "import Image" is fixed on Debian.
For more details, see: http://lists.debian.org/debian-python/2013/02/msg00017.html

*** How to fix ***
Please use the following code instead of 'import Image':

try:
    from PIL import Image
except ImportError:
    import Image

Tags: pil pos

Related branches

Revision history for this message
Mohammad Alhashash (alhashash) wrote :
description: updated
tags: removed: of point sale
Revision history for this message
Olivier Dony (Odoo) (odo-openerp) wrote :

Hello,

You're right, the pos module was breaking our convention of cleanly importing PIL.
Your patch was merged in trunk addons at revision 6554 (rev-id: <email address hidden>)

Thanks a lot for taking the time to report it and provide a patch for the issue at the same time!

Changed in openobject-addons:
importance: Undecided → Low
milestone: none → 6.1
status: New → Fix Released
Revision history for this message
Ronald Portier (Therp) (rportier1962) wrote :

Problem is that this is not working everywhere.

And why is this the "clean" way of importing?

Import Image is what the documentation says:

http://www.pythonware.com/library/pil/handbook/introduction.htm

I had to revert this patch/update to get my server starting again...

Because of this error:

Traceback (most recent call last):
  File "/home/oervwncrmtst61/oervwncrmtst61-openerp-server", line 252, in <module>
    openerp.modules.module.load_openerp_module(m)
  File "/home/oervwncrmtst61/bzr/openobject-server/openerp/modules/module.py", line 419, in load_openerp_module
    getattr(sys.modules['openerp.addons.' + module_name], info['post_load'])()
  File "/home/oervwncrmtst61/openerp-web/addons/web/__init__.py", line 29, in wsgi_postload
    app = common.http.Root(o)
  File "/home/oervwncrmtst61/openerp-web/addons/web/common/http.py", line 453, in __init__
    static_dirs = self._load_addons(openerp_addons_namespace)
  File "/home/oervwncrmtst61/openerp-web/addons/web/common/http.py", line 523, in _load_addons
    m = __import__('openerp.addons.' + module)
  File "/home/oervwncrmtst61/bzr/openobject-server/openerp/modules/module.py", line 139, in load_module
    mod = imp.load_module('openerp.addons.' + module_part, f, path, descr)
  File "/home/oervwncrmtst61/openobject-addons/point_of_sale/__init__.py", line 22, in <module>
    import point_of_sale
  File "/home/oervwncrmtst61/openobject-addons/point_of_sale/point_of_sale.py", line 26, in <module>
    from PIL import Image
ImportError: No module named PIL

Solved by making this edit:
# vim /home/oervwncrmtst61/openobject-addons/point_of_sale/point_of_sale.py

 25 import logging
 26 # Patch 2012-02-20 Ronald Portier
 27 # from PIL import Image
 28 import Image

Revision history for this message
Olivier Dony (Odoo) (odo-openerp) wrote : Re: [Bug 930550] Re: 'import Image' instead of 'from PIL import Image' in point_of_sale.py

On 02/20/2012 02:36 PM, Ronald Portier (Therp) wrote:
> Problem is that this is not working everywhere.
>
> And why is this the "clean" way of importing?

I understand how the adjective "clean" may be rather subjective.
The thing is, importing PIL from its own namespace is the standard Python way
of importing external packages (or stdlib packages for that matter).
PIL historically uses a distutils hack to enable "Import Image" to work.

> Import Image is what the documentation says:
>
> http://www.pythonware.com/library/pil/handbook/introduction.htm

In fact, the documentation of PIL itself is rather confusing, it uses "Import
Image" in the Tutorial section, and "from PIL import Image" in other places, e.g:
    http://www.pythonware.com/library/pil/handbook/image.htm

This seems to indicate that the author intended both methods to work in all
cases, but unfortunately the way this is done is not compatible with setuptools
and eggs (easy_install). Or so I understand.

This incompatibility is apparently one of the reasons for the creation of the
Pillow fork, and is explained here:
    http://mail.python.org/pipermail/image-sig/2010-August/006480.html

> I had to revert this patch/update to get my server starting again...

If you search the source code of the official distribution of OpenERP (server
and addons) for "import Image" you will see the reason why this bug was
reported: everywhere we use "from PIL import Image", except for POS.
The patch for current bug should therefore not have created the problem, it
would normally have been there all the time.

One workaround for the PIL setuptools issue would be to install PIL using
"python setup.py install" or the binary package for your distribution instead
of "easy_install".. perhaps you can confirm this?

Now I imagine we could replace every PIL-related import with this pattern:

  try:
     from PIL import Image
  except ImportError:
     import Image

which would keep it compatible with Pillow and still compatible with
easy_install'd PIL packages.
That looks a bit overkill though, even if it could be abstracted away... might
be worth discussing over a merge proposal...

Revision history for this message
Stefan Rijnhart (Opener) (stefan-opener) wrote : Re: 'import Image' instead of 'from PIL import Image' in point_of_sale.py

Hi,

just to confirm that using

    python setup.py install

instead of easy_install provides a valid workaround.

Cheers,
Stefan.

description: updated
no longer affects: livewallpaper
summary: - 'import Image' instead of 'from PIL import Image' in point_of_sale.py
+ 'import Image' instead of 'from PIL import Image'
description: updated
description: updated
Changed in livewallpaper:
status: New → Fix Released
Changed in qr-tools:
assignee: nobody → Ramiro Algozino (algozino)
milestone: none → 1.4
Changed in qr-tools:
status: New → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.