This package allows the execution of Python code from a Tcl interpreter, as in:
package require tclpython 3 set interpreter [python::interp new] $interpreter exec {print("Hello World")} puts [$interpreter eval 3/2.0] python::interp delete $interpreter |
Hello World 1.5 |
It works by creating one or more embedded Python interpreters and sending them strings to evaluate or execute. Modules can be loaded in the Python interpreter as if it were the real Python program.
The commands created by the package are very simple:
As expected, eval returns the Python interpreter result or reports an error at the Tcl level when the Python interpreter itself fails in executing the code string, while exec simply lets the Python interpreter execute the code and returns nothing or eventually report an error.
You can create several Python interpreters, if the tclpython package was linked against a Python library compiled with threads support, otherwise only 1 Python interpreter can exist at a time.
Notes:
In tclpython version 2, only the eval command was available to an interpreter. Some such invocations, generally that return nothing, need to be converted to the exec command, as the following examples show:
$interpreter eval {print("Hello World")} ;# version 2 $interpreter exec {print("Hello World")} ;# version 3 |
$interpreter eval {def initialize(): pass} ;# version 2 $interpreter exec {def initialize(): pass} ;# version 3 |
# version 2: set exists [$interpreter eval "try: type(initialize) == FunctionType\nexcept: 0"] # version 3: $interpreter exec "try: result = (type(initialize) == FunctionType)\nexcept: result = 0" set exists [$interpreter eval result] |
Send your comments, complaints, ... to jfontain@free.fr.
My homepage is at http://jfontain.free.fr/.