PIL Image ImportError
Asked Answered
C

1

7

I have Pillow and qrcode modules installed in a virtual environment.

From the python shell, I can create a test image programmatically using PIL:

>>> from PIL import Image
>>> img = Image.new('1', (200, 200))
>>> img.save('test-image.jpeg', 'JPEG')

Great, that works just as I would expect it to. However, I'm getting this error when I try to use a module that relies on PIL:

>>> import qrcode
>>> qr_code = qrcode.make("1") 
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/home/vagrant/.virtualenvs/env1/local/lib/python2.7/site-packages/qrcode/main.py", line 8, in make
     return qr.make_image()
   File "/home/vagrant/.virtualenvs/env1/local/lib/python2.7/site-packages/qrcode/main.py", line 186, in make_image
     from qrcode.image.pil import PilImage
   File "/home/vagrant/.virtualenvs/env1/local/lib/python2.7/site-packages/qrcode/image/pil.py", line 5, in <module>
     import Image
ImportError: No module named Image

Why can't qrcode import PIL's Image class but it works from the shell?

Caveator answered 1/10, 2013 at 21:29 Comment(2)
This is a bit of a shot in the dark, but… does from PIL import ImageDraw also work on the shell? If that module is somehow missing from the PIL package, qrcode will assume Image is also missing from the PIL package and try to import it from the top level, which will cause exactly the problem you're seeing.Notability
A more likely possibility is that you're not actually testing from the right virtual environment. From your shell, after that from PIL import Image, just type Image and see what path it gives you. Is it inside /home/vagrant/.virtualenvs/env1/blah/blah, or not?Notability
M
6

This is an issue with your installation: Image module have been installed as subpackage of a PIL module, while the library you are using expects Image module to be directly in the python path. Simplest solution is to replace:

import Image

with:

from PIL import Image

in file qrcode/image/pil.py.

Malachy answered 1/10, 2013 at 21:36 Comment(4)
Yeah, old-school PIL sometimes installed one way, sometimes the other. Many people would explicitly add the package to sys.path to work around this, but that was always a horrible idea. Pillow picked the more sensible one and went with it consistently, but there are a few other packages out there that expect the other.Notability
But… this isn't going to work. If you look at the source, it already tries from PIL import Image, and only falls back to import Image when that fails.Notability
In this case, there must be something wrong with your virtualenv. Just tested and it works: I ran virtualenv so && source so/bin/activate && pip install pillow qrcode six && python and typed import qrcode; qrcode.make("1").show(). Sorry for the poor, comment-fit formatting.Malachy
I spun up a new vagrant box and did a fresh install of everything and things are working as expected. Thanks for pointing me in the right direction.Caveator

© 2022 - 2024 — McMap. All rights reserved.