0

I looked at a lot of SO posts for this error and none of the answers address or resolve the error. Here is the pipfile:

    [[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
django = "*"
boto3 = "*"
psycopg2 = "*"
pillow = "*"
django-storages = "*"
django-crispy-forms = "*"
django-s3-storage = "*"
zappa-django-utils = "*"
psycopg2-binary = "==2.8.6"
django-environ = "*"
django-mptt = "*"
djangorestframework = "*"
mortgage = "*"
stripe = "*"
django-phonenumber-field = "*"
phonenumbers = "*"
django-mathfilters = "*"
image = "*"

[dev-packages]
mock = "*"
model-mommy = "*"
django-debug-toolbar = "*"

[requires]
python_version = "3.8"

As you can see, I have both the pillow and image packages installed. But, when I run py manage.py shell, I get this error:

import PIL
ModuleNotFoundError: No module named 'PIL'

In my user_profile app, I have the following:

import PIL
from PIL import Image
...

I tried uninstalling different PIL-related packages, I could run py manage.py shell without running into the ModuleNotFoundError: No module named 'PIL'. However, once deployed using AWS lambda, I ran into another error:

[ERROR] ImportError: cannot import name '_imaging' from 'PIL' (/var/task/PIL/__init__.py)
...
File "/var/task/comments/models.py", line 5, in <module>
  from user_profile.models import UserProfile
File "/var/task/user_profile/models.py", line 13, in <module>
  from PIL import Image
File "/var/task/PIL/Image.py", line 114, in <module>
  from . import _imaging as core

So, I went back and installed pillow (pipenv install pillow) which shows up in my Pipfile as pillow = "*". Once again deployed using AWS Lambda, and once again the same error message as above (from . import _imaging as core)

EarlyCoder
  • 1,213
  • 2
  • 18
  • 42
  • Does this answer your question? [ImportError: No module named PIL](https://stackoverflow.com/questions/8863917/importerror-no-module-named-pil) – Brian Destura Aug 17 '21 at 01:05
  • Looks like you using different interpreters to run Django server and interactive shell via command line (something alternative installations like pyenv or venv?) – rzlvmp Aug 17 '21 at 01:09
  • 1
    @bdbd, the post you are referring to is helpful and I did come across it prior to posting the question. Unfortunately, it did not work for me. I decided to uninstall all the PIL-related packages (pillow and image) and reinstall to see if that does it. When I unistalled all and reinstalled image package, it threw another exception. Then, I unistalled image too (```pipenv uninstall image```) and now I have no PIL related package, yet it may be working (as I am dealing with another unrelated issue). When I have a definitive answer, I'll answer my own post – EarlyCoder Aug 17 '21 at 01:33

1 Answers1

0

The problem I ran into was due to the fact that Pillow no longer supports import _imaging:

Warning: 
Pillow >= 2.1.0 no longer supports import _imaging. Please use from PIL.Image import core as _imaging instead.

To resolve the problem, you'd need to add

from PIL.Image import core as _imaging

below from PIL import Image in your code.

EarlyCoder
  • 1,213
  • 2
  • 18
  • 42