-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make import PIL; PIL.Image
work
#6614
Comments
Alternatively, maybe we should only allow the recommended https://pillow.readthedocs.io/en/stable/installation.html#warnings |
Or |
I don't find this issue that strange, but yes, I guess I would deprecate Also we have been given control of PIL on PyPI, but we've not done anything with it. Enabling So, >>> import PIL.Image
Please import Image from PIL.
>>> import PIL
Please import Image from PIL.
>>> import Pillow
Please import Image from PIL.
Something like that. |
Just to point it out - since the removal of I'm not opposed to removing |
I feel you. I maintain Hy, which isn't even 1.0 yet, and with every breaking change, however well-motivated, comes the wailing and gnashing of teeth. |
import PIL; PIL.Image
work unconditionallyimport PIL; PIL.Image
work
@radarhere I believe the most breaking change ever happened in Pillow 1.0 when Anyway, again, I don't feel particularly inclined to add support to Pillow such that Correct me if I'm wrong, but to make If so, I definitely don't see that happening. And if not, I'm not sure I see it happening without adding some workaround that would arguably be more confusing than just doing nothing. 🤷 |
After experimenting, I'm not convinced that it's possible to tell from within Pillow if If I'm wrong, feel free to enlighten me. |
OP is asking for mkdir PIL/Image
mv PIL/Image.py PIL/Image/__init__.py Actually, the above alone doesn't help: ➜ Developer mkdir PIL
➜ Developer touch PIL/__init__.py
➜ Developer mkdir PIL/Image
➜ Developer touch PIL/Image/__init__.py
➜ Developer python3
Python 3.10.6 (main, Aug 30 2022, 04:58:14) [Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL
>>> PIL.__file__
'/Users/alexclark/Developer/PIL/__init__.py'
>>> PIL.Image
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'PIL' has no attribute 'Image' However, if Python 3.10.6 (main, Aug 30 2022, 04:58:14) [Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL
>>> PIL.Image
<module 'PIL.Image' from '/Users/alexclark/Developer/PIL/Image/__init__.py'> Which means maybe we could support this by adding ➜ Developer mkdir PIL
➜ Developer touch PIL/__init__.py
➜ Developer touch PIL/Image.py
➜ Developer vi PIL/__init__.py
➜ Developer python3
Python 3.10.6 (main, Aug 30 2022, 04:58:14) [Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL
>>> PIL.Image
<module 'PIL.Image' from '/Users/alexclark/Developer/PIL/Image.py'>
Whether or not we should do this, I don't know. Current PIL """Pillow (Fork of the Python Imaging Library)
Pillow is the friendly PIL fork by Alex Clark and Contributors.
https://github.com/python-pillow/Pillow/
Pillow is forked from PIL 1.1.7.
PIL is the Python Imaging Library by Fredrik Lundh and Contributors.
Copyright (c) 1999 by Secret Labs AB.
Use PIL.__version__ for this Pillow version.
;-)
"""
from . import _version
# VERSION was removed in Pillow 6.0.0.
# PILLOW_VERSION was removed in Pillow 9.0.0.
# Use __version__ instead.
__version__ = _version.__version__
del _version
_plugins = [
"BlpImagePlugin",
"BmpImagePlugin",
"BufrStubImagePlugin",
"CurImagePlugin",
"DcxImagePlugin",
"DdsImagePlugin",
"EpsImagePlugin",
"FitsImagePlugin",
"FitsStubImagePlugin",
"FliImagePlugin",
"FpxImagePlugin",
"FtexImagePlugin",
"GbrImagePlugin",
"GifImagePlugin",
"GribStubImagePlugin",
"Hdf5StubImagePlugin",
"IcnsImagePlugin",
"IcoImagePlugin",
"ImImagePlugin",
"ImtImagePlugin",
"IptcImagePlugin",
"JpegImagePlugin",
"Jpeg2KImagePlugin",
"McIdasImagePlugin",
"MicImagePlugin",
"MpegImagePlugin",
"MpoImagePlugin",
"MspImagePlugin",
"PalmImagePlugin",
"PcdImagePlugin",
"PcxImagePlugin",
"PdfImagePlugin",
"PixarImagePlugin",
"PngImagePlugin",
"PpmImagePlugin",
"PsdImagePlugin",
"SgiImagePlugin",
"SpiderImagePlugin",
"SunImagePlugin",
"TgaImagePlugin",
"TiffImagePlugin",
"WebPImagePlugin",
"WmfImagePlugin",
"XbmImagePlugin",
"XpmImagePlugin",
"XVThumbImagePlugin",
]
class UnidentifiedImageError(OSError):
"""
Raised in :py:meth:`PIL.Image.open` if an image cannot be opened and identified.
"""
pass So I would say if adding |
Trying the suggestion from the previous comment, I initially hit a circular import, but I was able to work around it. So the following code does allow diff --git a/src/PIL/Image.py b/src/PIL/Image.py
index cf9ab2df6..8692bd7b9 100644
--- a/src/PIL/Image.py
+++ b/src/PIL/Image.py
@@ -51,10 +51,8 @@ from . import (
ExifTags,
ImageMode,
TiffTags,
- UnidentifiedImageError,
- __version__,
- _plugins,
)
+from ._version import __version__
from ._binary import i32le, o32be, o32le
from ._deprecate import deprecate
from ._util import DeferredError, is_path
@@ -375,6 +373,8 @@ def init():
if _initialized >= 2:
return 0
+ from . import _plugins
+
for plugin in _plugins:
try:
logger.debug("Importing %s", plugin)
@@ -3289,6 +3289,8 @@ def open(fp, mode="r", formats=None):
fp.close()
for message in accept_warnings:
warnings.warn(message)
+ from . import UnidentifiedImageError
+
msg = "cannot identify image file %r" % (filename if filename else fp)
raise UnidentifiedImageError(msg)
diff --git a/src/PIL/__init__.py b/src/PIL/__init__.py
index 0e6f82092..31a2c41ed 100644
--- a/src/PIL/__init__.py
+++ b/src/PIL/__init__.py
@@ -13,6 +13,7 @@ Use PIL.__version__ for this Pillow version.
;-)
"""
+from . import Image
from . import _version
# VERSION was removed in Pillow 6.0.0.
diff --git a/src/PIL/_deprecate.py b/src/PIL/_deprecate.py
index fa6e1d00c..24523a023 100644
--- a/src/PIL/_deprecate.py
+++ b/src/PIL/_deprecate.py
@@ -2,7 +2,7 @@ from __future__ import annotations
import warnings
-from . import __version__
+from ._version import __version__
def deprecate( However, it doesn't allow |
That If you want to avoid users having to import the sub-module isn't adding |
Can you please clarify what "go through the effort to break it" means in this case? For example in 2.1.0 we broke |
Which I read as "eventually break At the end of the day you have:
To make diff --git a/src/PIL/__init__.py b/src/PIL/__init__.py
index 63a45769b..85b26f25c 100644
--- a/src/PIL/__init__.py
+++ b/src/PIL/__init__.py
@@ -84,3 +84,9 @@ class UnidentifiedImageError(OSError):
"""
pass
+
+
+try:
+ from . import Image # noqa, must be last because it uses _plugins
+except ImportError:
+ ... is enough to make the OP's request work. It has to be at the end (and ignore the QA for "imports at the top" because of |
So, the code from the original post is about an interaction between matplotlib and Pillow. A matplotlib maintainer has weighed in on this and said that the behaviour is fine. So perhaps we aren't concerned about the way our two libraries relate. Setting that aside then, my thoughts are
I'm inclined to close this. |
Close-away, however I don't recall why |
It's not that import matplotlib, PIL; PIL.Image is really import PIL.Image, PIL; PIL.Image |
It's not even necessarily that, since >>> from PIL import Image
>>> import PIL
>>> PIL.Image
<module 'PIL.Image' from 'C:\\Users\\Nulano\\AppData\\Roaming\\Python\\Python311\\site-packages\\PIL\\Image.py'> |
With Pillow 9.2.0 and (e.g.) matplotlib 3.5.2:
This behavior is quite strange.
The text was updated successfully, but these errors were encountered: