How to use Xcode's provided tkinter module ?

Hi there, I’m having issue with the python3 installation provided by Xcode’s toolchain. I’m currently writing a LLDB plugin, using the LLDB python API, to allow the user to visualize audio data from the current debugged program in a GUI, using tkinter and matplotlib. I'm using those because I'm developing a cross-platform plugin, as I'm initially a Linux developer who wants to make this available to my fellow Apple audio devs.

My issue arise at least on two setups

  • MacOS 12.7.6 Monterey with Xcode 14.2
  • MacOS 14 with Xcode 15.4 (not my machine)

Because I wanna support Xcode’s toolchain, I want to use Xcode’s lldb. Xcode’s lldb uses Xcode’s provided python, which I’m having issues with when loading tkinter.The issue can be reproduced like this :

> xcrun python3 -c "import tkinter;tkinter._test()"
macOS 12 (1207) or later required, have instead 12 (1206) !
zsh: abort      xcrun python3 -c "import tkinter;tkinter._test()"

On MacOS 14 the version numbers are :

macOS 14 (1407) or later required, have instead 14 (1406) !

You can see it fails to load tkinter. From what I understood so far, it looks like the tkinter/tcl/tk version distributed with Xcode is not supported by MacOS ?I checked and the imported tkinter module is definitely the one provided by Xcode’s toolchain :

# Checking where tkinter is installed
> fd "^tkinter$" /Applications/Xcode.app
/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/tkinter/
/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/tkinter/
/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/site-packages/future/moves/tkinter/

# Checking that Xcode python uses the right module - it matches
> xcrun python3 -c "import tkinter;print(tkinter.sys.modules['tkinter'])"
<module 'tkinter' from '/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py'>

I can get a working tkinter working by installing it using homebrew or macports, but I’m not able to use it with Xcode’s python installation. I tried overwriting sys.path to force Xcode’s python to import homebrew’s tkinter module, but it still loads Xcode’s tkinter .so.

In the crash report I can see it indeed loads tcl/tk 8.5 and loads _tkinter.cpython-39-darwin.so from Xcode. I could disable the SIP (System Integrity Protection) to force to load another version of the library, but that wouldn’t be something I can ask the users.

On the LLDB forum, they advise against using another python interpreter that the one provided by the toolchain. So is there a way to get the provided tkinter/tcl/tk installation to work ? If not I’m confused about why it’s provided in the first place.

Thanks a lot for your time and please tell me if you have any questions.

PS: if possible i'll post the head of the crash report in the comment of this post

Here is the head of the crash report :

-------------------------------------
Translated Report (Full Report Below)
-------------------------------------

Process:               Python [24954]
Path:                  /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/Resources/Python.app/Contents/MacOS/Python
Identifier:            com.apple.python3
Version:               3.9.6 (3.9.6)
Build Info:            python3-124000000000000~2119
Code Type:             X86-64 (Native)
Parent Process:        zsh [573]
Responsible:           iTerm2 [403]
User ID:               503

Date/Time:             2024-09-30 15:34:20.0934 +0200
OS Version:            macOS 12.7.6 (21H1320)
Report Version:        12
Anonymous UUID:        E3E99228-438A-764A-B0C4-B5AD5C4657DE

Sleep/Wake UUID:       DD771265-A0E6-4E92-B561-1FCCDB9BE381

Time Awake Since Boot: 3300 seconds
Time Since Wake:       893 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_CRASH (SIGABRT)
Exception Codes:       0x0000000000000000, 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Application Specific Information:
abort() called


Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   libsystem_kernel.dylib        	    0x7ff8155e7fce __pthread_kill + 10
1   libsystem_pthread.dylib       	    0x7ff81561e1ff pthread_kill + 263
2   libsystem_c.dylib             	    0x7ff815569d14 abort + 123
3   Tcl                           	    0x7ffb15706caf Tcl_PanicVA + 364
4   Tcl                           	    0x7ffb15706d2f Tcl_Panic + 128
5   Tk                            	    0x7ffb15803da1 TkpInit + 548
6   Tk                            	    0x7ffb1578482e 0x7ffb15755000 + 194606
7   _tkinter.cpython-39-darwin.so 	       0x10b79d684 0x10b793000 + 42628
8   _tkinter.cpython-39-darwin.so 	       0x10b797f71 0x10b793000 + 20337
9   _tkinter.cpython-39-darwin.so 	       0x10b7978a1 0x10b793000 + 18593
10  Python3                       	       0x10bb8f4a9 0x10bb03000 + 574633
11  Python3                       	       0x10bc3e20d 0x10bb03000 + 1290765
12  Python3                       	       0x10bc3aea5 _PyEval_EvalFrameDefault + 27557
13  Python3                       	       0x10bc3f16e 0x10bb03000 + 1294702
14  Python3                       	       0x10bb4cce9 _PyFunction_Vectorcall + 265
15  Python3                       	       0x10bb4c3d4 _PyObject_FastCallDictTstate + 260
16  Python3                       	       0x10bb4d0cb _PyObject_Call_Prepend + 139
17  Python3                       	       0x10bbafb06 0x10bb03000 + 707334
18  Python3                       	       0x10bba6df4 0x10bb03000 + 671220
19  Python3                       	       0x10bb4c5aa _PyObject_MakeTpCall + 378
20  Python3                       	       0x10bc3e2e2 0x10bb03000 + 1290978
21  Python3                       	       0x10bc3af44 _PyEval_EvalFrameDefault + 27716
22  Python3                       	       0x10bb4cdf3 0x10bb03000 + 302579
23  Python3                       	       0x10bc3e20d 0x10bb03000 + 1290765
24  Python3                       	       0x10bc3aea5 _PyEval_EvalFrameDefault + 27557
25  Python3                       	       0x10bc3f16e 0x10bb03000 + 1294702
26  Python3                       	       0x10bc34201 PyEval_EvalCode + 81
27  Python3                       	       0x10bc83f9b PyRun_StringFlags + 315
28  Python3                       	       0x10bc83e15 PyRun_SimpleStringFlags + 69
29  Python3                       	       0x10bca2891 Py_RunMain + 449
30  Python3                       	       0x10bca3313 0x10bb03000 + 1704723
31  Python3                       	       0x10bca336b Py_BytesMain + 43
32  dyld                          	       0x10c81c52e start + 462

Thread 1:
0   libsystem_pthread.dylib       	    0x7ff815619f48 start_wqthread + 0

Thread 2:
0   libsystem_pthread.dylib       	    0x7ff815619f48 start_wqthread + 0

Thread 3:
0   libsystem_pthread.dylib       	    0x7ff815619f48 start_wqthread + 0


Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x0000000000000000  rbx: 0x000000010c897600  rcx: 0x00007ff7b4addc48  rdx: 0x0000000000000000
  rdi: 0x0000000000000103  rsi: 0x0000000000000006  rbp: 0x00007ff7b4addc70  rsp: 0x00007ff7b4addc48
   r8: 0x00007ff856d70c98   r9: 0x0000000000000000  r10: 0x0000000000000000  r11: 0x0000000000000246
  r12: 0x0000000000000103  r13: 0x00006000009d01d0  r14: 0x0000000000000006  r15: 0x0000000000000016
  rip: 0x00007ff8155e7fce  rfl: 0x0000000000000246  cr2: 0x0000000000000000
  
Logical CPU:     0
Error Code:      0x02000148 
Trap Number:     133


Binary Images:
    0x7ff8155e0000 -     0x7ff815617fff libsystem_kernel.dylib (*) <2fe67e94-4a5e-3506-9e02-502f7270f7ef> /usr/lib/system/libsystem_kernel.dylib
    0x7ff815618000 -     0x7ff815623fff libsystem_pthread.dylib (*) <5a5f7316-85b7-315e-baf3-76211ee65604> /usr/lib/system/libsystem_pthread.dylib
    0x7ff8154e8000 -     0x7ff815570fff libsystem_c.dylib (*) <202d7260-ea46-3956-a471-19c9bcf45274> /usr/lib/system/libsystem_c.dylib
    0x7ffb15689000 -     0x7ffb15754fff com.tcltk.tcllibrary (8.5.9) <cd258efe-b8d1-3c70-a187-94565e8a255c> /System/Library/Frameworks/Tcl.framework/Versions/8.5/Tcl
    0x7ffb15755000 -     0x7ffb15838fff com.tcltk.tklibrary (8.5.9) <0417ca4c-06d8-316e-99c7-c812fc80159c> /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk
       0x10b793000 -        0x10b79efff _tkinter.cpython-39-darwin.so (*) <eeb10ea0-365b-32d5-90e8-721d0a80dfa5> /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/lib-dynload/_tkinter.cpython-39-darwin.so
       0x10bb03000 -        0x10bd8efff com.apple.python3 (3.9.6) <8c0dd12f-aa40-38c6-9ad8-c3276ba59c20> /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/Python3
       0x10c817000 -        0x10c882fff dyld (*) <eea022bb-a6ab-3cd1-8ac1-54ce8cfd3333> /usr/lib/dyld

You can see the source of this error in the Darwin open source. My reading of that code is that the deployment target for the library was macOS 14.7 and you’re running it on macOS 14.6. macOS 14.7 is publicly available. Is there a reason you haven’t update? [1]

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] He says, on a machine running macOS 14.6.1 O-: After I’m done with DevForums I’m gonna update my Mac!

My machine is on MacOS 12.7.6 Monterey (not 14) and I have the following error :

macOS 12 (1207) or later required, have instead 12 (1206) !

The other error (for MacOS 14) is from my friend who tested it for me on his machine which is on MacOS 14.

I cannot update my MacOS further than MacOS 12 because it's an iMac from 2015 which doesn't have the cryptography chip required for Ventura (from what I understood). When I go to Software Update, no MacOS update is proposed.

According to this page : https://developer.apple.com/support/xcode/ I downloaded the latest version of Xcode for my version of MacOS : Xcode 14.2 macOS Monterey 12.5

Since my MacOS version 12.7.6 > 12.5 I expected the whole Xcode toolchain to be compatible with my system

Quick edit : The exact version of my friend's computer is MacOS 14.7 (23H124)

It gives him this error :

macOS 14 (1407) or later required, have instead 14 (1406) !

Thanks for the help !

He says, on a machine running macOS 14.6.1 O-: After I’m done with DevForums I’m gonna update my Mac!

Did I actually do that yesterday? No )-:

But I had a theory as to what might be going on here, and doing this upgrade was a good way to test that theory. Here’s what I did today:

  1. I ran your test on my Mac, currently running 14.6.1:

    % sw_vers
    ProductName:		macOS
    ProductVersion:		14.6.1
    BuildVersion:		23G93
    % 
    % xcrun python3 -c "import tkinter;tkinter._test()"
    DEPRECATION WARNING: The system version of Tk is deprecated and may be removed in a future release. Please don't rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning.
    

    This worked, presenting the happy little test UI.

  2. I upgraded my Mac to 14.7:

    % sw_vers
    ProductName:		macOS
    ProductVersion:		14.7
    BuildVersion:		23H124
    
  3. And then repeated the test:

    % xcrun python3 -c "import tkinter;tkinter._test()"
    macOS 14 (1407) or later required, have instead 14 (1406) !
    zsh: abort      xcrun python3 -c "import tkinter;tkinter._test()"
    

    And we crashed out with the same symptoms.

I don’t fully understand that code but I think it’s just a case of them getting the ‘branch’ backwards. Instead of failing if the system is older, they fail if the system is newer.

I recommend that you file a bug about this. Hmmm, but where? You could open an issue with the original Tcl project, and perhaps even fix it there. It’s likely that Apple will pick that up at some point. Or you could file a bug with Apple.

Regardless of what you choose, please post a reference to the bug, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

How to use Xcode's provided tkinter module ?
 
 
Q