gpt4 book ai didi

Python嵌入

转载 作者:太空狗 更新时间:2023-10-30 01:03:08 26 4
gpt4 key购买 nike

我想要一个大的二进制文件,它嵌入了 Python 解释器和一个小脚本——我对整个静态链接、配置和制作以及 GCC 等完全陌生。拜托,有人可以向我描述构建此类可执行文件的基本步骤吗?

我在 MacOS 10.6 上,我下载了 Python 3.3 beta。然后,我创建了“test.c”:

#include <Python.h>

int
main(int argc, char *argv[])
{
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print('Today is', ctime(time()))\n");
Py_Finalize();
return 0;
}

现在,在一个文件夹中,我同时拥有“Python-3.3.0b1”文件夹和“test.c”文件。

我输入:

gcc -I ./Python-3.3.0b1 -o test test.c

但我遇到了很多“包含”错误。

但我什至不确定这是否是正确的方法......?我是否应该首先以某种方式只构建 Python,然后将其与“test.c”“链接”?

顺便说一句,如果 Python 发布了,他们会使用相同的“./configure”和“make”过程吗?他们是否使用了一些特殊选项,或者我是否能够构建与 python.org 提供的非常相似(相同)的 Python 可执行文件?

此外,我尝试了这个“./configure”和“make”,它创建了一个名为“build/lib.macosx-10.6-x86_64-3.3”的文件夹,里面有很多 *.so 文件(?)但是什么也没有称为“ python ”或类似...?

最佳答案

问题是 gcc 对您的 Python 安装了解不够,并且没有正确链接所有 header 。您需要从 distutils 获取该信息,并尝试弄清楚如何将该信息导入 gcc。这在 Linking Requirements 中有更好的解释。 Python 文档的嵌入部分。

但还有更简单的方法。有一个名为 pymkfile.py 的脚本,它将创建一个包含您需要的所有信息的 make 文件。参见 this tutorial 的第 4 节.

第 4 节中的一个非常与您尝试执行的操作类似的示例:

The simplest method of embedding Python code is to use the PyRun_SimpleString() function. This function sends a single line of code to the interpreter. Because the interpreter is persistent, it's like typing the individual lines in an interactive Python session, which means that you can execute new lines to the interpreter by calling PyRun_SimpleString with each line of code. For example, the following code runs a simple Python program to split and reassemble a string:

#include <Python.h>
int main()
{
printf("String execution\n");
Py_Initialize();
PyRun_SimpleString("import string");
PyRun_SimpleString("words = string.split('rod jane freddy')");
PyRun_SimpleString("print string.join(words,', ')");
Py_Finalize();
return 0;
}

You can see that the basic structure is simple. You have the initialize and finalize steps, and embedded between are the calls to the Python interpreter. To compile the program, copy the code and save it to a file called pystring.c. First, use the script to build a suitable Makefile:

$ pymkfile.py pystring > Makefile

Now, run make to actually build the code. After the final application has been created, run the application to see the results:

$ ./pystring

String execution
rod, jane, freddy

You've just executed a simple bit of Python code by using a C wrapper. You could use the same basic technique to perform many different tasks that use Python embedded in an application. The only obvious limitation is that, in essence, the two components are separate. There's no communication between the embedded Python interpreter and the host C application.

So, you gave the interpreter some code and it executed and printed it out; note how you didn't have to take the output from the interpreter, then print it: The interpreter sent the information directly to the standard output.

关于Python嵌入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11228941/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com