| Ticket UUID: | 6fbabfe1661c22e7990bb917e9fa49cc16e016ea | ||
| Title: | Tkinter: Cannot find a usable init.tcl with recent core-9-0-branch | ||
| Type: | Bug | Created on: | 2025-09-30 23:21:35 |
| Submitter: | chrstphrchvz | Assigned to: | nobody |
| Subsystem: | 38. Init - Library - Autoload | Severity: | Important |
| Priority: | 5 Medium | Last modified: | 2026-05-23 11:18:34 |
| Status: | Closed | Closed by: | apnadkarni |
| Resolution: | Fixed | Closed on: | 2026-05-23 11:18:34 |
| Version: | core-9-0-branch | ||
| Description: | ||||
|
Using macOS 15 Intel and Python 3.15.0a0 (recent CPython main branch). I am trying to create a Tcl interpreter from Tkinter (without loading Tk): import tkinter i = tkinter.Tcl() The most recent check-in on core-9-0-branch I am aware which works is [201dd6fe]. Between [0433b67a] and [c658915b], an assertion fails (the same one described in bug [f0b80a3d]). Between [c6b85e5e] and present ([d23568f2]), Tcl_Init() emits an error about not finding init.tcl:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
i = tkinter.Tcl()
File "/Volumes/Ventura/Users/user/git/cpython/Lib/tkinter/__init__.py", line 2592, in Tcl
return Tk(screenName, baseName, className, useTk)
File "/Volumes/Ventura/Users/user/git/cpython/Lib/tkinter/__init__.py", line 2479, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_tkinter.TclError: Cannot find a usable init.tcl in the following directories:
/Users/user/tcl90sdk26/lib/tcl9.0 /Volumes/Ventura/Users/user/git/lib/tcl9.0 /Volumes/Ventura/Users/user/lib/tcl9.0 /Volumes/Ventura/Users/user/git/library /Volumes/Ventura/Users/user/library /Volumes/Ventura/Users/user/tcl9.0/library /Volumes/Ventura/Users/user/tcl9.0.3/library /Volumes/Ventura/Users/tcl9.0.3/library
| ||||
| User Comments: | ||||
apnadkarni added on 2025-10-01 02:34:50:
> Between [0433b67a] and [c658915b], an assertion fails (the same one described in bug [f0b80a3d]). To be ignored as that assertion assumed zipfs worked on macos. > Between [c6b85e5e] and present ([d23568f2]), <tt>Tcl_Init()</tt> emits an error Can you post (a) How Tcl is being built (i.e. configure flags) (b) A link to the Python C code that initializes the Tcl interpreter (c) Confirm that init.tcl indeed does not exist on any of those paths (this is to distinguish non-existence from init.tcl raising an error. Both will show the same error message. (d) If you run the tclsh from the installation Python is using, what does "info library" or "set tcl_library" print? I don't have a mac and Github MacOS CI is not showing any issues so cannot try this for myself. /Ashok apnadkarni added on 2025-10-01 03:00:31:
Never mind about tkinter source, found it. I will need to look at how its initialization of Tcl differs from tclsh/wish etc. Answers to (a), (c) and (d) would still be helpful. chrstphrchvz added on 2025-10-01 03:20:52:
Ashok, thanks for taking a look at this. (a) How Tcl is being built (i.e. configure flags)
CFLAGS='-fsanitize=undefined -fsanitize=address' \
LDFLAGS='-fsanitize=undefined -fsanitize=address' \
CC='ccache xcrun --sdk /Library/Developer/CommandLineTools/SDKs/MacOSX26.sdk --run clang' \
CPPFLAGS='-DPURIFY' \
./configure \
--without-system-libtommath \
--enable-64bit \
--enable-symbols \
--prefix=${HOME}/tcl90sdk26 \
--exec-prefix=${HOME}/tcl90sdk26
(b) A link to the Python C code that initializes the Tcl interpreter The Tcl_AppInit() being used is the one https://github.com/python/cpython/blob/main/Modules/tkappinit.c rather than the one in https://github.com/python/cpython/blob/main/Modules/_tkinter.c (c) Confirm that init.tcl indeed does not exist on any of those paths (this is to distinguish non-existence from init.tcl raising an error. Both will show the same error message. /Volumes/Ventura/Users/user/library does not contain init.tcl. The rest of the directories do not exist. (d) If you run the tclsh from the installation Python is using, what does "info library" or "set tcl_library" print? $ ~/tcl90sdk26/bin/tclsh9.0 % info library //zipfs:/lib/tcl/tcl_library % set tcl_library //zipfs:/lib/tcl/tcl_library The tclsh/wish in this Tcl/Tk installation seem to work as expected. jan.nijtmans added on 2025-10-01 08:22:49:
I'm not 100% sure it's related, but .... On macOS, you should add --disable-zipfs to the build flags, since macOS doesn't allow to attach a zip-file to a shared library. (I'm surprised that Tcl/Tk works for you without this option). On static builds (--disable-shared) you don't need --disable-zipfs, then it works, but that appears not to be the situation here. I don't think that --enable-64bit is doing anything on macOS any more: All supported macOS versions are 64-bit. You can safely remove this option (although it doesn't hurt either) apnadkarni added on 2025-10-01 11:18:46:
Thanks for the link to tkinter, I was looking at exactly the wrong one :-) It appears your tcl is built with zipfs enabled (so init.tcl and friends are placed within the zip archive and not in a native disk directory). But because tkinter never calls TclZipfs_AppInit as part of its initialization, the init.tcl within the zipfs archive is never looked at. Possible solutions are:
A third possibility is below but first I need to clear up a point of confusion I have about MacOS. I only recently learnt that MacOS builds do not support zipfs archives and yet I see that's how your Tcl is built. So do MacOS binaries support zip archives attached to the executable or shared library or not? Does it depend on the OS version, architecture, compiler toolchain or something else? Now, as to why it was working before (i.e. as of commit [201dd6fe]), it's because zipfs was searched on every interpreter creation. I changed that in response to multiple tickets, related to both performance and defects, to only initialize at program start with the premise that if zipfs archives are to be used TclZipfs_AppInit must be called. So the third possibility is to partially revert that requirement. I am loathe to do that because it leads to unpredictable and ad-hoc sequences of initialization steps (locating libraries, encoding etc.) depending on the order in which Tcl C API is called. Just as Tcl_Init must be called (not just Tcl_CreateInterp) to use encodings, auto-loading etc., it seems reasonable that TclZipfs_AppHook must be called to use bound zipfs archives. Something to discuss at the next online meet perhaps. jan.nijtmans added on 2025-10-01 11:51:49:
> So do MacOS binaries support zip archives attached to the executable or shared library or not? Does it depend on the OS version, architecture, compiler toolchain or something else? It's related to security. If I remember correctly, on MacOS 10.x it worked fine. But when I switched to MacOS-11 (that's what I'm still running now), it broke, because adding a zip-file to a shared library or executable violates the file structure. The Mach-O validity checks are failing, so the OS refuses to run the binary. Marc Culler wrote the "macher" (https://github.com/culler/macher) utility to fix that such that the executable with a zip-file attached works again. But this utility only works with executables, not with shared libraries (I understood that it's quite complicated to make it work with shared libraries). So, I'm quite sure that attaching a zip-file to a shared library doesn't work since at least MacOS-11: I experienced this myself. With static executables it works, if you install Culler's "macher" utility. apnadkarni added on 2025-10-01 12:48:53:
Tx for the explanation, Jan. Christopher, as another point of clarification, calling TclZipfs_AppHook will work correctly even if no zipfs archives are attached so it is the more general solution. marc_culler (claiming to be Marc Culler) added on 2025-10-01 13:25:23:
I believe that the issue with the zipfs build on macOS is related to two things: code signatures and the structure of a mach-O binary. The code signature is required to be at the end of a thin mach-O binary and at the end of each arch segment in a fat binary. Code signatures are required for arm64 CPUs and an ad-hoc signature is added automatically by the Apple linker on ARM systems. The gatekeeper checks whether some or all mach-O load commands have the size which is reported in the mach-O header, meaning in particular that the last load command must extend to the end of the file, but also that the zipfile cannot be attached to the end of another load command unless the header is adjusted accordingly. The last load command is the code signature, so a zipfile cannot be added after the signature. Also, adding a zipfile destroys the signature, so the file must be re-signed after adding the zipfile. This requires that the codesign program be able to find the correct place to add the signature, for which it can only use the mach-O header. We had a workaround which worked for a while on Intel only. That involved adding a new fake arch segment as the last arch in the dylib. I don't think that ever worked on ARM systems, and I think it stopped working on Intel, possibly due to changes to the gatekeeper. Adding a new arch segment to a thin mach-O binary requires adding a a fat header while adding a new arch to a fat binary requires rewriting and expanding the fat header. I suspect that the workaround may not ever have worked correctly in all of those cases. I would have to check the macher code to refresh my memory. apnadkarni added on 2025-10-01 15:04:21:
Christopher, if you are up for it, please try branch bug-6fbabfe166 if it works for you without any changes to tkinter code or your build steps. I'm still not in favor of the changes in this branch but it would help in making a decision if it fixes your issue. jan.nijtmans added on 2025-10-01 17:05:36:
Review: the new TclListObjAppendIfAbsent() function isn't used anywhere ;-) chrstphrchvz added on 2025-10-01 19:57:50:
I have tried the changes from bug-6fbabfe166 and unfortunately the same error persists. Tkinter does work if I configure Tcl and Tk with apnadkarni added on 2025-10-02 11:03:43:
Sorry, bit of a brain fade on my part. When you reported the value of
With the core-9-0-branch, the above gives the same error as you were seeing. With the bug-6fbabfe166 branch, Tcl seems to initialize successfully. Having said that, my preference would still be to replace the Tcl_FindExecutable call in tkinter with TclZipfs_AppHook if at all possible (would need more testing of course). It avoids a number of problems with initialization of encodings when the system encoding is not utf-8 and has to be loaded from disk. If that is not possible and mandating On a related note, I am also wondering about the jan.nijtmans added on 2025-10-02 12:56:20:
> We had a workaround which worked for a while on Intel only. That involved adding a new fake arch segment as the last arch in the dylib. I don't think that ever worked on ARM systems, and I think it stopped working on Intel, possibly due to changes to the gatekeeper. I downloaded last-night's developer prebuild from here, unpacked it, and followed the instructions: $ xattr -d com.apple.quarantine wish9.0.3_snapshot $ ./wish9.0.3_snapshot % set tcl_library //zipfs:/app/tcl_library % set tk_library //zipfs:/app/tk_library % tcl::build-info 9.0.3+git-f69f108ed43e7fe9e71a4e4ca03c82866091828d.clang-1700.static.tommath-0103 % tk::build-info 9.0.3+git-e729b7c73417fd7311342bc195e9c444bd00ce16.aqua.clang-1700.objective-c.static It worked fine on my MacOS-11 machine. It is created with the latest "macher", version 1.8. It's a big binary, which contains sections for both intel and arm64. So, could someone check it on a more recent Mac? sl1200mk2 added on 2025-10-02 13:14:13:
Hi Jan, on macOS 26 (arm machine): nico@brouhaha ~ % cd Downloads nico@brouhaha Downloads % xattr -d com.apple.quarantine wish9.0.3_snapshot nico@brouhaha Downloads % ./wish9.0.3_snapshot % set tcl_library //zipfs:/app/tcl_library % set tk_library //zipfs:/app/tk_library % tcl::build-info 9.0.3+git-f69f108ed43e7fe9e71a4e4ca03c82866091828d.clang-1700.static.tommath-0103 % tk::build-info 9.0.3+git-e729b7c73417fd7311342bc195e9c444bd00ce16.aqua.clang-1700.objective-c.static ++ jan.nijtmans added on 2025-10-02 13:21:58:
Thanks! That means it still works for executables, on any Mac. For dylib's, it doesn't work on MacOS-11. Therefore the --enable-framework option impliates --disable-zipfs, and that's what needs to be used normally. chrstphrchvz added on 2025-10-03 00:58:32:
Tcl built from latest bug-6fbabfe166 with I have not looked into using apnadkarni added on 2025-10-04 10:56:44:
Thanks for trying it out, Christopher. Having thought about it further, even if py tkinter were to be modified to use Accordingly, merged the branch and closing. dgp added on 2025-11-14 16:38:30:
The commit [6e612ef335] to the core-9-0-branch that was submitted to fix this ticket also had the effect of bringing back the failing tests issue we hashed out in ticket [87b69745be]. Many tests fail on my system because my system has broken encoding files stored where some configurations of Tcl can find and use them. I accept that an effective fix is for me to clean up my system and discard these broken files. I keep them around because they signal to me changes in what Tcl initialization is doing. Is there a simple explanation why we have once again set up a --disable-shared --enable-zipfs build of Tcl to look for encoding files in a place where the build/install did not put any encoding files to be found? Why should a --enable-zipfs build of Tcl be allowed to look anywhere else for its encoding files than in the zip archive attached to the build? dgp added on 2025-11-14 16:55:19:
I don't see the test failures on the Tcl trunk (9.1), so maybe there's a solution that can resolve this with the right backport. apnadkarni added on 2025-11-17 06:13:00:
Don, I feel your pain. Maybe more because I've been trying to fix this ping-pong of broken behavior for a few months now. The fundamental problem is that the search for Tcl library (and encodings) is suffering from the "noble" aim of attempting to fix up errors in configuration or installs by searching up and down the directory hierarchy. As a consequence, when Tcl is not not run in a location corresponding to where it is configured and does not define TCL_LIBRARY either (as is the case for the unixInit-3.2 test), there is a chance it will pick up the wrong directory corresponding to some other version of Tcl. This is what you are seeing. Regarding your question about why an --enable-zipfs build should be looking anywhere but zipfs, I would agree with you. In fact, I would go further and ask why, even on --disable-zipfs builds, should it look anywhere than the directory set by the configure script or explicitly set by user via TCL_LIBRARY. The problem in enforcing this is that
TIP 732 is my attempt to fix this but is going to need testing effort from developers on the affected platforms (I do not have access to macos and have only a rudimentary knowledge of Linux/Unix installation conventions). apnadkarni added on 2025-11-17 06:16:28:
Don wrote:
I am not aware what anything that might have fixed this in the trunk. If you are up for it, bisecting would be helpful. Since I cannot reproduce your failure, not tkinter (which requires macos), it's difficult for me to pinpoint the commit. I also wonder if whatever fixes it for you on trunk also breaks tkinter on macos again! jan.nijtmans added on 2025-11-17 10:28:33:
> Fixing this needs somone familiar with all the macOS variants to error out on appropriate versions of macOS to not define ZIPFS_BUILD I don't think that's too difficult. On MacOS, ZIPFS_BUILD works under two conditions: You _must_ have a --disable-shared build, and you must have Marc Cullers' "macher" utility installed (while building). Some older MacOS versions even worked without "macher", but I wouldn't thrust that. Hope this helps apnadkarni added on 2025-11-25 10:19:52:
Perhaps, but it needs someone familiar and with access to macOS to do this. dgp added on 2026-05-22 18:49:58:
Returning to this ticket as one of the many twists and turns related to setting up Tcl's encoding search path. The patch that arose from this ticket changed the functioning of an internal routine TclZipfsInitEncodingDirs() from setting the [encoding dirs] to be the single known encoding directory inside the zipfs archive to appending that directory onto whatever pre-existing search patch Tcl claims to be established. Is there any lingering knowledge about how necessary that detail was to the purpose of addressing this ticket? I would be happier with continuing to set the search path to the single zip archive location. Short of that, it would be a better functioning answer to pre-pend rather than append the zip archive location. Is there some known environment or use case where we expect the pre-set path (if any) to allow us to find working *.enc files in a way superior to looking first at the set of zip archive encodings built into Tcl? dgp added on 2026-05-22 19:09:53:
In my earlier comments griping about this change last November,
I suggested there might be backportable fixes in the 9.1 branch,
but that is not the case.
I see the problems manifest in 9.0 and not 9.1 solely because
I have a directory
$INSTALLDIR/lib/tcl9.0/encoding
and I don't (yet) have a directory
$INSTALLDIR/lib/tcl9.1/encoding
Any tinkering or testing with --disable-zipfs builds on the trunk
is going to raise all the same issues there too.
dgp added on 2026-05-22 19:19:12:
Just to spell out the issue more explicitly, if a developer ever experiments and installs a --disable-zipfs build of Tcl 9.m.p , then all later builds of Tcl 9.m.q in any configuration are going to have the encoding files of that installed 9.m.p experiment included and favored on the encoding search path, even favored over the set of files stored in the zip archive built in to the Tcl 9.m.q library itself. Encoding files are revised very infrequently, so problems like this are hard to notice. When we do notice, I'd sure like to settle on a more sensible set of behaviors. If we need encoding searches to operate in this way, and we didn't just land on this functionality by accident, I'd sure like to have that explanation clearly understood and recorded. apnadkarni added on 2026-05-23 11:18:34:
I responded in [2a0966cd]. Let us continue discussion there.
| ||||
