Distinguish Extension Modules from Embedding
Core idea
A CPython extension exposes native code to Python, usually through a module initialization function and C API calls. Embedding reverses the direction: a native host initializes Python and manages interpreter lifetime. Both require compatible headers, libraries and build settings. The examples inspect the running interpreter, not a native build; the accompanying trusted local C fixture demonstrates an extension separately.
import mathfrom importlib.machinery import EXTENSION_SUFFIXESorigin=math.__spec__.originprint(origin == 'built-in' or any(origin.endswith(s) for s in EXTENSION_SUFFIXES))print(math.isqrt(81))The local math module is built in; the check also recognizes shared extensions. Packaging varies by distribution.
Expected output:
True9Discover build configuration
import sysconfigprint(bool(sysconfig.get_path('include')))print(bool(sysconfig.get_config_var('EXT_SUFFIX')))Configuration supplies candidate include and extension naming settings; presence of strings does not prove a compiler is installed.
Expected output:
TrueTrueCommon mistake
Do not import arbitrary shared libraries or assume every implementation exposes CPython’s C API. Keep native builds outside the learner sandbox.
Practice
Review the accompanying native fixture: identify Python.h, the method table and PyInit entry point; contrast this with a host using Py_Initialize.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Python C API overview
Check your understanding of python c api overview.
What is printed?
import sysprint(sys.implementation.name)What is printed?
import mathprint(math.isqrt(100))Who initializes Python in embedding?
What is needed to build an extension?