Skip to content

Commit 64d0b0c

Browse files
committed
ENH: Gracefully handle python-build-standalone ImportError with Tk.
Closes matplotlib#30390.
1 parent a797f94 commit 64d0b0c

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

lib/matplotlib/backends/_backend_tk.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,34 @@
2222
TimerBase, ToolContainerBase, cursors, _Mode, MouseButton,
2323
CloseEvent, KeyEvent, LocationEvent, MouseEvent, ResizeEvent)
2424
from matplotlib._pylab_helpers import Gcf
25-
from . import _tkagg
26-
from ._tkagg import TK_PHOTO_COMPOSITE_OVERLAY, TK_PHOTO_COMPOSITE_SET
25+
26+
try:
27+
from . import _tkagg
28+
from ._tkagg import TK_PHOTO_COMPOSITE_OVERLAY, TK_PHOTO_COMPOSITE_SET
29+
except ImportError as e:
30+
# catch incompatibility of python-build-standalone with Tk
31+
cause1 = getattr(e, '__cause__', None)
32+
cause2 = getattr(cause1, '__cause__', None)
33+
if (isinstance(cause1, ImportError) and
34+
isinstance(cause2, AttributeError) and
35+
"'_tkinter' has no attribute '__file__'" in str(cause2)):
36+
37+
is_uv_python = "/uv/python" in (os.path.realpath(sys.executable))
38+
if is_uv_python:
39+
raise ImportError(
40+
"Failed to import tkagg backend. You are using a uv-installed python "
41+
"executable, which is not compatible with Tk. "
42+
"Please use another Python interpreter or select another backend."
43+
) from e
44+
else:
45+
raise ImportError(
46+
"Failed to import tkagg backend. This is likely caused by using a "
47+
"Python executable based on python-build-standalone, which is not "
48+
"compatible with Tk. "
49+
"Please use another Python interpreter or select another backend."
50+
) from e
51+
else:
52+
raise
2753

2854

2955
_log = logging.getLogger(__name__)

0 commit comments

Comments
 (0)