From 64d0b0cef9a1b874f7799a79487d145c488be557 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Tue, 5 Aug 2025 22:47:30 +0200 Subject: [PATCH 1/6] ENH: Gracefully handle python-build-standalone ImportError with Tk. Closes #30390. --- lib/matplotlib/backends/_backend_tk.py | 30 ++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/backends/_backend_tk.py b/lib/matplotlib/backends/_backend_tk.py index eaf868fd8bec..d8cd3e94d104 100644 --- a/lib/matplotlib/backends/_backend_tk.py +++ b/lib/matplotlib/backends/_backend_tk.py @@ -22,8 +22,34 @@ TimerBase, ToolContainerBase, cursors, _Mode, MouseButton, CloseEvent, KeyEvent, LocationEvent, MouseEvent, ResizeEvent) from matplotlib._pylab_helpers import Gcf -from . import _tkagg -from ._tkagg import TK_PHOTO_COMPOSITE_OVERLAY, TK_PHOTO_COMPOSITE_SET + +try: + from . import _tkagg + from ._tkagg import TK_PHOTO_COMPOSITE_OVERLAY, TK_PHOTO_COMPOSITE_SET +except ImportError as e: + # catch incompatibility of python-build-standalone with Tk + cause1 = getattr(e, '__cause__', None) + cause2 = getattr(cause1, '__cause__', None) + if (isinstance(cause1, ImportError) and + isinstance(cause2, AttributeError) and + "'_tkinter' has no attribute '__file__'" in str(cause2)): + + is_uv_python = "/uv/python" in (os.path.realpath(sys.executable)) + if is_uv_python: + raise ImportError( + "Failed to import tkagg backend. You are using a uv-installed python " + "executable, which is not compatible with Tk. " + "Please use another Python interpreter or select another backend." + ) from e + else: + raise ImportError( + "Failed to import tkagg backend. This is likely caused by using a " + "Python executable based on python-build-standalone, which is not " + "compatible with Tk. " + "Please use another Python interpreter or select another backend." + ) from e + else: + raise _log = logging.getLogger(__name__) From bc2ed8750bc93cd490bad91d0adaf04241809d15 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 21 Aug 2025 15:58:22 -0400 Subject: [PATCH 2/6] DOC: add canonical uv update command in error message --- lib/matplotlib/backends/_backend_tk.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/backends/_backend_tk.py b/lib/matplotlib/backends/_backend_tk.py index d8cd3e94d104..5b22ec6b5d0e 100644 --- a/lib/matplotlib/backends/_backend_tk.py +++ b/lib/matplotlib/backends/_backend_tk.py @@ -39,7 +39,8 @@ raise ImportError( "Failed to import tkagg backend. You are using a uv-installed python " "executable, which is not compatible with Tk. " - "Please use another Python interpreter or select another backend." + "Please update your python via: " + "`uv self update && uv python upgrade --reinstall`" ) from e else: raise ImportError( From 59b29f5ecf314879bd227b4e1c0012cfa6047922 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Fri, 22 Aug 2025 13:44:12 -0400 Subject: [PATCH 3/6] DOC: tweak suggestion for upgrading uv managed Python Co-authored-by: Zanie Blue --- lib/matplotlib/backends/_backend_tk.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/backends/_backend_tk.py b/lib/matplotlib/backends/_backend_tk.py index 5b22ec6b5d0e..5f49add4fe55 100644 --- a/lib/matplotlib/backends/_backend_tk.py +++ b/lib/matplotlib/backends/_backend_tk.py @@ -37,10 +37,10 @@ is_uv_python = "/uv/python" in (os.path.realpath(sys.executable)) if is_uv_python: raise ImportError( - "Failed to import tkagg backend. You are using a uv-installed python " - "executable, which is not compatible with Tk. " - "Please update your python via: " - "`uv self update && uv python upgrade --reinstall`" + "Failed to import tkagg backend. You appear to be using an outdated " + "version of uv's managed Python distribution which is not compatible with Tk. " + "Please upgrade to the latest uv version, then update Python with:" + "`uv python upgrade --reinstall`" ) from e else: raise ImportError( From c332377bcc14b3826e4500a2da730d3c3552ac09 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Sun, 24 Aug 2025 03:22:47 +0200 Subject: [PATCH 4/6] Fix line length --- lib/matplotlib/backends/_backend_tk.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/backends/_backend_tk.py b/lib/matplotlib/backends/_backend_tk.py index 5f49add4fe55..b1d5521577d4 100644 --- a/lib/matplotlib/backends/_backend_tk.py +++ b/lib/matplotlib/backends/_backend_tk.py @@ -38,9 +38,9 @@ if is_uv_python: raise ImportError( "Failed to import tkagg backend. You appear to be using an outdated " - "version of uv's managed Python distribution which is not compatible with Tk. " - "Please upgrade to the latest uv version, then update Python with:" - "`uv python upgrade --reinstall`" + "version of uv's managed Python distribution which is not compatible " + " with Tk. Please upgrade to the latest uv version, then update " + " Python with: `uv python upgrade --reinstall`" ) from e else: raise ImportError( From ed12aa97ba67b673693994ca31458f6715fe1734 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Sun, 24 Aug 2025 03:23:35 +0200 Subject: [PATCH 5/6] Mentioned fix in recent python-build-standalone --- lib/matplotlib/backends/_backend_tk.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/backends/_backend_tk.py b/lib/matplotlib/backends/_backend_tk.py index b1d5521577d4..bec93c2b0ec6 100644 --- a/lib/matplotlib/backends/_backend_tk.py +++ b/lib/matplotlib/backends/_backend_tk.py @@ -46,8 +46,9 @@ raise ImportError( "Failed to import tkagg backend. This is likely caused by using a " "Python executable based on python-build-standalone, which is not " - "compatible with Tk. " - "Please use another Python interpreter or select another backend." + "compatible with Tk. Recent versions of python-build-standalone " + "should be compatible with Tk. Please update your python version " + "or select another backend." ) from e else: raise From 6d1f6d3548dbe82aa3b63007a76ec59ed63ac4de Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Sun, 24 Aug 2025 03:24:28 +0200 Subject: [PATCH 6/6] whitespace fix --- lib/matplotlib/backends/_backend_tk.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/backends/_backend_tk.py b/lib/matplotlib/backends/_backend_tk.py index bec93c2b0ec6..e547b88329f3 100644 --- a/lib/matplotlib/backends/_backend_tk.py +++ b/lib/matplotlib/backends/_backend_tk.py @@ -39,8 +39,8 @@ raise ImportError( "Failed to import tkagg backend. You appear to be using an outdated " "version of uv's managed Python distribution which is not compatible " - " with Tk. Please upgrade to the latest uv version, then update " - " Python with: `uv python upgrade --reinstall`" + "with Tk. Please upgrade to the latest uv version, then update " + "Python with: `uv python upgrade --reinstall`" ) from e else: raise ImportError(