1

I need to run a TCL script in python, my TCL script also has a user-defined(internal) package I tried these scripts:

1.

import Tkinter
import socket

def TCLRun():
 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 s.connect(('127.0.0.1', 5006))
 root = Tkinter.Tk()
## root.tk.eval('package require XXX')
 tcl_script ="""
package require XXX
set YYY [COMMAND FROM PACKAGE]
puts $YYY 
} """
 # call the Tkinter tcl interpreter
 root.tk.call('eval', tcl_script)
 root.mainloop()

with this error:

import TCLCall
>>> TCLCall.TCLRun()

    Traceback (most recent call last):
      File "<pyshell#2>", line 1, in <module>
        TCLCall.TCLRun()
      File "C:\Users\XXX\Desktop\PKT\TCLCall.py", line 24, in TCLRun
        root.tk.call('eval', tcl_script)
    TclError: can not find channel named "stdout"

and,

2.

import Tkinter
root=Tkinter.Tk()
root.tk.eval('package require XXX')
root.tk.eval('set YYY COMMAND')

returns error about sdtout!

other one:

3.

 import subprocess
    p = subprocess.Popen(
        "tclsh tcltest.tcl",
        shell=True,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)
    stdout, stderr = p.communicate()
    print stdout
    print stderr

returns the following error:

can't find package __teapot__

    while executing

"package require __teapot__"

none of them working, please help me with this issue!

I can use some commands that operating something on our product with following code:

import socket
import time

def Sckt():
 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 s.connect(('127.0.0.1', 5006))

 s.send('0 COMMAND \r\n')

that would give you some ideas!

thank,

3
  • are you sure the tcl-code is running as standalone? try tclsh tcltest.tcl the errors seem to come from tcl, so the python around your tcl might only make it harder to understand the underlying problem. Commented Jan 16, 2013 at 7:28
  • Yes, I ran the script several times and it works. the problem is from python that doesn't recognize it! Commented Jan 16, 2013 at 18:17
  • I added an answer in another question here: stackoverflow.com/a/61623719/5626983 Commented May 5, 2020 at 22:00

1 Answer 1

1

With a custom package that is not part of your vendor's installation, you need to instruct Tcl where to search for it from. To do that, append the parent directory of the directory containing the package to Tcl's global auto_path variable. For example, if the XXX package is in the directory /home/amir/.tclpackages/XXX1.0 then you would have your script do this:

lappend auto_path /home/amir/.tclpackages

This can go anywhere in the Tcl script before the package require XXX. (Well, you could put it afterwards but then the package require would fail…)

Sign up to request clarification or add additional context in comments.

1 Comment

actually I added that command to my tcl script. but my problem is if I should put that package in my python27 folder?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.