GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • 目錄
  • 擴充和嵌入 Python 直譯器
  • 安全性注意事項
  • Your first C API extension module
  • 回報錯誤
  • 改進此頁面
  • 顯示原始碼
  • 顯示翻譯原始碼
  • 索引
  • 模組
  • Python
  • 3.15.2 documentation

This view

  • Original page
  • New address

GeezerVison is showing python.github.io — 擴充和嵌入 Python 直譯器 — Python 3.15.2 說明文件

擴充和嵌入 Python 直譯器¶

This document describes how to write modules in C or C++ to extend the Python interpreter with new modules. Those modules can do what Python code does -- define functions, object types and methods -- but also interact with native libraries or achieve better performance by avoiding the overhead of an interpreter. The document also describes how to embed the Python interpreter in another application, for use as an extension language. 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.

這份說明文件假設你具備 C 和 Python 的基礎知識。關於 Python 的非正式介紹,請參閱 Python 教學。Python 語言參考手冊給予此語言更為正式的定義。Python built-ins reference記錄了內建函式與物件型別,而 Python 標準函式庫 (Standard Library)則記錄了賦予此語言廣泛應用範圍的模組(包含內建模組與以 Python 撰寫的模組)。

關於完整的 Python/C API 詳細介紹,請參閱另外一份 Python/C API 參考手冊。

To support extensions, Python's C API (Application Programmers Interface) defines a set of functions, macros and variables that provide access to most aspects of the Python run-time system. The Python API is incorporated in a C source file by including the header "Python.h".

備註

The C extension interface is specific to CPython, and extension modules do not work on other Python implementations. In many cases, it is possible to avoid writing C extensions and preserve portability to other implementations. For example, if your use case is calling C library functions or system calls, you should consider using the ctypes module or the cffi library rather than writing custom C code. These modules let you write Python code to interface with C code and are more portable between implementations of Python than writing and compiling a C extension module.

推薦的第三方工具¶

This document only covers the basic tools for creating extensions provided as part of this version of CPython. Some third party tools offer both simpler and more sophisticated approaches to creating C and C++ extensions for Python.

While this document is aimed at extension authors, it should also be helpful to the authors of such tools. For example, the tutorial module can serve as a simple test case for a build tool or sample expected output of a code generator.

C API Tutorial¶

This tutorial describes how to write a simple module in C or C++, using the Python C API -- that is, using the basic tools provided as part of this version of CPython.

  1. Your first C API extension module

Guides for intermediate topics¶

本指南中的這一節將說明,在沒有第三方工具的協助下,如何建立 C 和 C ++ 擴充。它主要是寫給使用那些工具的創作者們,而不是讓你建立自己的 C 擴充的推薦方法。

  • Using the C API: Assorted topics

  • 擴充型別定義教學

  • Defining Extension Types: Assorted Topics

  • 建立 C 與 C++ 擴充套件

  • 建置 Windows 上的 C 和 C++ 擴充

在更大的應用程式中嵌入 CPython 運行環境 (runtime)¶

有時候,相較於建立一個擴充,使其在 Python 直譯器中可作為主應用程式運行,還不如將 CPython 運行環境嵌入至一個更大的應用程式中更可取。本節將涵蓋一些要成功完成此任務所涉及的細節。

  • 在其它 App 內嵌入 Python

© 版權所有 2001 Python Software Foundation.
此頁面採用 Python 軟體基金會授權條款第 2 版。
文件中的範例、應用技巧與其他程式碼額外採用了 Zero Clause BSD 授權條款。
更多訊息請見歷史與授權條款。

Python 軟體基金會是一家非營利法人。 敬請捐贈。

最後更新於 11月 11, 2026 (06:44 UTC)。 發現 bug?
以 Sphinx8.2.3建立。
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.