java程序中是否可以嵌入python程序

java程序中是否可以嵌入python程序,第1张

可以 有jpython jpython 是吧python 编译成java 字节码 所以 可以调用 实现在java里写python python里写java 只要导入相关的包 架设jpython编译环境就好

拿出一个例子来分析一下,不就是总结吗

#include <Pythonh>

char python_code1 = "\

import wx\n\

f = wxFrame(None, -1, 'Hello from wxPython!', size=(250, 150))\n\

fShow()\n\

";

PyRun_SimpleString(python_code1);

char python_code2 = "\

import sys\n\

syspathappend('')\n\

import embedded_sample\n\

\n\

def makeWindow(parent):\n\

win = embedded_sampleMyPanel(parent)\n\

return win\n\

";

PyObject globals = PyDict_New();

PyObject builtins = PyImport_ImportModule("__builtin__");

PyDict_SetItemString(globals, "__builtins__", builtins);

Py_DECREF(builtins);

// Execute the code to make the makeWindow function

result = PyRun_String(python_code2, Py_file_input, globals, globals);

// Was there an exception

if (! result) {

PyErr_Print();

wxPyEndBlockThreads(blocked);

return NULL;

}

Py_DECREF(result);

// Now there should be an object named 'makeWindow' in the dictionary that

// we can grab a pointer to:

PyObject func = PyDict_GetItemString(globals, "makeWindow");

你把python27dll复制成python27_ddll就可以了。然后把dll放到windows/system32目录。或者是程序执行目录中。

原因是你的程序是debug版本的。你编译成release版本就没有这个问题了。

你编译能成功可见python27的lib应该连接成了。如果动态链接库就在当前目录里,但是就是引用不成功,你可以试验使用filemon这个程序监控一下调用过程。来确认目录是否正确。如果loadlibrary不成功,通常是目录问题或者是程序版本问题。

“安装目录下的DLL中”, 这句话有些费解。直接放在执行文件同一个目录中。

以上就是关于java程序中是否可以嵌入python程序全部的内容,包括:java程序中是否可以嵌入python程序、在C/C++程序中嵌入Python脚本的注意事项总结。、在c++中嵌入python时,程序无法运行,原因是python27_d.dll丢失,如何解决等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/9714886.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存