Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't import PIL after installing Pillow with Poetry

I'm trying to install and use Pillow with Python 3.9.2 (managed with pyenv). I'm using Poetry to manage my virtual environments and dependencies, so I ran poetry add pillow, which successfully added Pillow = "^8.2.0" to my pyproject.toml. Per the Pillow docs, I added from PIL import Image in my script, but when I try to run it, I get:

File "<long/path/to/file.py>", line 3, in <module>
    from PIL import Image
ModuleNotFoundError: No module named 'PIL'

When I look in the venv Poetry is creating for me, I can see a PIL directory (/long/path/lib/python3.9/site-packages/PIL/) and an Image.py file inside it.

What am I missing here? I've tried:

  • Downcasing to from pil import Image per this; did not work
  • Downgrading to lower versions of Python and PIL; works, but defeats the purpose
  • ETA: Exporting a requirements.txt file from Poetry, creating a virtualenv with venv, and installing the packages manually; works, but cuts me off from using Poetry/pyproject.toml

Any help would be tremendously appreciated.

like image 461
Michael Avatar asked Jan 30 '26 19:01

Michael


1 Answers

I couldn't find a way to solve this either (using poetry 1.1.13).

Ultimately, I resorted to a workaround of poetry add pillow && pip install pillow so I could move on with my life. :P

poetry add pillow gets the dependency in to the TOML, so consumers of the package should be OK.

like image 82
Gordon Bean Avatar answered Feb 02 '26 11:02

Gordon Bean