• Home
  • Raw
  • Download

Lines Matching refs: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.
42 The details of Python's C interface are given in this manual. A great deal of
51 The simplest form of embedding Python is the use of the very high level
52 interface. This interface is intended to execute a Python script without needing
56 #include <Python.h>
78 :c:func:`Py_Initialize` to inform the interpreter about paths to Python run-time
79 libraries. Next, the Python interpreter is initialized with
80 :c:func:`Py_Initialize`, followed by the execution of a hard-coded Python script
83 you may want to get the Python script from another source, perhaps a text-editor
84 routine, a file, or a database. Getting the Python code from a file can better
95 Python code from your application, but exchanging data values is quite
99 It should be noted that extending Python and embedding Python is quite the same
102 Python to C really does:
104 #. Convert data values from Python to C,
108 #. Convert the data values from the call from C to Python.
110 When embedding Python, the interface code does:
112 #. Convert data values from C to Python,
114 #. Perform a function call to a Python interface routine using the converted
117 #. Convert the data values from the call from Python to C.
122 C routine, when embedding, you call a Python routine.
124 This chapter will not discuss how to convert data from Python to C and vice
135 The first program aims to execute a function in a Python script. Like in the
136 section about the very high level interface, the Python interpreter does not
140 The code to run a function defined in a Python script is:
145 This code loads a Python script using ``argv[1]``, and calls the function named
148 the finished executable :program:`call`), and use it to execute a Python
169 for data conversion between Python and C, and for error reporting. The
170 interesting part with respect to embedding Python starts with ::
178 :c:func:`PyImport_Import`. This routine needs a Python string as its argument,
193 proceeds by constructing a tuple of arguments as normal. The call to the Python
205 Extending Embedded Python
208 Until now, the embedded Python interpreter had no access to functionality from
209 the application itself. The Python API allows this by extending the embedded
212 forget for a while that the application starts the Python interpreter. Instead,
214 that gives Python access to those routines, just like you would write a normal
215 Python extension. For example::
252 :func:`emb.numargs` function accessible to the embedded Python interpreter.
253 With these extensions, the Python script can do things like
261 Python.
269 Embedding Python in C++
272 It is also possible to embed Python in a C++ program; precisely how this is done
275 program. There is no need to recompile Python itself using C++.
284 compiler (and linker) in order to embed the Python interpreter into your
285 application, particularly because Python needs to load library modules
312 To avoid confusion between several Python installations (and especially
313 between the system Python and your own compiled Python), it is recommended
320 examine Python's :file:`Makefile` (use :func:`sysconfig.get_makefile_filename`