0

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

Sblu
  • 43
  • 8
  • Welcome to SO Sblu, could you share the code generating this error please ? – cbo Nov 17 '20 at 17:01
  • 1
    Assuming it's [this comm package](https://core.tcl-lang.org/tcllib/doc/trunk/embedded/md/tcllib/files/modules/comm/comm.md), did you install tcllib? – Shawn Nov 17 '20 at 18:10
  • @shawn, yes it is that comm package. i'm new to tcl in general. how do you check ot see if it is already installed? – Sblu Nov 17 '20 at 19:03
  • 1
    @cbo, the code is posted – Sblu Nov 17 '20 at 19:03
  • my bad @Sblu, will correct my downvoting accordingly when time limit is passed. – cbo Nov 17 '20 at 19:16
  • so I did an offline install of the latest tcllib. same result – Sblu Nov 17 '20 at 21:36
  • when I run this outside of python, in tclsh: package require comm works. the question is how do i either tell python to use the offline package i installed or else update the one that it has to include the comm package – Sblu Nov 17 '20 at 21:46
  • i also tried this with python 3.8.6, same result – Sblu Nov 17 '20 at 21:50

2 Answers2

1

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:

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • Thanks @Donal. this helped to move along, but not to resolution, unfortunately. I can jerry-rig it, by moving directories around, but I'd much rather fix the script. Where is this being searched for exactly? '''>>> interp.eval('package require comm') Traceback (most recent call last): File "", line 1, in _tkinter.TclError: Can't find a usable init.tcl in the following directories: C:/Python36/lib/tcl8.6 C:/lib/tcl8.6 C:/lib/tcl8.6 C:/library C:/library C:/tcl8.6.6/library C:/tcl8.6.6/library''' – Sblu Nov 19 '20 at 17:30
  • i realize this is somewhat of a different topic addressed here, for now I just cheated by moving directories around to make progress in lieu of a more elegant solution: https://stackoverflow.com/questions/29320039/python-tkinter-throwing-tcl-error – Sblu Nov 19 '20 at 21:00
0

Summary of the solution is as @Donal explained https://stackoverflow.com/a/64884895/14656464 and then possibly followed by this next resolution: Python Tkinter throwing Tcl error by pointing the offline version of tcl lib that contains the comms library to where the python environment expects it.

Sblu
  • 43
  • 8