uv: tkagg is not compatible with uv-managed Python installs
From the matplot lib issue tracker (comment), apparently:
python-build-standalone is not compatible with tkagg
I am on Linux and it seems like tkagg is the default interactive backend (not sure about other platforms):
❯ python -c "import matplotlib.pyplot as plt; print(plt.get_backend())"
tkagg
Since uv relies on python-build-standalone for managed installs, this has the unfortunate consequence that I have to use my system Python whenever I need to plot something in interactive mode.
This is related to this matplotlib issue and this python-build-standalone issue, at which point things were even worse because people couldn’t even import matplotlib successfully!
It sounds like python-build-standalone and tkagg will not be compatible anytime soon. I don’t necessarily mind, but in any case I think it would be nice if we could either:
- document this issue and recommend a different interactive backend that works with python-build-standalone, along with the steps to take to make that work
- take some steps on the python-build-standalone side so that there is some functioning interactive backend by default
Hardly a day goes by that I don’t need to plot something with matplotlib and I assume I am not alone, so this should benefit a lot of people!
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 47
- Comments: 30 (5 by maintainers)
Commits related to this issue
- DOC: Add install instructions for pixi and uv Closes #29746. Notes on issues with uv an tk taken from https://github.com/astral-sh/uv/issues/6893#issuecomment-2565965851 — committed to timhoffm/matplotlib by timhoffm 2 years ago
- DOC: Add install instructions for pixi and uv Closes #29746. Notes on issues with uv an tk taken from https://github.com/astral-sh/uv/issues/6893#issuecomment-2565965851 — committed to timhoffm/matplotlib by timhoffm 2 years ago
- DOC: Add install instructions for pixi and uv (#29752) * DOC: Add install instructions for pixi and uv Closes #29746. Notes on issues with uv an tk taken from https://github.com/astral-sh/uv/issues... — committed to matplotlib/matplotlib by timhoffm 2 years ago
I stand by my comment in https://github.com/matplotlib/matplotlib/issues/23074#issuecomment-1134052145 that this is an issue that needs to be handled, if at all, on the python-build-standalone/astral side.
Matplotlib has been using this method of loading the tk binding from 2016 (via https://github.com/matplotlib/matplotlib/pull/6442) in mpl 1.5. The motivation for doing this and the dlopen was to make the built wheels independent of the version of tk/x11 that was in the multibuild (predecessor to cibuildwheel) and now cibuildwheel container where we build the wheels. Without doing this, if that version did not exactly match the version that the users’ Python was built with we would expect crashes. A lot of this pain is inherent to wheels (see https://pypackaging-native.github.io for more details) and we don’t, to my knowledge, have these issues with any of the other packaging ecosystems (linux packages, brew, conda, …).
With I the matrix of compatibility for python-build-stand-alone after the symbols got hidden:
For version of python-build-standalone from before the symbols were hidden for Matplotlib >3.4 I am seeing segfaults locally (I only tested back to 3.5, but what I suspect is the source of the problem is in mpl3.4). I suspect the issue is that we use
dlopen("libX11.so.6", RTLD_LAZY)to see if libX11 is available. We use libX11 as part of detecting if we are running in a graphical environment as part of detecting if the user has already imported an GUI framework or even have an available display. Setting$DISPLAYto an empty string (which skips the check) avoids the segfault but of course will force mpl back to only non-interactive backends. This seems consistent with the quirk note about pyqt + x11. It may be the case that my local X11 too new and/or just built differently and other systems may not see the segfault. I’m also not sure if on pure wayland systems there is anlibX11.soto find.This is what I have been using for testing:
setting whatever Python version.
I don’t have a Windows system setup to test at the moment, but we use a different method of finding the symbols (by inspecting dlls loaded to the current process) so I suspect we side-step this problem, but I’m not sure.
With mpl >3.6 the auto-backend selection no longer raises, but if the user explicitly asks for
tkaggit will fail but I think this is OK as there are not any usable tk libraries available. By analogy, if the user asks for tk with a Python build that does not include tk at all, failing is the correct behavior.I think the long term paths out of this are:
tkintermodule pulled out of the standard library all together and only live on pypi. I think this is technically possible as debian has already done this split but I have no idea how politically possible it is. This is again assuming the segfault can be fixed.An (I assume off the table) option is for astral to build custom Matplotlib wheels to work with python-build-standalone (you could revert our runtime logic to instead statically link to the correct tk and x11 libraries at your build time, that code is pretty static so keeping a local patch up-to-date should not be onerous) and then swap them in for users instead of the ones from pypi when using
uv. Doing this would mean the Matplotlib wheels could be kept in exact sync with the build details of python-build-standalone (you could also do PyQt, PySides, and the rest of the GUI toolkits for the same reason), but would open up a whole bunch of other cans of worms 🦋 .For users that need a fix now rather than when we have solved Python packaging I suggest:
uv add pyqt6for pyqt (although, per https://github.com/astral-sh/python-build-standalone/blob/main/docs/quirks.rst#static-linking-of-libx11--incompatibility-with-pyqt-on-linux there is a chance this won’t actually work),uv.uv’s issues tracker) usepixi(https://prefix.dev) which primarily pulls its built artifacts from conda-forge (so they are all built consistently which avoids all of theses problems) and for anything that is not packaged on conda-forge will also add pypi / local dependencies. pixi is built around a similar project-based-venv workflow as uv.Running into this too.
savefigworks fine with the standalone builds; the problem is when trying to bring up an interactive window.For example, when running:
gives the warning on the call to
pyplot.show()(and nothing else of interest happens besides the program terminating)I’m running with cpython-3.11.9-linux-x86_64-gnu on WSL (ubuntu 22.04) right now.
edit: my current workaround is to have
PyQt6installed in whichever environment I’m working, as matplotlib will then pick that up as the default interactive backend without any further intervention (I haven’t set additional environment variables or callmatplotlib.useexplicitly).After lots of trouble,
uv pip install PyQt6is the only thing that got matplotlib interactive backend to work inside my venv!Use qt instead 🤓
For people coming here and needing immediate help, setting your backend as described here may solve your problem: https://matplotlib.org/stable/users/explain/figure/backends.html
Thanks! Replied to your comment over there but tl;dr I think we’re going to have to move to building libtcl, libtk, and
_tkinteras dynamic libraries so that uv’s managed Python build acts enough like every other Python build for things to work as expected.This is also an issue for me, I just tried to run
and I’m getting the error:
Thanks for pointing it out. I already tried it out to no avail. Also tried doing
export QT_QPA_PLATFORM = offscreenand I get something along the lines ofraise() not supported by plugin. Tried with PyQt5, PyQt6, the whole nine yards. Just can’t get it to work.Makes sense. I would just like to add a link to https://github.com/astral-sh/uv/issues/11942 & https://github.com/astral-sh/python-build-standalone/issues/146 , to make it clear to other thread readers, that there are a lot of other projects that depend on tkinter that are potentially breaking, and not only tkagg/matplotlib, such as FreeSimpleGUI etc.
@pautric looks like you’re using
pyenv? That’s a separate problem from this issue — and unrelated to uv as far as I can tell.pyenvbuilds Python on your machine, perhaps it did so without Tk available.I’m sorry, but as Charlie said, this is extremely unlikely for us. We make it easy to disable our Python versions and use system interpreters for this reason. There are other tools that focus on building Python from source.
Please let’s keep discussion here focused on the interaction between tkagg, matplotlib, and python-build-standalone.
I appreciate your perspective but I think we’re extremely unlikely to move towards requiring users to build Python from source. It does a huge disservice to users by pushing the complexity onto them. The solution here is to fix the issue in the matplotlib integration 😃
I have a vague recollection that installing libxcb-cursor0 from the operating system’s package manager helped me with a similar issue once (but it was a while ago so can’t remember the details I’m afraid!)