|  | 
| 22 | 22 |     TimerBase, ToolContainerBase, cursors, _Mode, MouseButton, | 
| 23 | 23 |     CloseEvent, KeyEvent, LocationEvent, MouseEvent, ResizeEvent) | 
| 24 | 24 | 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 | 
| 27 | 55 | 
 | 
| 28 | 56 | 
 | 
| 29 | 57 | _log = logging.getLogger(__name__) | 
|  | 
0 commit comments