0

When I run this in the terminal it imports without an error:

from PIL import Image

When I run the same code in my Flask app running over Apache/WSGI, it causes an error:

cannot import name '_imaging' from 'PIL' (/var/www/app/env/lib/python3.9/site-packages/PIL/__init__.py)

I installed Pillow into the virtual environment correctly. I did not install the older PIL package and have checked a dozen times to make sure it's not installed and causing a conflict.

I installed WSGI for Python 3 like this:

sudo apt-get install libapache2-mod-wsgi-py3

EVERYTHING about the app, including all other Python modules/dependencies has been running just fine for months. Pillow is the only one that doesn't work in the deployed app, but does work when run from the terminal.

My Apache configuration file includes this reference to the virtual environment location:

WSGIPythonHome "/var/www/app/env"

I've tried older versions of Pillow with no success. I've tried uninstalling and reinstalling Pillow. I've tried nuking my virtual environment and creating it again from scratch. Kind of at a loss here.

davidism
  • 121,510
  • 29
  • 395
  • 339
  • 1
    You cannot install `libapache2-mod-wsgi-py3` not for system python of specific version for which it was compiled. DIY – STerliakov Dec 22 '22 at 19:27
  • 1
    Does this answer your question? [How to install & configure mod\_wsgi for py3](https://stackoverflow.com/questions/19344252/how-to-install-configure-mod-wsgi-for-py3) – STerliakov Dec 22 '22 at 19:27
  • Are you saying that `libapache2-mod-wsgi-py3` cannot be used if you have a specific version of Python that is NOT the system version of Python? Either way, I solved the issue by uninstalling `libapache2-mod-wsgi-py3` and used `pip install mod_wsgi` within my virtual env. Then I moved the mod_wsgi.so file it created into my Apache `modules` directory and edited `mods-available/wsgi.load` to include a path to the .so file. This is the specific answer that SOLVED it for me: https://stackoverflow.com/questions/43661508/error-with-compiled-pillow-on-python-3-6-virtualenv/43671174#43671174 – Luke Peters Dec 22 '22 at 22:29
  • 1
    Yes, exactly: `libapache2-mod-wsgi-py3` cannot be used for custom python installations or venv's. It may occasionally work, but is unreliable, at least. – STerliakov Dec 23 '22 at 08:36
  • Good to know, thank you! Learned this lesson the hard way – Luke Peters Dec 23 '22 at 20:20

1 Answers1

0

I solved the problem by uninstalling libapache2-mod-wsgi-py3 and using pip install mod_wsgi inside my virtual environment instead.

I followed this particular answer specifically: Error with compiled Pillow on Python 3.6 virtualenv