python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.11k stars 2.22k forks source link

ImportError: cannot import name '_imagingft' from 'PIL' #8273

Closed bryankaplan closed 1 month ago

bryankaplan commented 1 month ago

Goal

Ultimately I'm trying to run detectron2. I've isolated the Pillow problem separate from detectron2, but that goal impacts my installation, as noted below.

Steps to Reproduce

On Void Linux:

$ python -m venv venv
$ . ./venv/bin/activate
$ pip install pillow==9.5.0 --no-binary :all:
$ ipython
In [1]: from PIL import _imagingft
ImportError: cannot import name '_imagingft' from 'PIL'

Whoa, what's up with that pip install line?

  1. I'm installing 9.5 to work around this bug.
  2. I'm installing with --no-binary :all: based on advice given in #7949.

Expected Result

_imagingft is successfully imported.

Observed Result

ImportError: cannot import name '_imagingft' from 'PIL'

OS, Python and Pillow versions

python3 -m PIL --report output:

--------------------------------------------------------------------
Pillow 9.5.0
Python 3.12.4 (main, Jun  9 2024, 22:05:49) [GCC 13.2.0]
--------------------------------------------------------------------
Python modules loaded from /tmp/pil-test/venv/lib/python3.12/site-packages/PIL
Binary modules loaded from /tmp/pil-test/venv/lib/python3.12/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 9.5.0
*** TKINTER support not installed
*** FREETYPE2 support not installed
*** LITTLECMS2 support not installed
*** WEBP support not installed
*** WEBP Transparency support not installed
*** WEBPMUX support not installed
*** WEBP Animation support not installed
--- JPEG support ok, compiled for libjpeg-turbo 3.0.1
*** OPENJPEG (JPEG2000) support not installed
--- ZLIB (PNG/ZIP) support ok, loaded 1.3.1
*** LIBTIFF support not installed
*** RAQM (Bidirectional Text) support not installed
*** LIBIMAGEQUANT (Quantization method) support not installed
--- XCB (X protocol) support ok
--------------------------------------------------------------------
BLP
Extensions: .blp
Features: open, save, encode
--------------------------------------------------------------------
BMP image/bmp
Extensions: .bmp
Features: open, save
--------------------------------------------------------------------
BUFR
Extensions: .bufr
Features: open, save
--------------------------------------------------------------------
CUR
Extensions: .cur
Features: open
--------------------------------------------------------------------
DCX
Extensions: .dcx
Features: open
--------------------------------------------------------------------
DDS
Extensions: .dds
Features: open, save
--------------------------------------------------------------------
DIB image/bmp
Extensions: .dib
Features: open, save
--------------------------------------------------------------------
EPS application/postscript
Extensions: .eps, .ps
Features: open, save
--------------------------------------------------------------------
FITS
Extensions: .fit, .fits
Features: open, save
--------------------------------------------------------------------
FLI
Extensions: .flc, .fli
Features: open
--------------------------------------------------------------------
FTEX
Extensions: .ftc, .ftu
Features: open
--------------------------------------------------------------------
GBR
Extensions: .gbr
Features: open
--------------------------------------------------------------------
GIF image/gif
Extensions: .gif
Features: open, save, save_all
--------------------------------------------------------------------
GRIB
Extensions: .grib
Features: open, save
--------------------------------------------------------------------
HDF5
Extensions: .h5, .hdf
Features: open, save
--------------------------------------------------------------------
ICNS image/icns
Extensions: .icns
Features: open, save
--------------------------------------------------------------------
ICO image/x-icon
Extensions: .ico
Features: open, save
--------------------------------------------------------------------
IM
Extensions: .im
Features: open, save
--------------------------------------------------------------------
IMT
Features: open
--------------------------------------------------------------------
IPTC
Extensions: .iim
Features: open
--------------------------------------------------------------------
JPEG image/jpeg
Extensions: .jfif, .jpe, .jpeg, .jpg
Features: open, save
--------------------------------------------------------------------
JPEG2000 image/jp2
Extensions: .j2c, .j2k, .jp2, .jpc, .jpf, .jpx
Features: open, save
--------------------------------------------------------------------
MCIDAS
Features: open
--------------------------------------------------------------------
MPEG video/mpeg
Extensions: .mpeg, .mpg
Features: open
--------------------------------------------------------------------
MSP
Extensions: .msp
Features: open, save, decode
--------------------------------------------------------------------
PCD
Extensions: .pcd
Features: open
--------------------------------------------------------------------
PCX image/x-pcx
Extensions: .pcx
Features: open, save
--------------------------------------------------------------------
PIXAR
Extensions: .pxr
Features: open
--------------------------------------------------------------------
PNG image/png
Extensions: .apng, .png
Features: open, save, save_all
--------------------------------------------------------------------
PPM image/x-portable-anymap
Extensions: .pbm, .pgm, .pnm, .ppm
Features: open, save
--------------------------------------------------------------------
PSD image/vnd.adobe.photoshop
Extensions: .psd
Features: open
--------------------------------------------------------------------
QOI
Extensions: .qoi
Features: open
--------------------------------------------------------------------
SGI image/sgi
Extensions: .bw, .rgb, .rgba, .sgi
Features: open, save
--------------------------------------------------------------------
SPIDER
Features: open, save
--------------------------------------------------------------------
SUN
Extensions: .ras
Features: open
--------------------------------------------------------------------
TGA image/x-tga
Extensions: .icb, .tga, .vda, .vst
Features: open, save
--------------------------------------------------------------------
TIFF image/tiff
Extensions: .tif, .tiff
Features: open, save, save_all
--------------------------------------------------------------------
WEBP
Features: open
--------------------------------------------------------------------
WMF
Extensions: .emf, .wmf
Features: open, save
--------------------------------------------------------------------
XBM image/xbm
Extensions: .xbm
Features: open, save
--------------------------------------------------------------------
XPM image/xpm
Extensions: .xpm
Features: open
--------------------------------------------------------------------
XVTHUMB
Features: open
--------------------------------------------------------------------

Please include code that reproduces the issue

Done, above.

Thoughts

I reckon I'm probably missing a dependency, but I don't know what. I've tried installing freetype-devel, libjpeg-turbo-devel, and libraqm-devel to no avail.

aclark4life commented 1 month ago

Have you tried the void linux binary ?

Yay295 commented 1 month ago

ipython may not be using your local venv. Try pip install ipython after activating your environment.

nulano commented 1 month ago

I reckon I'm probably missing a dependency, but I don't know what. I've tried installing freetype-devel

If you're installing from source (which you are, since you used --no-binary :all:), then yes, you need freetype-devel. But that should be the only dependency you need for fonts if void linux is at all reasonable (which I assume it is).


ipython may not be using your local venv. Try pip install ipython after activating your environment.

Good point. @bryankaplan Does running the following produce the same output as your python3 -m PIL --report above?

from PIL import features
features.pilinfo()
bryankaplan commented 1 month ago

Have you tried the void linux binary ?

@aclark4life That's v10.4, and I need 9.5.

ipython may not be using your local venv. Try pip install ipython after activating your environment.

@Yay295 Yes, it is. This code was extracted from the larger project to demonstrate the problem. But I tried installing ipython in the venv, just to be sure, and indeed the ImportError is identical.

bryankaplan commented 1 month ago

Does running the following produce the same output as your python3 -m PIL --report above?

@nulano They're 100% identical. (I diff'd the output.)

nulano commented 1 month ago

Ok, I've managed to reproduce in a Docker container.

You probably tried to install Pillow before installing freetype-devel, which didn't work:

$ docker pull ghcr.io/void-linux/void-glibc-full
$ docker run --rm -it ghcr.io/void-linux/void-glibc-full
# xbps-install -Su
[...]
# xbps-install gcc python3-devel libjpeg-turbo-devel zlib-devel freetype-devel
[...]
# python -m venv venv
# . ./venv/bin/activate
(venv) # pip install pillow==9.5.0 --no-binary :all:
[...]
(venv) # python -c "from PIL import _imagingft; print(_imagingft.freetype2_version)"
ImportError: cannot import name '_imagingft' from 'PIL' (/venv/lib/python3.12/site-packages/PIL/__init__.py)

Even if you then install freetype-devel and try to install Pillow in a new venv, it will use a cached wheel:

(venv) # deactivate
# xbps-install freetype-devel
[...]
# python -m venv venv2
# . ./venv2/bin/activate
(venv2) # pip install pillow==9.5.0 --no-binary :all:
Collecting pillow==9.5.0
  Using cached Pillow-9.5.0-cp312-cp312-linux_x86_64.whl
Installing collected packages: pillow
Successfully installed pillow-9.5.0
(venv2) # python -c "from PIL import _imagingft; print(_imagingft.freetype2_version)"
ImportError: cannot import name '_imagingft' from 'PIL' (/venv/lib/python3.12/site-packages/PIL/__init__.py)

You have to add a parameter to pip to ask it to ignore the cached wheel (--no-cache-dir Pillow), or just remove the wheel from cache with pip cache remove Pillow:

(venv2) # deactivate
# python -m venv venv3
# . ./venv3/bin/activate
(venv3) # pip cache remove Pillow
(venv3) # pip install pillow==9.5.0 --no-binary :all:
[...]
(venv3) # python -c "from PIL import _imagingft; print(_imagingft.freetype2_version)"
2.13.2

Does that help?

bryankaplan commented 1 month ago

@nulano Thank you! I knew as soon as I started reading your thought that it was probably the answer, and I started typing this response before the test was complete. Test succeeded. Thank you so much!