Skip to content

fix: propagate Tcl library path across Tk-created interpreters#958

Merged
jjhelmus merged 1 commit intoastral-sh:mainfrom
shaanmajid:fix/tkinter-tcl-library-env
Feb 17, 2026
Merged

fix: propagate Tcl library path across Tk-created interpreters#958
jjhelmus merged 1 commit intoastral-sh:mainfrom
shaanmajid:fix/tkinter-tcl-library-env

Conversation

@shaanmajid
Copy link
Contributor

@shaanmajid shaanmajid commented Jan 22, 2026

Fixes #939

Problem

tkinter fails with "Can't find a usable init.tcl" on macOS when both:

  1. sys.prefix != sys.base_prefix (e.g., when using venv, uvx, uv run --isolated)
  2. stdin is a non-TTY device (pytest capture, CI, piped input)

Root cause

On macOS Aqua, when stdin is non-TTY, Tk's init path (Tk_CreateConsoleWindow -> TkpInit) creates a secondary Tcl interpreter. The existing patch sets tcl_library on the main interpreter, but this secondary interpreter doesn't inherit it and falls back to compiled-in search paths that don't exist in a relocated/venv context.

Fix

Temporarily set the TCL_LIBRARY environment variable around the Tcl_AppInit call in Tkapp_New. The env var is visible to all interpreters Tk creates internally during init, then unset immediately after to avoid environment pollution. Also
extends _get_tcl_lib_path() to compute the Tcl library path on Unix (previously Windows-only).

Patches updated for Python 3.10–3.14.

@shaanmajid shaanmajid changed the title fix: propagate Tcl library path across Tk-created interps fix: propagate Tcl library path across Tk-created interpreters Jan 22, 2026
@jjhelmus
Copy link
Contributor

After looking into _tkinter and Tk further this approach seems like the best option.
@shaanmajid, is there anything additional you wanted to add to this PR or is it ready for review.

@shaanmajid shaanmajid marked this pull request as ready for review February 13, 2026 03:51
@shaanmajid
Copy link
Contributor Author

To be honest, I didn't realize this was still in draft haha. Yeah, it's ready :) Thanks for taking a look!

Set `TCL_LIBRARY` around `Tcl_AppInit` so all Tk-created interps can find the packaged Tcl/Tk.
@shaanmajid shaanmajid force-pushed the fix/tkinter-tcl-library-env branch from ba1a4df to 42a50d8 Compare February 13, 2026 19:56
@jjhelmus
Copy link
Contributor

LGTM. Thanks for the fix @shaanmajid

@jjhelmus jjhelmus merged commit 0ebb3ee into astral-sh:main Feb 17, 2026
951 of 952 checks passed
@shaanmajid shaanmajid deleted the fix/tkinter-tcl-library-env branch February 18, 2026 00:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using tkinter with pytest, "Can't find a usable init.tcl" ... "This probably means that Tcl wasn't installed properly."

2 participants