This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ned.deily
Recipients ajstewart, ned.deily
Date 2021-05-28.21:56:15
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1622238975.9.0.29635564121.issue44253@roundup.psfhosted.org>
In-reply-to
Content
tkinter does not find Tk; the linker at build time (ld) and usually the dynamic linker (dyld) at run time do all the work. Typically on macOS, the linkers prefer to dynamically link to libraries, deferring the decision to dyld at run time exactly which dynamic library will be linked to rather than statically linking in a library at build time. The expected shared library name (or path) is recorded at build time into the executables generated. The --with-tcltk-includes Python ./configure option is passed at build time to the C compiler chain to add the directories(s) for Tcl and Tk header files, if not otherwise found, and the --with-tcltk-libs option gives the locations of the libraries to add directories with Tcl and Tk libraries for ld to link against including generating the shared library file paths in the binaries produced.

In a standard Python build, tkinter has a Python component that calls a private C-language extension _module helper named _tkinter that does all the C-level calls to Tk and Tkinter. You can find the file name of the _tkinter extension module by importing it in the interpreter and looking at its __file__ attribute. With that you can use the Xcode / Command Line Tools "otool" utility to examine the binary to find the shared library names it is linked with and expecting to find at run time.

An example with a current python.org macOS Python:

$ /usr/local/bin/python3.9
Python 3.9.5 (v3.9.5:0a7dcbdb13, May  3 2021, 13:05:53)
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import _tkinter
>>> _tkinter.__file__
'/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload/_tkinter.cpython-39-darwin.so'
>>> ^D
$ otool -L '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload/_tkinter.cpython-39-darwin.so'
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload/_tkinter.cpython-39-darwin.so:
	/Library/Frameworks/Python.framework/Versions/3.9/lib/libtcl8.6.dylib (compatibility version 8.6.0, current version 8.6.11)
	/Library/Frameworks/Python.framework/Versions/3.9/lib/libtk8.6.dylib (compatibility version 8.6.0, current version 8.6.11)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.100.5)

In this case, the libtcl and libtk shared libraries are included within the python.org distribution installed in /Library/Frameworks.

Another example with a MacPorts python built from source:

$ otool -L $(/opt/macports/bin/python3.9 -c "import _tkinter;print(_tkinter.__file__)")
/opt/macports/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/_tkinter.cpython-39-darwin.so:
	/opt/macports/lib/libtcl8.6.dylib (compatibility version 8.6.0, current version 8.6.11)
	/opt/macports/lib/libtk8.6.dylib (compatibility version 8.6.0, current version 8.6.11)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.100.5)

In this case, the _tkinter module was linked with libtcl and libtk included in the MacPorts distribution.

Unlike many other platforms, the shared library names seen in macOS binaries are usually absolute path names although they can be relative paths.  It is possible to use environment variables to override the default search paths used by dyld at run time and it is also possible to ask dyld to display debug info about exactly what files it ends up using, among other things.  See the dyld man page for lots of useful info.  An example:

$ DYLD_PRINT_LIBRARIES=1 /opt/macports/bin/python3.9 -c "import _tkinter;print(_tkinter.__file__)" 2>&1 | grep 'libt'
dyld: loaded: <FCA34869-A151-383A-AF54-F988070D5977> /opt/macports/lib/libtcl8.6.dylib
dyld: loaded: <403FAC32-CB24-3ED3-B680-02AE4FBB11A7> /opt/macports/lib/libtk8.6.dylib

Hope this helps!
History
Date User Action Args
2021-05-28 21:56:15ned.deilysetrecipients: + ned.deily, ajstewart
2021-05-28 21:56:15ned.deilysetmessageid: <1622238975.9.0.29635564121.issue44253@roundup.psfhosted.org>
2021-05-28 21:56:15ned.deilylinkissue44253 messages
2021-05-28 21:56:15ned.deilycreate