Python嵌入 [英] Python embedding

查看:130
本文介绍了Python嵌入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个嵌入Python解释器和一个小脚本的大型二进制文件 - 我完全不了解这个静态链接,配置& make和GCC等人。请问,有人可以向我描述构建这样的可执行文件的基本步骤吗?

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

  #include< Python.h> 


main(int argc,char * argv [])
{
Py_Initialize();
PyRun_SimpleString(从进口时间开始,ctime\\\

print('Today is',ctime(time()))\\\
);
Py_Finalize();
返回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安装不够了解,并且没有正确链接所有头文件。您需要从 distutils 中获取该信息,并尝试找出如何将这些信息导入到gcc中。这可以在Python的嵌入部分的链接要求中得到更好的解释文档。



但是有一个更简单的方法。有一个名为 pymkfile.py 的脚本,它将创建一个包含所有需要的信息的 make 文件。请参阅本教程的第4部分。



从第4部分可以看到一个非常类似于你想要做的例子:


嵌入Python代码最简单的方法是使用
PyRun_SimpleString()函数。该函数向
解释器发送一行代码。由于解释器是持久的,就像在交互式Python会话中输入单独的行,这意味着您可以通过用每行代码调用PyRun_SimpleString来执行
解释器的新行。例如,
以下代码运行一个简单的Python程序来拆分和重组字符串:



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

$ / code>




你可以看到基本结构很简单。您已经初始化并完成了
步骤,并且嵌入它们之间的是对Python解释器的调用。要编译
程序,请复制代码并将其保存到名为pystring.c的文件中。首先,使用脚本
构建一个合适的Makefile:



  $ pymkfile。 py pystring> Makefile 




现在,运行 make 来实际构建代码。在创建最终应用程序后,
运行应用程序以查看结果:



  $ ./pystring 

字符串执行
rod,jane,freddy




您刚刚通过使用C封装器执行了一小段Python代码。您可以使用
相同的基本技术来执行许多不同的任务,这些任务在应用程序中使用Python嵌入式
。唯一明显的限制是,实质上,两个组件
是分开的。在嵌入式Python解释器
和主机C应用程序之间没有任何通信。

因此,您给解释器一些代码并执行并打印出来;注意
如何不必从解释器获得输出,然后输出:解释器直接将信息发送到标准输出。



I would like to have a single big binary which embeds Python interpreter and a small script - I'm totally new to this whole static linking, configure & make and GCC et al. Please, could someone describe to me the basic steps in building such executable?

I'm on MacOS 10.6, I downloaded Python 3.3 beta. Then, I created "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;
}

Now, in a folder, I have both "Python-3.3.0b1" folder and "test.c" file.

I typed:

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

but I got lots of "include" error.

But I'm not even sure if this is the right way to proceed..? Should I first somehow build just Python and then "link" it with "test.c"?

Btw., if Python gets released do they use the same procedure of "./configure" and "make"? Do they use some special options or am I able to build very similar (the same) Python executable as the one provided from python.org?

Also, I tried this "./configure" and "make" and it made a folder called "build/lib.macosx-10.6-x86_64-3.3" with lots of *.so files (?) but there is nothing called "python" or similar...?

解决方案

The problem is that gcc doesn't know enough about your Python installation and isn't linking all the headers properly. You need to get that information from distutils and try and figure out how to get that information into gcc. This is explained better in the Linking Requirements of the Embedding section of the Python documentation.

But there's an easier way. There is a script called pymkfile.py that will create a make file that includes all the information you need. See Section 4 of this tutorial.

From Section 4, a very similar example to what you're trying to do:

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嵌入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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