Help with Django/PIL/QRCode

Hello all,

I have an Django app that features a QR code generator. It works as intended in my local development environment, but I can't seem to get it to work on PythonAnywhere. The other features of the app work in my PythonAnywhere testing environment. I would really love if anyone had some input. Thanks in advance!

I'm using a virtual environment for both local and prod. The relevant packages are Python 3.6.5, Django==2.2.3, qrcode==6.1, Pillow==6.2.1. Is there any way of confirming that my site is infact using the packages installed in my virtual env and not the global packages?

When I start a console using my virtualenv, I am able to import all of the above packages and generate a QR code without a problem. But when my Django site tries to do the same task (hopefully using the same venv and same packages) it throws an error.

The trackback is as follows (with some private info removed):

File "/home/ryangilmore/qr_docs/venv/lib/python3.6/site-packages/qrcode/image/" in <module> 6. from PIL import Image, ImageDraw

File "/home/ryangilmore/qr_docs/venv/lib/python3.6/site-packages/PIL/" in <module> 95. from . import _imaging as core

During handling of the above exception (cannot import name '_imaging'), another exception occurred:

File "/home/ryangilmore/qr_docs/venv/lib/python3.6/site-packages/django/core/handlers/" in inner 34. response = get_response(request)

File "/home/ryangilmore/qr_docs/venv/lib/python3.6/site-packages/django/core/handlers/" in _get_response 115. response = self.process_exception_by_middleware(e, request)

File "/home/ryangilmore/qr_docs/venv/lib/python3.6/site-packages/django/core/handlers/" in _get_response 113. response = wrapped_callback(request, callback_args, *callback_kwargs)

File "/home/ryangilmore/qr_docs/qr_docs/dochistory/" in DocQR 164. code = doc.qrcode()

File "/home/ryangilmore/qr_docs/qr_docs/dochistory/" in qrcode 179. code = qrcode.make(self.file.url)

File "/home/ryangilmore/qr_docs/venv/lib/python3.6/site-packages/qrcode/" in make 11. return qr.make_image()

File "/home/ryangilmore/qr_docs/venv/lib/python3.6/site-packages/qrcode/" in make_image 289. from qrcode.image.pil import PilImage

File "/home/ryangilmore/qr_docs/venv/lib/python3.6/site-packages/qrcode/image/" in <module> 8. import Image

Exception Type: ModuleNotFoundError at /qrcode/19 Exception Value: No module named 'Image'

The traceback is pretty clear that the exceptions are being raised from modules installed in /home/ryangilmore/qr_docs/venv. If you run

python -c "from PIL import Image; print(Image.__file__)"

in the console where you have the code working, what does it return?

Hi glenn, I appreciate your response. As I mentioned in my above post, all of the packages seem to work correctly in the console. When I run your code, it prints "/usr/lib/python3.6/site-packages/PIL/".

To add some context, the QR code is generated with the following bit and pieces of code:

Django model import qrcode from io import BytesIO

def qrcode(self): code = qrcode.make(self.file.url) buf = BytesIO() return buf.getvalue()

Django view def DocQR(request, pk): doc = Document.objects.get(pk=pk) code = doc.qrcode() response = HttpResponse(code, content_type='image/png') response['Content-Disposition'] = str('attachment; filename=qr_' return response

So the QR code is generated by the qrcode library and saved to BytesIO. I believe that this means that the PNG image exists in memory at this point. The image is then served as an HttpResponse through the Django view as a PNG image. Everything works fine in my local dev environment.

My best guesses are that either my PythonAnywhere app is having trouble finding the correct venv / packages, or there is some issue using these specific packages on this platform.

If anyone has any ideas, I would love to hear them. Otherwise I am going to create a new microapp (maybe with Flask) to just test this specific scenario.

When you're running it in the console, you're not using the virtualenv, you're using the PIL that is installed for all users. If you want your web app to work the same way as it is in the console, install the same versions of Pillow and qrcode that we have installed for the version of Python you're using for your web app.