Running into this issue when trying to utilize the comm package:
>>> interp = tkinter.Tcl() >>> interp.eval('package require comm') Traceback (most recent call last): File "<stdin>", line 1, in <module> _tkinter.TclError: can't find package comm
i’m using python 3.6.8 on windows 10
Advertisement
Answer
If you know the directory where the comm
package is installed (either the directory containing the pkgIndex.tcl
or the immediate parent directory), you should make sure that’s on Tcl’s auto_path
before trying to package require
the package. The function you need to do that is this:
def add_library_directory(tcl_context, directory_name): tcl_context.eval("lappend auto_path {}".format( # This does the trick to make the string substitution fully safe tkinter._stringify(directory_name))) interp = tkinter.Tcl() # Up to you to actually find the location... add_library_directory(interp, "/path/to/dir") interp.eval('package require comm')
For details of tkinter._stringify
, which is massively underdocumented despite being very useful, see this other Stack Overflow question: