如何通过传递参数从TCL脚本调用python函数?

如何通过传递参数从TCL脚本调用python函数?,python,python-2.7,python-3.x,tcl,Python,Python 2.7,Python 3.x,Tcl,我有一个python文件sample.py,其中包含两个函数。所以这里我想从tcl脚本调用特定的python函数,还想向该python函数传递一个参数。你能分享一下你的想法吗。我不知道这是否可能。你的回答将对我们更有帮助 sample.py def f1(a,b,c): x = a + b + c retun x def f2(a,b): x = a + b return x 看起来您需要启动python解释器、读取示例脚本、调用函数并打印结果。然后,Tcl可

我有一个python文件
sample.py
,其中包含两个函数。所以这里我想从tcl脚本调用特定的python函数,还想向该python函数传递一个参数。你能分享一下你的想法吗。我不知道这是否可能。你的回答将对我们更有帮助

sample.py

def f1(a,b,c):
    x = a + b + c
    retun x

def f2(a,b):
    x = a + b
    return x

看起来您需要启动python解释器、读取示例脚本、调用函数并打印结果。然后,Tcl可以捕获打印输出:

$ tclsh
% set a 3
3
% set b 5
5
% set result [exec python -c "import sample; print sample.f2($a,$b)"]
8

看起来您需要启动python解释器、读取示例脚本、调用函数并打印结果。然后,Tcl可以捕获打印输出:

$ tclsh
% set a 3
3
% set b 5
5
% set result [exec python -c "import sample; print sample.f2($a,$b)"]
8
使用,您可以进行过程中评估:

package require tclpython

set a 3
set b 5

# Make a Python system within this process
set py [python::interp new]

# Run some code that doesn't return anything
$py exec {import sample}

# Run some code that does return something; note that we substitute a and b
# *before* sending to Python
set result [$py eval "sample.f2($a,$b)"]
puts "result = $result"

# Dispose of the interpreter now that we're done
python::interp delete $py
要注意的主要问题是在使用求值时引用传递到Python代码中的复杂值。这对于数字来说很简单,需要注意字符串的引号。

使用,您可以进行过程中评估:

package require tclpython

set a 3
set b 5

# Make a Python system within this process
set py [python::interp new]

# Run some code that doesn't return anything
$py exec {import sample}

# Run some code that does return something; note that we substitute a and b
# *before* sending to Python
set result [$py eval "sample.f2($a,$b)"]
puts "result = $result"

# Dispose of the interpreter now that we're done
python::interp delete $py

要注意的主要问题是在使用求值时引用传递到Python代码中的复杂值。这对于数字来说很简单,需要注意字符串的引号。

您想要一个包含多个进程的解决方案还是一个进程的解决方案?您想要一个包含多个进程还是一个进程的解决方案?