파이썬 인터프리터 확장 및 내장

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.

This document assumes basic knowledge about C and Python. For an informal introduction to Python, see 파이썬 자습서. 파이썬 언어 레퍼런스 gives a more formal definition of the language. 파이썬 표준 라이브러리 documents the existing object types, functions and modules (both built-in and written in Python) that give the language its wide application range.

전체 파이썬/C API에 대한 자세한 설명은 별도의 파이썬/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.

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 확장을 만드는 데 권장되는 방법이라기보다는, 주로 도구를 제작하는 사람들을 대상으로 합니다.

더 큰 응용 프로그램에 CPython 런타임을 내장하기

때로는, 파이썬 인터프리터를 메인 응용 프로그램으로 사용하고 그 안에서 실행되는 확장을 만드는 대신, CPython 런타임을 더 큰 응용 프로그램에 내장하는 것이 바람직합니다. 이 절에서는 이를 성공적으로 수행하는 데 관련된 몇 가지 세부 사항에 관해 설명합니다.