PIL Image ImportError

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

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

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

Great, this works as I expected. However, I get 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 the PIL Image class, but does it work from the shell?

+6
source share
1 answer

This is the problem with your installation: the Image module was installed as a subpackage of the PIL module, and the library used expects the Image module to be directly in the python path. The simplest solution is to replace:

 import Image 

with:

 from PIL import Image 

in the qrcode/image/pil.py .

+5
source

Source: https://habr.com/ru/post/955056/


All Articles