1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>Extending and Embedding the Python Interpreter — Python 3.5.1 documentation</title>
11
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 3.5.1 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 3.5.1 documentation" href="../contents.html" />
33
<link rel="next" title="1. Extending Python with C or C++" href="extending.html" />
34
<link rel="prev" title="37. Undocumented Modules" href="../library/undoc.html" />
35
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
36
<script type="text/javascript" src="../_static/copybutton.js"></script>
37
<script type="text/javascript" src="../_static/version_switch.js"></script>
42
<body role="document">
43
<div class="related" role="navigation" aria-label="related navigation">
46
<li class="right" style="margin-right: 10px">
47
<a href="../genindex.html" title="General Index"
48
accesskey="I">index</a></li>
50
<a href="../py-modindex.html" title="Python Module Index"
53
<a href="extending.html" title="1. Extending Python with C or C++"
54
accesskey="N">next</a> |</li>
56
<a href="../library/undoc.html" title="37. Undocumented Modules"
57
accesskey="P">previous</a> |</li>
58
<li><img src="../_static/py.png" alt=""
59
style="vertical-align: middle; margin-top: -1px"/></li>
60
<li><a href="https://www.python.org/">Python</a> »</li>
62
<span class="version_switcher_placeholder">3.5.1</span>
63
<a href="../index.html">Documentation </a> »
69
<div class="document">
70
<div class="documentwrapper">
71
<div class="bodywrapper">
72
<div class="body" role="main">
74
<div class="section" id="extending-and-embedding-the-python-interpreter">
75
<span id="extending-index"></span><h1>Extending and Embedding the Python Interpreter<a class="headerlink" href="#extending-and-embedding-the-python-interpreter" title="Permalink to this headline">¶</a></h1>
76
<p>This document describes how to write modules in C or C++ to extend the Python
77
interpreter with new modules. Those modules can not only define new functions
78
but also new object types and their methods. The document also describes how
79
to embed the Python interpreter in another application, for use as an extension
80
language. Finally, it shows how to compile and link extension modules so that
81
they can be loaded dynamically (at run time) into the interpreter, if the
82
underlying operating system supports this feature.</p>
83
<p>This document assumes basic knowledge about Python. For an informal
84
introduction to the language, see <a class="reference internal" href="../tutorial/index.html#tutorial-index"><span>The Python Tutorial</span></a>. <a class="reference internal" href="../reference/index.html#reference-index"><span>The Python Language Reference</span></a>
85
gives a more formal definition of the language. <a class="reference internal" href="../library/index.html#library-index"><span>The Python Standard Library</span></a> documents
86
the existing object types, functions and modules (both built-in and written in
87
Python) that give the language its wide application range.</p>
88
<p>For a detailed description of the whole Python/C API, see the separate
89
<a class="reference internal" href="../c-api/index.html#c-api-index"><span>Python/C API Reference Manual</span></a>.</p>
90
<div class="section" id="recommended-third-party-tools">
91
<h2>Recommended third party tools<a class="headerlink" href="#recommended-third-party-tools" title="Permalink to this headline">¶</a></h2>
92
<p>This guide only covers the basic tools for creating extensions provided
93
as part of this version of CPython. Third party tools like Cython,
94
<code class="docutils literal"><span class="pre">cffi</span></code>, SWIG and Numba offer both simpler and more sophisticated
95
approaches to creating C and C++ extensions for Python.</p>
96
<div class="admonition seealso">
97
<p class="first admonition-title">See also</p>
98
<dl class="last docutils">
99
<dt><a class="reference external" href="https://packaging.python.org/en/latest/extensions.html">Python Packaging User Guide: Binary Extensions</a></dt>
100
<dd>The Python Packaging User Guide not only covers several available
101
tools that simplify the creation of binary extensions, but also
102
discusses the various reasons why creating an extension module may be
103
desirable in the first place.</dd>
107
<div class="section" id="creating-extensions-without-third-party-tools">
108
<h2>Creating extensions without third party tools<a class="headerlink" href="#creating-extensions-without-third-party-tools" title="Permalink to this headline">¶</a></h2>
109
<p>This section of the guide covers creating C and C++ extensions without
110
assistance from third party tools. It is intended primarily for creators
111
of those tools, rather than being a recommended way to create your own
113
<div class="toctree-wrapper compound">
115
<li class="toctree-l1"><a class="reference internal" href="extending.html">1. Extending Python with C or C++</a><ul>
116
<li class="toctree-l2"><a class="reference internal" href="extending.html#a-simple-example">1.1. A Simple Example</a></li>
117
<li class="toctree-l2"><a class="reference internal" href="extending.html#intermezzo-errors-and-exceptions">1.2. Intermezzo: Errors and Exceptions</a></li>
118
<li class="toctree-l2"><a class="reference internal" href="extending.html#back-to-the-example">1.3. Back to the Example</a></li>
119
<li class="toctree-l2"><a class="reference internal" href="extending.html#the-module-s-method-table-and-initialization-function">1.4. The Module’s Method Table and Initialization Function</a></li>
120
<li class="toctree-l2"><a class="reference internal" href="extending.html#compilation-and-linkage">1.5. Compilation and Linkage</a></li>
121
<li class="toctree-l2"><a class="reference internal" href="extending.html#calling-python-functions-from-c">1.6. Calling Python Functions from C</a></li>
122
<li class="toctree-l2"><a class="reference internal" href="extending.html#extracting-parameters-in-extension-functions">1.7. Extracting Parameters in Extension Functions</a></li>
123
<li class="toctree-l2"><a class="reference internal" href="extending.html#keyword-parameters-for-extension-functions">1.8. Keyword Parameters for Extension Functions</a></li>
124
<li class="toctree-l2"><a class="reference internal" href="extending.html#building-arbitrary-values">1.9. Building Arbitrary Values</a></li>
125
<li class="toctree-l2"><a class="reference internal" href="extending.html#reference-counts">1.10. Reference Counts</a></li>
126
<li class="toctree-l2"><a class="reference internal" href="extending.html#writing-extensions-in-c">1.11. Writing Extensions in C++</a></li>
127
<li class="toctree-l2"><a class="reference internal" href="extending.html#providing-a-c-api-for-an-extension-module">1.12. Providing a C API for an Extension Module</a></li>
130
<li class="toctree-l1"><a class="reference internal" href="newtypes.html">2. Defining New Types</a><ul>
131
<li class="toctree-l2"><a class="reference internal" href="newtypes.html#the-basics">2.1. The Basics</a></li>
132
<li class="toctree-l2"><a class="reference internal" href="newtypes.html#type-methods">2.2. Type Methods</a></li>
135
<li class="toctree-l1"><a class="reference internal" href="building.html">3. Building C and C++ Extensions</a><ul>
136
<li class="toctree-l2"><a class="reference internal" href="building.html#building-c-and-c-extensions-with-distutils">3.1. Building C and C++ Extensions with distutils</a></li>
137
<li class="toctree-l2"><a class="reference internal" href="building.html#distributing-your-extension-modules">3.2. Distributing your extension modules</a></li>
140
<li class="toctree-l1"><a class="reference internal" href="windows.html">4. Building C and C++ Extensions on Windows</a><ul>
141
<li class="toctree-l2"><a class="reference internal" href="windows.html#a-cookbook-approach">4.1. A Cookbook Approach</a></li>
142
<li class="toctree-l2"><a class="reference internal" href="windows.html#differences-between-unix-and-windows">4.2. Differences Between Unix and Windows</a></li>
143
<li class="toctree-l2"><a class="reference internal" href="windows.html#using-dlls-in-practice">4.3. Using DLLs in Practice</a></li>
149
<div class="section" id="embedding-the-cpython-runtime-in-a-larger-application">
150
<h2>Embedding the CPython runtime in a larger application<a class="headerlink" href="#embedding-the-cpython-runtime-in-a-larger-application" title="Permalink to this headline">¶</a></h2>
151
<p>Sometimes, rather than creating an extension that runs inside the Python
152
interpreter as the main application, it is desirable to instead embed
153
the CPython runtime inside a larger application. This section covers
154
some of the details involved in doing that successfully.</p>
155
<div class="toctree-wrapper compound">
157
<li class="toctree-l1"><a class="reference internal" href="embedding.html">1. Embedding Python in Another Application</a><ul>
158
<li class="toctree-l2"><a class="reference internal" href="embedding.html#very-high-level-embedding">1.1. Very High Level Embedding</a></li>
159
<li class="toctree-l2"><a class="reference internal" href="embedding.html#beyond-very-high-level-embedding-an-overview">1.2. Beyond Very High Level Embedding: An overview</a></li>
160
<li class="toctree-l2"><a class="reference internal" href="embedding.html#pure-embedding">1.3. Pure Embedding</a></li>
161
<li class="toctree-l2"><a class="reference internal" href="embedding.html#extending-embedded-python">1.4. Extending Embedded Python</a></li>
162
<li class="toctree-l2"><a class="reference internal" href="embedding.html#embedding-python-in-c">1.5. Embedding Python in C++</a></li>
163
<li class="toctree-l2"><a class="reference internal" href="embedding.html#compiling-and-linking-under-unix-like-systems">1.6. Compiling and Linking under Unix-like systems</a></li>
175
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
176
<div class="sphinxsidebarwrapper">
177
<h3><a href="../contents.html">Table Of Contents</a></h3>
179
<li><a class="reference internal" href="#">Extending and Embedding the Python Interpreter</a><ul>
180
<li><a class="reference internal" href="#recommended-third-party-tools">Recommended third party tools</a></li>
181
<li><a class="reference internal" href="#creating-extensions-without-third-party-tools">Creating extensions without third party tools</a></li>
182
<li><a class="reference internal" href="#embedding-the-cpython-runtime-in-a-larger-application">Embedding the CPython runtime in a larger application</a></li>
187
<h4>Previous topic</h4>
188
<p class="topless"><a href="../library/undoc.html"
189
title="previous chapter">37. Undocumented Modules</a></p>
191
<p class="topless"><a href="extending.html"
192
title="next chapter">1. Extending Python with C or C++</a></p>
194
<ul class="this-page-menu">
195
<li><a href="../bugs.html">Report a Bug</a></li>
196
<li><a href="../_sources/extending/index.txt"
197
rel="nofollow">Show Source</a></li>
200
<div id="searchbox" style="display: none" role="search">
201
<h3>Quick search</h3>
202
<form class="search" action="../search.html" method="get">
203
<input type="text" name="q" />
204
<input type="submit" value="Go" />
205
<input type="hidden" name="check_keywords" value="yes" />
206
<input type="hidden" name="area" value="default" />
208
<p class="searchtip" style="font-size: 90%">
209
Enter search terms or a module, class or function name.
212
<script type="text/javascript">$('#searchbox').show(0);</script>
215
<div class="clearer"></div>
217
<div class="related" role="navigation" aria-label="related navigation">
220
<li class="right" style="margin-right: 10px">
221
<a href="../genindex.html" title="General Index"
224
<a href="../py-modindex.html" title="Python Module Index"
227
<a href="extending.html" title="1. Extending Python with C or C++"
230
<a href="../library/undoc.html" title="37. Undocumented Modules"
232
<li><img src="../_static/py.png" alt=""
233
style="vertical-align: middle; margin-top: -1px"/></li>
234
<li><a href="https://www.python.org/">Python</a> »</li>
236
<span class="version_switcher_placeholder">3.5.1</span>
237
<a href="../index.html">Documentation </a> »
243
© <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
245
The Python Software Foundation is a non-profit corporation.
246
<a href="https://www.python.org/psf/donations/">Please donate.</a>
248
Last updated on Jan 22, 2016.
249
<a href="../bugs.html">Found a bug</a>?
251
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'