Extending and Embedding the Python Interpreter扩展和嵌入Python解释器

This document describes how to write modules in C or C++ to extend the Python interpreter with new modules. 本文档描述了如何用C或C++编写模块,以使用新模块扩展Python解释器。Those modules can not only define new functions but also new object types and their methods. 这些模块不仅可以定义新函数,还可以定义新对象类型及其方法。The document also describes how to embed the Python interpreter in another application, for use as an extension language. 该文档还描述了如何将Python解释器嵌入到另一个应用程序中,用作扩展语言。Finally, it shows how to compile and link extension modules so that they can be loaded dynamically (at run time) into the interpreter, if the underlying operating system supports this feature.最后,它展示了如何编译和链接扩展模块,以便在底层操作系统支持此功能的情况下,将它们动态(在运行时)加载到解释器中。

This document assumes basic knowledge about Python. 本文档假定了解Python的基本知识。For an informal introduction to the language, see The Python Tutorial. 有关该语言的非正式介绍,请参阅Python教程The Python Language ReferencePython语言参考 gives a more formal definition of the language. 给出了更正式的语言定义。The Python Standard LibraryPython标准库 documents the existing object types, functions and modules (both built-in and written in Python) that give the language its wide application range.文档记录了现有的对象类型、函数和模块(内置的和用Python编写的),这些都为该语言提供了广泛的应用范围。

For a detailed description of the whole Python/C API, see the separate Python/C API Reference Manual.有关整个Python/C API的详细描述,请参阅单独的Python/C API参考手册

Creating extensions without third party tools在没有第三方工具的情况下创建扩展

This section of the guide covers creating C and C++ extensions without assistance from third party tools. 本节指南介绍了在没有第三方工具帮助的情况下创建C和C++扩展。It is intended primarily for creators of those tools, rather than being a recommended way to create your own C extensions.它主要面向这些工具的创建者,而不是创建自己的C扩展的推荐方式。

Embedding the CPython runtime in a larger application在更大的应用程序中嵌入CPython运行时

Sometimes, rather than creating an extension that runs inside the Python interpreter as the main application, it is desirable to instead embed the CPython runtime inside a larger application. 有时,与其创建一个在Python解释器内部作为主应用程序运行的扩展,不如将CPython运行时嵌入到更大的应用程序中。This section covers some of the details involved in doing that successfully.本节介绍了成功实现这一目标所涉及的一些细节。