Sonic
Sonic

Reputation: 85

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

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,

Upvotes: 1

Views: 2021

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137567

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…)

Upvotes: 1

Related Questions