java可以调用python_从Java调用Python模块

I have a Python interface of a graph library written in C - igraph (the name of library). My need is to invoke the python modules pertaining to this graph library from Java code. It goes like this, the core of library is in c. This core has been imported into Python and interfaces to the functions embedded in core are available in Python. My project's rest of the code is in Java and hence I would like to call the graph functions by Java as well. Jython - which lets you invoke python modules with in Java was an option.I went on trying Jython to discover that it will not work in my case as the core code is in C and Jython wont support anything that is imported as a c dll in python code.I also thought of opting for the approach of calling graph routines directly in c. That is without passing through Python code. I am assuming there must be something which lets you call c code from Java, how ever I am not good in C hence I did not go for it. My last resort seems to execute Python interpreter from command line using Java. But that is a dirty and shameless. Also to deal with the results produced by Python code I will have to write the results in a file and read it back in java. Again dirty way. Is there something that any one can suggest me? Thanks to every one giving time.

感谢Igal回答。我看了一眼。乍一看,它似乎只是简单地调用python脚本。Jep jep = new Jep(false, SCRIPT_PATH, cl);

jep.set("query", query);

jep.runScript(SCRIPT_PATH + file);

jep.close();

它与我们从命令行通过Java代码调用python解释器时的操作非常相似。Runtime runtime = Runtime.getRuntime();

Process proc = runtime.exec("python test.py");

关注的是如何使用Python脚本生成的结果。天真的方法是将它们写入文件并用Java读回。我正在寻找一种更聪明的方法。无论如何,谢谢你的建议。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 MySQL 触发器中调用 Python 脚本,你可以使用以下步骤: 1. 在 MySQL 中创建一个触发器,以便在特定事件发生时触发执行 Python 脚本。 2. 在 Python 中编写脚本,以便执行所需的操作。你可以使用 Python 的 MySQL 连接库(如 PyMySQL)与 MySQL 进行交互。 3. 在触发器中调用 Python 脚本,可以通过 shell 命令调用 Python 解释器来实现。例如,在 Linux 系统上,你可以使用以下命令: ``` `/usr/bin/python /path/to/your/python/script.py` ``` 4. 将该命令作为 MySQL 触发器中的 shell 命令调用即可。 需要注意的是,如果你的 Python 脚本需要接收参数,你需要在触发器中将这些参数传递给 shell 命令。可以使用 MySQL 的 `CONCAT()` 函数将参数拼接成字符串,然后在 shell 命令中解析这个字符串。 示例代码如下: ```mysql CREATE TRIGGER `my_trigger` AFTER INSERT ON `my_table` FOR EACH ROW BEGIN SET @args = CONCAT('arg1', ' ', 'arg2', ' ', NEW.`column`); SET @cmd = CONCAT('/usr/bin/python /path/to/your/python/script.py', ' ', @args); SET @result = sys_exec(@cmd); END; ``` 在这个示例中,我们创建了一个名为 `my_trigger` 的触发器,它会在 `my_table` 表中插入新行后触发。在触发器中,我们使用 `CONCAT()` 函数将三个参数拼接成字符串,并将其存储在变量 `@args` 中。然后,我们使用 `CONCAT()` 函数将 Python 解释器路径和脚本路径与 `@args` 拼接起来,存储在变量 `@cmd` 中。最后,我们调用 MySQL 的 `sys_exec()` 函数来执行 shell 命令并将结果存储在变量 `@result` 中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值