astral-sh / uv

An extremely fast Python package and project manager, written in Rust.
https://docs.astral.sh/uv
Apache License 2.0
23.39k stars 671 forks source link

tkagg is not compatible with uv-managed Python installs #6893

Open bluthej opened 1 month ago

bluthej commented 1 month ago

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:

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!

spaceman-cb commented 1 month ago

This is a big issue for me as well. Any help would be appreciated.

spaceman-cb commented 1 month ago

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

konstin commented 1 month ago

What operating system and Python version do you use, and do you set and custom matplotlib settings? For me, with Ubuntu 24.04 and cpython-3.12.1-linux-x86_64-gnu:

pyproject.toml

[project]
name = "mpl-basic"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
    "matplotlib>=3.9.2",
]

main.py

import matplotlib.pyplot as plt

if __name__ == '__main__':
    print(plt.get_backend())

    plt.plot([0, 1], [0, 1])
    plt.savefig("hello.png")

With uv run main.py, I get agg as backend and a hello.png.

tpgillam commented 1 month ago

Running into this too. savefig works fine with the standalone builds; the problem is when trying to bring up an interactive window.

For example, when running:

from matplotlib import pyplot

pyplot.plot([0, 1], [0, 1])
pyplot.show()

gives the warning on the call to pyplot.show() (and nothing else of interest happens besides the program terminating)

UserWarning: FigureCanvasAgg is non-interactive, and thus cannot be shown

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 PyQt6 installed 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 call matplotlib.use explicitly).

spaceman-cb commented 1 month ago

I am on a mac and was running matplotlib 3.9.1 and 3.9.0. savefig('foo.pdf') was causing issues. Not sure why it was trying to use tk when nothing about it was interactive.

bluthej commented 1 month ago

@konstin as I said in the issue, the problems I'm having are with the interactive backend, the static backend indeed works just fine out of the box (just like what @tpgillam said).