Lines Matching full:python
7 Embedding Python in Another Application
10 The previous chapters discussed how to extend Python, that is, how to extend the
11 functionality of Python by attaching a library of C functions to it. It is also
13 embedding Python in it. Embedding provides your application with the ability to
14 implement some of the functionality of your application in Python rather than C
16 to tailor the application to their needs by writing some scripts in Python. You
17 can also use it yourself if some of the functionality can be written in Python
20 Embedding Python is similar to extending it, but not quite. The difference is
21 that when you extend Python, the main program of the application is still the
22 Python interpreter, while if you embed Python, the main program may have nothing
23 to do with Python --- instead, some parts of the application occasionally call
24 the Python interpreter to run some Python code.
26 So if you are embedding Python, you are providing your own main program. One of
27 the things this main program has to do is initialize the Python interpreter. At
29 optional calls to pass command line arguments to Python. Then later you can
33 containing Python statements to :c:func:`PyRun_SimpleString`, or you can pass a
36 described in the previous chapters to construct and use Python objects.
38 A simple demo of embedding Python can be found in the directory
45 The details of Python's C interface are given in this manual. A great deal of
54 The simplest form of embedding Python is the use of the very high level
55 interface. This interface is intended to execute a Python script without needing
59 #include <Python.h>
73 :c:func:`Py_Initialize` to inform the interpreter about paths to Python run-time
74 libraries. Next, the Python interpreter is initialized with
75 :c:func:`Py_Initialize`, followed by the execution of a hard-coded Python script
78 you may want to get the Python script from another source, perhaps a text-editor
79 routine, a file, or a database. Getting the Python code from a file can better
90 Python code from your application, but exchanging data values is quite
94 It should be noted that extending Python and embedding Python is quite the same
97 Python to C really does:
99 #. Convert data values from Python to C,
103 #. Convert the data values from the call from C to Python.
105 When embedding Python, the interface code does:
107 #. Convert data values from C to Python,
109 #. Perform a function call to a Python interface routine using the converted
112 #. Convert the data values from the call from Python to C.
117 C routine, when embedding, you call a Python routine.
119 This chapter will not discuss how to convert data from Python to C and vice
130 The first program aims to execute a function in a Python script. Like in the
131 section about the very high level interface, the Python interpreter does not
135 The code to run a function defined in a Python script is:
140 This code loads a Python script using ``argv[1]``, and calls the function named
143 :program:`call`), and use it to execute a Python script, such as:
145 .. code-block:: python
163 for data conversion between Python and C, and for error reporting. The
164 interesting part with respect to embedding Python starts with ::
172 :c:func:`PyImport_Import`. This routine needs a Python string as its argument,
187 proceeds by constructing a tuple of arguments as normal. The call to the Python
199 Extending Embedded Python
202 Until now, the embedded Python interpreter had no access to functionality from
203 the application itself. The Python API allows this by extending the embedded
206 forget for a while that the application starts the Python interpreter. Instead,
208 that gives Python access to those routines, just like you would write a normal
209 Python extension. For example::
235 :func:`emb.numargs` function accessible to the embedded Python interpreter.
236 With these extensions, the Python script can do things like
238 .. code-block:: python
244 Python.
252 Embedding Python in C++
255 It is also possible to embed Python in a C++ program; precisely how this is done
258 program. There is no need to recompile Python itself using C++.
267 compiler (and linker) in order to embed the Python interpreter into your
268 application, particularly because Python needs to load library modules
273 :file:`python{X.Y}-config` script which is generated as part of the
274 installation process (a :file:`python-config` script may also be
295 To avoid confusion between several Python installations (and especially
296 between the system Python and your own compiled Python), it is recommended
297 that you use the absolute path to :file:`python{X.Y}-config`, as in the above
303 examine Python's :file:`Makefile` (use :func:`sysconfig.get_makefile_filename`
309 .. code-block:: python