uv: Can't use tkinter with new venv set up with uv
(Maintainer edit)
NOTE If you are still affected by this, try reinstalling the managed Python interpreter
python install --reinstall ...
uv venv
source .venv/bin/activate
python -c "from tkinter import Tk; window = Tk()"
This gives me the error message:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/dstansby/.local/share/uv/python/cpython-3.12.5-macos-aarch64-none/lib/python3.12/tkinter/__init__.py", line 2346, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_tkinter.TclError: Can't find a usable init.tcl in the following directories:
/tools/deps/lib/tcl8.6 /Users/dstansby/.venv/lib/tcl8.6 /Users/dstansby/lib/tcl8.6 /Users/dstansby/.venv/library /Users/dstansby/library /Users/dstansby/tcl8.6.12/library /Users/tcl8.6.12/library
This probably means that Tcl wasn't installed properly.
I can successfully run python -c "from tkinter import Tk; window = Tk()" in a fresh conda environment, but when I install python on conda it also installs tk, so maybe that’s why it works? Maybe this isn’t an issue with uv?, but if anyone knows how to get this working I’d be very gateful!
uv 0.4.4 (Homebrew 2024-09-04) macOS
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 43
- Comments: 35 (15 by maintainers)
So… expanding on the workaround so it works on all platforms, doesn’t clobber existing env vars, might work with tk 8.7+, and doesn’t bother doing anything if tk is actually working already (given that just because you used uv to write your library there’s no guarantee it will be run using the standalone Python builds that uv uses)… we have something like this.
You can
uv python install --reinstall <version>Those are in python installation directory not in virtualenv, I’m not next to linux box but under windows:
So, path would be:
C:\Users\mplichta\AppData\Roaming\uv\python\cpython-3.13.0-windows-x86_64-none\tcl\tcl8.6As workaroud you can use tkinker like:
To anyone stumbling on this in the future, the process that worked for my team is:
.venvfolderuv python install --reinstall <version>(e.g.,uv python install --reinstall 3.10).venvTo test:
source .venv/bin/activatepython -m tkinterI just released this in https://github.com/astral-sh/uv/releases/tag/0.5.11
I’m not opposed to doing a similar thing here.
Since most people expect to use Tkinter that comes with the python, I guess including
TCL_LIBRARYandTK_LIBRARYenv path pointing to where uv installed python as @emcek described would be a good default solution? The uv can provide some override mechanism, like allowing use to specify custom path to these two environment variable in the.python_versionfile or maybe some other files? This way TCL should always work by default.I tried @emcek method on Mac and the result is successful
However I have changed the path of
TCL_LIBRARYandTK_LIBRARYa bit as for mac its “lib” instead of “tcl”, as pointed out by @astrojuanluMaybe uv can populate the two environmental variable by default?
This issue is closed. Is there a fix? I just tried this on Ubuntu, and python cannot find tkinter.
I use uv 0.5.30 and updated all the python installs. Still i get the following error: from tkinter import Variable, StringVar, IntVar, DoubleVar, BooleanVar ImportError: cannot import name ‘Variable’ from ‘tkinter’ (unknown location)
Is this related?
It’ll be available following a
python-build-standalonerelease, then a uv release — probably relatively soon.We don’t support using arbitrary archives yet.
This issue is mentioned in #6893
Seems like Tkinter is just unusable for now.
Hmmm… my workaround above fixes tk, but when I try to use the tkagg backend for matplotlib things get broken again. I’ll do some more investigation.
from . import _tkagg ImportError: initialization failedto maybe point others to a workaround, all you’d need to do is to export an env variable called TCL_LIBRARY pointing to the correct tcl directory. to figure the correct one out, I did the following:
ATTENTION: this assumes that your .venv is based on the default python available to uv and also that tcl is at version 8.6, so keep that in mind
for comfort I’ve then just put the var in my .env file which gets automatically loaded by .envrc (direnv) - which btw would totally be an appealing feature for uv to replicate 😛
For the record, rye did this https://github.com/astral-sh/rye/pull/233