从Groovy脚本调用python类的方法 [英] Invoke method of python class from groovy script

查看:68
本文介绍了从Groovy脚本调用python类的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从groovy脚本执行我的python类的方法.此方法有两个参数.

I want to execute a method of my python class from groovy script. This method have two parameter.

当我从终端执行此命令时:python -c'导入Myclass;Myclass.method("param1","param2")'正常工作.

When i execute this command from terminal: python -c 'import Myclass; Myclass.method("param1","param2")' it is working.

我使用以下常规脚本代码:

I use this groovy script code :

def cmd = "cd /path/to/the/folder && python -c 'import Myclass; Myclass.method(param1,param2)'"
def proc = ["/bin/sh", "-c", cmd].execute()
proc.waitFor()
println "return code: ${proc.exitValue()}"
println "stderr: ${proc.err.text}"
println "stdout: ${proc.in.text}"

当我想在groovy脚本中执行相同操作时,我在参数NameError上出错:NameError:未定义名称'param1'.

When i want to do the same in the groovy script i have error with the parameter : NameError: name 'param1' is not defined.

你知道为什么吗?

最诚挚的问候

推荐答案

在终端中执行脚本时,您使用了字符串文字"param1","param2" ,而不是未定义的变量 param1,param2 .由于您已经使用了单引号和双引号,因此应在反引号旁使用双引号:

When you executed the script in the terminal, you used the string literals "param1", "param2" , not the undefined variables param1, param2. Since you have already used both single and double quotes, you should escape the double quotes with a backslash:

def cmd = "cd /path && python -c 'import Myclass; Myclass.method(\"param1\", \"param2\")'"

或者,只需使用三引号:

or, just use the triple quotes:

def cmd = '''cd /path && python -c 'import Myclass; Myclass.method("param1", "param2")' '''

这篇关于从Groovy脚本调用python类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆