你好,各位软件开发人员。
我想通过嵌入Python解释器来分发一个可编写脚本的C程序。
C程序使用Py_Initialize、PyImport_Import等来完成Python嵌入。
我正在寻找一个只分发以下组件的解决方案:
我怎样才能做到这一点?有一步一步的食谱吗?
该解决方案应该适用于Windows和Linux。
提前谢谢。
发布于 2010-03-22 19:12:00
发布于 2010-03-29 19:13:32
我只是在一台没有安装Python的计算机上测试我的可执行文件,并且它工作了。
当您将Python链接到您的可执行文件(无论是动态的还是静态的)时,您的可执行文件已经获得了基本的Python语言功能(操作符、方法、字符串、列表、元组、dict等基本结构)。没有任何其他的依赖。
然后,我让Python的setup.py通过python setup.py sdist --format=zip编译Python源代码发行版,这给了我一个名为pylib-2.6.4.zip的ZIP文件。
我进一步采取的步骤是:
char pycmd[1000]; // temporary buffer for forged Python script lines
...
Py_NoSiteFlag=1;
Py_SetProgramName(argv[0]);
Py_SetPythonHome(directoryWhereMyOwnPythonScriptsReside);
Py_InitializeEx(0);
// forge Python command to set the lookup path
// add the zipped Python distribution library to the search path as well
snprintf(
pycmd,
sizeof(pycmd),
"import sys; sys.path = ['%s/pylib-2.6.4.zip','%s']",
applicationDirectory,
directoryWhereMyOwnPythonScriptsReside
);
// ... and execute
PyRun_SimpleString(pycmd);
// now all succeeding Python import calls should be able to
// find the other modules, especially those in the zipped library
...发布于 2010-03-22 18:42:09
你看过便携式Python吗?不需要安装任何东西。只需复制包含的文件以使用解释器即可。
编辑:这是一个Windows唯一的解决方案。
https://stackoverflow.com/questions/2494468
复制相似问题