-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
ENH: Gracefully handle python-build-standalone ImportError with Tk #30394
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
Changes from 1 commit
64d0b0c
bc2ed87
59b29f5
c332377
ed12aa9
6d1f6d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
tacaswell marked this conversation as resolved.
Outdated
|
||
| 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." | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be updated too to mention that a newer python-build-standalone version will fix this - for users of other tools (Poetry, mise, Bazel rules_python, etc.) there should be a path forward without changing the tools and workflow they're using, though it's not practical or necessary to specifically document what exactly they should do to upgrade. (Also, if you're editing this, small nit above - I would specifically say that you're using an outdated Python version, now that current uv-installed Python versions are compatible with Tk.)
timhoffm marked this conversation as resolved.
Outdated
|
||
| ) from e | ||
| else: | ||
| raise | ||
|
|
||
|
|
||
| _log = logging.getLogger(__name__) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to astral-sh/python-build-standalone#382, you might be able to use what
uvuses: https://github.com/astral-sh/uv/blob/c25c800367a5f43069f1c9d778cfd5de1bcc54a6/crates/uv-python/python/get_interpreter_info.py#L639-L645 (i.e.,sysconfigvalues).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that more helpful?
I think we should catch the error, not exclude python-build-standalone per-se. If they'd fix the tk issue, our versions would directly work. On
is_uv_python- I've explicitly tested for uv and tailored the error message, because I anticipate that'll by far the most route to trigger this, and I project that most uv users will not know what python-build-standalone is. So let's give them a uv-targeted error message.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't suggest excluding python-build-standalone, just detecting it differently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My point is I do not really care about python-build-standalone. The criterion is 1. does it raise the known error? And if so, is it a uv-install that I can point users to as the practical cause.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If that's the case that you only care about
uv, then the PR/commit title should be adjusted.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possibly my wording was not exactly precise.
The issue originates from the current way python-build-standalone is implmented. - So the PR title is justified.
Then there are two aspects to handling:
Detection: I want to detect the specific error and respond to that. - It's not helpful to detect python-build-standalone. They may improve to fix the error in the future. So issuing a warning/error purely on the fact that python-build-standalone is used would be presumptuous.
Error message: In the simplest case, we could just have the second error message
(or if you want to be more affirmative explicitly test for python-build-standalone and remove the "likely"). However, the by-far most common way to obtain a python-build-standalone python is via
uv. But it's a technical detail ofuvand users are most likely not aware of it. So telling them, they have python-build-standalone is not helpful for fixing the issue. Therefore, I added theuvdetection to tell them that uv-provided python is not compatible.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am 👍 on doing the work to detect if it is uv.