Inspired by the article "Embedding Python in Multi-Threaded C/C++ Applications" (), I felt the need for a more comprehensive coverage on the topic of embedding Python. While writing this article, I had two objectives:
受《在多线程 C/C++ 应用程序中嵌入 Python》(Linux 杂志)这篇文章的启发,我觉得自已需要对 Pyrthon 的嵌入作更全面的理解。当写这篇文章时,我有两个目标:
Now, you have got some modules written in Python by others and you want to use them. You are experienced in C/C++, but fairly new to Python. You might wonder if you can find a tool to convert them to C code, like the conversion from FORTRAN. The answer is no. Some tools can help you generate the executable from a Python module. Is the problem solved? No. Converting code into executables usually makes things even more complicated, as you must figure out how your C/C++ application communicates with the executable "black-box".
现在,你已经得到一些由其它人写的 Python 模块,并且你想使用它们。你熟悉 C/C++,但是对 Python 却很陌生。你可能想知道是不是有一个工具,它可以将这些Python 代码转化成C代码,就像FORTRAN一样。答案是否定的。一些工具可以帮助你从Python 模块中生成可执行的东西。问题解决了吗?没有。将代码转化成可执行文件通常将事情变得更加复杂,因为你必须弄清楚你的C/C++应用程序怎样与这些可执行的黑例子通信。
I am going to introduce C/C++ programmers to Python/C API, a C library that helps to embed python modules into C/C++ applications. The API library provides a bunch of C routines to initialize the Python Interpreter, call into your Python modules and finish up the embedding. The library is built with Python and distributed with all the recent Python releases.
我将向C/C++程序员介绍Pythogn/C的API,一个帮助将Puthon 模块嵌入到C/C++程序中的C库。这个API库提供一个C选择时分支来初始化Python 解释器,调用你的Python 模块并且完成嵌入。这个库是和Python 一起编译的,而且安装在所有最近的发布版中。
Part I of this article series discusses the basics of Python embedding. Part II will move on to more advanced topics. This tutorial does not teach the Python language systematically, but I will briefly describe how the Python code works when it comes up. The emphasis will be on how to integrate Python modules with your C/C++ applications. See article: .
这篇文章的第一部分讨论Python 嵌入的基础。第二部分讨论更高级的话题。这篇教程不教Python 语法,但是当有需要时,我将会简洁的描述Python 代码是怎样工作的。重点是怎样将Python 模块整合进你的C/C++应用程序中。见文章《在 C/C++ 中嵌入Python:Part:II》
In order to use the source code, you should install a recent Python release, Visual C++ (or GCC compiler on Linux). The environment that I have used to test is: Python 2.4 (Windows and Linux), Visual C++ 6.0 (Windows) or GCC 3.2 (RedHat 8.0 Linux). With Visual C++, select the Release configuration to build. Debug configuration requires Python debug library "python24_d.lib", which is not delivered with normal distributions.
为了使用源码,你应该安装一个最近的Python 版本,VisualC++(或者Linux 上的GCC编译器)。我用来测试的环境是:。。。。略
Python is a powerful interpreted language, like Java, Perl and PHP. It supports a long list of great features that any programmer would expect, two of my favorite features are "simple" and "portable". Along with the available tools and libraries, Python makes a good language for modeling and simulation developers. Best of all, it's free and the tools and libraries written for Python programmers are also free. For more details on the language, visit the official .
Python 是一个强大的粘合语言,像Java,Perl 和PHP。它支持一长串的任何程序员都期望的特性。我最喜欢的两个特性是 "简单" 和 "轻便"。结合一些可用的工具和库,Pyhotn 成为一个优秀的建模语言和模块开发者。最优的是,它以及所有用它写成的库也是自由免费的。更多细节见官方主页。
First, let us start from a sample C program that calls a function within a Python module. Here is the source file "call_function.c":
首先,让我们以一个简单的C程序调用一个在Python 模块中的函数起步。下面是 "call_function.c" 中的代码:
The Python source file "py_function.py" is as follows:
- // call_function.c - A sample of calling
- // python functions from C code
- //
- #include < Python.h > int main(int argc, char * argv[]) {
- PyObject * pName,
- *pModule,
- *pDict,
- *pFunc,
- *pValue;
- if (argc < 3) {
- printf("Usage: exe_name python_source function_name\n");
- return 1;
- }
- // Initialize the Python Interpreter
- Py_Initialize();
- // Build the name object
- pName = PyString_FromString(argv[1]);
- // Load the module object
- pModule = PyImport_Import(pName);
- // pDict is a borrowed reference
- pDict = PyModule_GetDict(pModule);
- // pFunc is also a borrowed reference
- pFunc = PyDict_GetItemString(pDict, argv[2]);
- if (PyCallable_Check(pFunc)) {
- PyObject_CallObject(pFunc, NULL);
- } else {
- PyErr_Print();
- }
- // Clean up
- Py_DECREF(pModule);
- Py_DECREF(pName);
- // Finish the Python Interpreter
- Py_Finalize();
- return 0;
- }
Python 源码文件 "py_function.py" 如下:
- '''py_function.py - Python source designed to ''''''demonstrate the use of python embedding'''def multiply() : c = 12345 * 6789 print 'The result of 12345 x 6789 :',
- c
- return c
来源: http://lib.csdn.net/article/cplusplus/47351