Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | |
| 4 | .. _embedding: |
| 5 | |
| 6 | *************************************** |
| 7 | Embedding Python in Another Application |
| 8 | *************************************** |
| 9 | |
| 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 |
| 12 | possible to do it the other way around: enrich your C/C++ application by |
| 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 |
| 15 | or C++. This can be used for many purposes; one example would be to allow users |
| 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 |
| 18 | more easily. |
| 19 | |
| 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. |
| 25 | |
| 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 |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 28 | the very least, you have to call the function :c:func:`Py_Initialize`. There are |
Georg Brandl | c575c90 | 2008-09-13 17:46:05 +0000 | [diff] [blame] | 29 | optional calls to pass command line arguments to Python. Then later you can |
| 30 | call the interpreter from any part of the application. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 31 | |
| 32 | There are several different ways to call the interpreter: you can pass a string |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 33 | containing Python statements to :c:func:`PyRun_SimpleString`, or you can pass a |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 34 | stdio file pointer and a file name (for identification in error messages only) |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 35 | to :c:func:`PyRun_SimpleFile`. You can also call the lower-level operations |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 36 | described in the previous chapters to construct and use Python objects. |
| 37 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 38 | |
| 39 | .. seealso:: |
| 40 | |
| 41 | :ref:`c-api-index` |
| 42 | The details of Python's C interface are given in this manual. A great deal of |
| 43 | necessary information can be found here. |
| 44 | |
| 45 | |
| 46 | .. _high-level-embedding: |
| 47 | |
| 48 | Very High Level Embedding |
| 49 | ========================= |
| 50 | |
| 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 |
| 53 | to interact with the application directly. This can for example be used to |
| 54 | perform some operation on a file. :: |
| 55 | |
| 56 | #include <Python.h> |
| 57 | |
| 58 | int |
| 59 | main(int argc, char *argv[]) |
| 60 | { |
Andrew Svetlov | e1fa22a | 2012-10-31 16:03:14 +0200 | [diff] [blame] | 61 | Py_SetProgramName(argv[0]); /* optional but recommended */ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 62 | Py_Initialize(); |
| 63 | PyRun_SimpleString("from time import time,ctime\n" |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 64 | "print('Today is', ctime(time()))\n"); |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 65 | Py_Finalize(); |
| 66 | return 0; |
| 67 | } |
| 68 | |
Georg Brandl | 4552e3f | 2012-11-02 07:34:37 +0100 | [diff] [blame] | 69 | The :c:func:`Py_SetProgramName` function should be called before |
| 70 | :c:func:`Py_Initialize` to inform the interpreter about paths to Python run-time |
| 71 | libraries. Next, the Python interpreter is initialized with |
| 72 | :c:func:`Py_Initialize`, followed by the execution of a hard-coded Python script |
| 73 | that prints the date and time. Afterwards, the :c:func:`Py_Finalize` call shuts |
| 74 | the interpreter down, followed by the end of the program. In a real program, |
| 75 | you may want to get the Python script from another source, perhaps a text-editor |
| 76 | routine, a file, or a database. Getting the Python code from a file can better |
| 77 | be done by using the :c:func:`PyRun_SimpleFile` function, which saves you the |
| 78 | trouble of allocating memory space and loading the file contents. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 79 | |
| 80 | |
| 81 | .. _lower-level-embedding: |
| 82 | |
| 83 | Beyond Very High Level Embedding: An overview |
| 84 | ============================================= |
| 85 | |
| 86 | The high level interface gives you the ability to execute arbitrary pieces of |
| 87 | Python code from your application, but exchanging data values is quite |
| 88 | cumbersome to say the least. If you want that, you should use lower level calls. |
| 89 | At the cost of having to write more C code, you can achieve almost anything. |
| 90 | |
| 91 | It should be noted that extending Python and embedding Python is quite the same |
| 92 | activity, despite the different intent. Most topics discussed in the previous |
| 93 | chapters are still valid. To show this, consider what the extension code from |
| 94 | Python to C really does: |
| 95 | |
| 96 | #. Convert data values from Python to C, |
| 97 | |
| 98 | #. Perform a function call to a C routine using the converted values, and |
| 99 | |
| 100 | #. Convert the data values from the call from C to Python. |
| 101 | |
| 102 | When embedding Python, the interface code does: |
| 103 | |
| 104 | #. Convert data values from C to Python, |
| 105 | |
| 106 | #. Perform a function call to a Python interface routine using the converted |
| 107 | values, and |
| 108 | |
| 109 | #. Convert the data values from the call from Python to C. |
| 110 | |
| 111 | As you can see, the data conversion steps are simply swapped to accommodate the |
| 112 | different direction of the cross-language transfer. The only difference is the |
| 113 | routine that you call between both data conversions. When extending, you call a |
| 114 | C routine, when embedding, you call a Python routine. |
| 115 | |
| 116 | This chapter will not discuss how to convert data from Python to C and vice |
| 117 | versa. Also, proper use of references and dealing with errors is assumed to be |
| 118 | understood. Since these aspects do not differ from extending the interpreter, |
| 119 | you can refer to earlier chapters for the required information. |
| 120 | |
| 121 | |
| 122 | .. _pure-embedding: |
| 123 | |
| 124 | Pure Embedding |
| 125 | ============== |
| 126 | |
| 127 | The first program aims to execute a function in a Python script. Like in the |
| 128 | section about the very high level interface, the Python interpreter does not |
| 129 | directly interact with the application (but that will change in the next |
| 130 | section). |
| 131 | |
| 132 | The code to run a function defined in a Python script is: |
| 133 | |
| 134 | .. literalinclude:: ../includes/run-func.c |
| 135 | |
| 136 | |
| 137 | This code loads a Python script using ``argv[1]``, and calls the function named |
| 138 | in ``argv[2]``. Its integer arguments are the other values of the ``argv`` |
Antoine Pitrou | 71bca34 | 2011-11-30 21:19:21 +0100 | [diff] [blame] | 139 | array. If you :ref:`compile and link <compiling>` this program (let's call |
| 140 | the finished executable :program:`call`), and use it to execute a Python |
Ezio Melotti | a19ebdb | 2013-02-22 07:46:22 +0200 | [diff] [blame] | 141 | script, such as: |
| 142 | |
| 143 | .. code-block:: python |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 144 | |
| 145 | def multiply(a,b): |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 146 | print("Will compute", a, "times", b) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 147 | c = 0 |
| 148 | for i in range(0, a): |
| 149 | c = c + b |
| 150 | return c |
| 151 | |
| 152 | then the result should be:: |
| 153 | |
| 154 | $ call multiply multiply 3 2 |
| 155 | Will compute 3 times 2 |
| 156 | Result of call: 6 |
| 157 | |
| 158 | Although the program is quite large for its functionality, most of the code is |
| 159 | for data conversion between Python and C, and for error reporting. The |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 160 | interesting part with respect to embedding Python starts with :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 161 | |
| 162 | Py_Initialize(); |
Georg Brandl | 3aa0c9d | 2012-07-01 09:43:20 +0200 | [diff] [blame] | 163 | pName = PyUnicode_FromString(argv[1]); |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 164 | /* Error checking of pName left out */ |
| 165 | pModule = PyImport_Import(pName); |
| 166 | |
| 167 | After initializing the interpreter, the script is loaded using |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 168 | :c:func:`PyImport_Import`. This routine needs a Python string as its argument, |
Georg Brandl | 3aa0c9d | 2012-07-01 09:43:20 +0200 | [diff] [blame] | 169 | which is constructed using the :c:func:`PyUnicode_FromString` data conversion |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 170 | routine. :: |
| 171 | |
| 172 | pFunc = PyObject_GetAttrString(pModule, argv[2]); |
| 173 | /* pFunc is a new reference */ |
| 174 | |
| 175 | if (pFunc && PyCallable_Check(pFunc)) { |
| 176 | ... |
| 177 | } |
| 178 | Py_XDECREF(pFunc); |
| 179 | |
| 180 | Once the script is loaded, the name we're looking for is retrieved using |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 181 | :c:func:`PyObject_GetAttrString`. If the name exists, and the object returned is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 182 | callable, you can safely assume that it is a function. The program then |
| 183 | proceeds by constructing a tuple of arguments as normal. The call to the Python |
| 184 | function is then made with:: |
| 185 | |
| 186 | pValue = PyObject_CallObject(pFunc, pArgs); |
| 187 | |
| 188 | Upon return of the function, ``pValue`` is either *NULL* or it contains a |
| 189 | reference to the return value of the function. Be sure to release the reference |
| 190 | after examining the value. |
| 191 | |
| 192 | |
| 193 | .. _extending-with-embedding: |
| 194 | |
| 195 | Extending Embedded Python |
| 196 | ========================= |
| 197 | |
| 198 | Until now, the embedded Python interpreter had no access to functionality from |
| 199 | the application itself. The Python API allows this by extending the embedded |
| 200 | interpreter. That is, the embedded interpreter gets extended with routines |
| 201 | provided by the application. While it sounds complex, it is not so bad. Simply |
| 202 | forget for a while that the application starts the Python interpreter. Instead, |
| 203 | consider the application to be a set of subroutines, and write some glue code |
| 204 | that gives Python access to those routines, just like you would write a normal |
| 205 | Python extension. For example:: |
| 206 | |
| 207 | static int numargs=0; |
| 208 | |
| 209 | /* Return the number of arguments of the application command line */ |
| 210 | static PyObject* |
| 211 | emb_numargs(PyObject *self, PyObject *args) |
| 212 | { |
| 213 | if(!PyArg_ParseTuple(args, ":numargs")) |
| 214 | return NULL; |
Georg Brandl | c877a7c | 2010-11-26 11:55:48 +0000 | [diff] [blame] | 215 | return PyLong_FromLong(numargs); |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | static PyMethodDef EmbMethods[] = { |
| 219 | {"numargs", emb_numargs, METH_VARARGS, |
| 220 | "Return the number of arguments received by the process."}, |
| 221 | {NULL, NULL, 0, NULL} |
| 222 | }; |
| 223 | |
Georg Brandl | 05b482c | 2008-12-07 22:45:56 +0000 | [diff] [blame] | 224 | static PyModuleDef EmbModule = { |
| 225 | PyModuleDef_HEAD_INIT, "emb", NULL, -1, EmbMethods, |
| 226 | NULL, NULL, NULL, NULL |
| 227 | }; |
| 228 | |
Georg Brandl | acc68cc | 2008-12-09 23:48:44 +0000 | [diff] [blame] | 229 | static PyObject* |
| 230 | PyInit_emb(void) |
| 231 | { |
| 232 | return PyModule_Create(&EmbModule); |
| 233 | } |
| 234 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 235 | Insert the above code just above the :c:func:`main` function. Also, insert the |
| 236 | following two statements before the call to :c:func:`Py_Initialize`:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 237 | |
| 238 | numargs = argc; |
Georg Brandl | acc68cc | 2008-12-09 23:48:44 +0000 | [diff] [blame] | 239 | PyImport_AppendInittab("emb", &PyInit_emb); |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 240 | |
| 241 | These two lines initialize the ``numargs`` variable, and make the |
| 242 | :func:`emb.numargs` function accessible to the embedded Python interpreter. |
Ezio Melotti | a19ebdb | 2013-02-22 07:46:22 +0200 | [diff] [blame] | 243 | With these extensions, the Python script can do things like |
| 244 | |
| 245 | .. code-block:: python |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 246 | |
| 247 | import emb |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 248 | print("Number of arguments", emb.numargs()) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 249 | |
| 250 | In a real application, the methods will expose an API of the application to |
| 251 | Python. |
| 252 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 253 | .. TODO: threads, code examples do not really behave well if errors happen |
| 254 | (what to watch out for) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 255 | |
| 256 | |
| 257 | .. _embeddingincplusplus: |
| 258 | |
| 259 | Embedding Python in C++ |
| 260 | ======================= |
| 261 | |
| 262 | It is also possible to embed Python in a C++ program; precisely how this is done |
| 263 | will depend on the details of the C++ system used; in general you will need to |
| 264 | write the main program in C++, and use the C++ compiler to compile and link your |
| 265 | program. There is no need to recompile Python itself using C++. |
| 266 | |
| 267 | |
Antoine Pitrou | 71bca34 | 2011-11-30 21:19:21 +0100 | [diff] [blame] | 268 | .. _compiling: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 269 | |
Antoine Pitrou | 71bca34 | 2011-11-30 21:19:21 +0100 | [diff] [blame] | 270 | Compiling and Linking under Unix-like systems |
| 271 | ============================================= |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 272 | |
Antoine Pitrou | 71bca34 | 2011-11-30 21:19:21 +0100 | [diff] [blame] | 273 | It is not necessarily trivial to find the right flags to pass to your |
| 274 | compiler (and linker) in order to embed the Python interpreter into your |
| 275 | application, particularly because Python needs to load library modules |
| 276 | implemented as C dynamic extensions (:file:`.so` files) linked against |
| 277 | it. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 278 | |
Antoine Pitrou | 71bca34 | 2011-11-30 21:19:21 +0100 | [diff] [blame] | 279 | To find out the required compiler and linker flags, you can execute the |
| 280 | :file:`python{X.Y}-config` script which is generated as part of the |
Éric Araujo | 9c7b919 | 2012-01-15 02:31:58 +0100 | [diff] [blame] | 281 | installation process (a :file:`python3-config` script may also be |
Antoine Pitrou | 71bca34 | 2011-11-30 21:19:21 +0100 | [diff] [blame] | 282 | available). This script has several options, of which the following will |
| 283 | be directly useful to you: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 284 | |
Antoine Pitrou | 71bca34 | 2011-11-30 21:19:21 +0100 | [diff] [blame] | 285 | * ``pythonX.Y-config --cflags`` will give you the recommended flags when |
| 286 | compiling:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 287 | |
Ned Deily | ecf0851 | 2013-05-26 18:53:39 -0700 | [diff] [blame] | 288 | $ /opt/bin/python3.3-config --cflags |
| 289 | -I/opt/include/python3.3m -I/opt/include/python3.3m -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes |
Antoine Pitrou | 71bca34 | 2011-11-30 21:19:21 +0100 | [diff] [blame] | 290 | |
| 291 | * ``pythonX.Y-config --ldflags`` will give you the recommended flags when |
| 292 | linking:: |
| 293 | |
Ned Deily | ecf0851 | 2013-05-26 18:53:39 -0700 | [diff] [blame] | 294 | $ /opt/bin/python3.3-config --ldflags |
| 295 | -L/opt/lib/python3.3/config-3.3m -lpthread -ldl -lutil -lm -lpython3.3m -Xlinker -export-dynamic |
Antoine Pitrou | 71bca34 | 2011-11-30 21:19:21 +0100 | [diff] [blame] | 296 | |
| 297 | .. note:: |
| 298 | To avoid confusion between several Python installations (and especially |
| 299 | between the system Python and your own compiled Python), it is recommended |
| 300 | that you use the absolute path to :file:`python{X.Y}-config`, as in the above |
| 301 | example. |
| 302 | |
| 303 | If this procedure doesn't work for you (it is not guaranteed to work for |
Éric Araujo | 9c7b919 | 2012-01-15 02:31:58 +0100 | [diff] [blame] | 304 | all Unix-like platforms; however, we welcome :ref:`bug reports <reporting-bugs>`) |
| 305 | you will have to read your system's documentation about dynamic linking and/or |
| 306 | examine Python's :file:`Makefile` (use :func:`sysconfig.get_makefile_filename` |
| 307 | to find its location) and compilation |
Antoine Pitrou | 71bca34 | 2011-11-30 21:19:21 +0100 | [diff] [blame] | 308 | options. In this case, the :mod:`sysconfig` module is a useful tool to |
| 309 | programmatically extract the configuration values that you will want to |
Ned Deily | 51cee7d | 2013-06-24 14:22:09 -0700 | [diff] [blame] | 310 | combine together. For example: |
Ezio Melotti | a19ebdb | 2013-02-22 07:46:22 +0200 | [diff] [blame] | 311 | |
| 312 | .. code-block:: python |
Antoine Pitrou | 71bca34 | 2011-11-30 21:19:21 +0100 | [diff] [blame] | 313 | |
| 314 | >>> import sysconfig |
Ned Deily | 51cee7d | 2013-06-24 14:22:09 -0700 | [diff] [blame] | 315 | >>> sysconfig.get_config_var('LIBS') |
| 316 | '-lpthread -ldl -lutil' |
Antoine Pitrou | 71bca34 | 2011-11-30 21:19:21 +0100 | [diff] [blame] | 317 | >>> sysconfig.get_config_var('LINKFORSHARED') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 318 | '-Xlinker -export-dynamic' |
| 319 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 320 | |
Antoine Pitrou | 71bca34 | 2011-11-30 21:19:21 +0100 | [diff] [blame] | 321 | .. XXX similar documentation for Windows missing |