Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d3491dc

Browse files
authored
Merge pull request #30394 from timhoffm/python-build-standalone-importerror
ENH: Gracefully handle python-build-standalone ImportError with Tk
2 parents e401f82 + 6d1f6d3 commit d3491dc

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

lib/matplotlib/backends/_backend_tk.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,36 @@
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 appear to be using an outdated "
41+
"version of uv's managed Python distribution which is not compatible "
42+
"with Tk. Please upgrade to the latest uv version, then update "
43+
"Python with: `uv python upgrade --reinstall`"
44+
) from e
45+
else:
46+
raise ImportError(
47+
"Failed to import tkagg backend. This is likely caused by using a "
48+
"Python executable based on python-build-standalone, which is not "
49+
"compatible with Tk. Recent versions of python-build-standalone "
50+
"should be compatible with Tk. Please update your python version "
51+
"or select another backend."
52+
) from e
53+
else:
54+
raise
2755

2856

2957
_log = logging.getLogger(__name__)

0 commit comments

Comments
 (0)