| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="api.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="api.html" title='Python/C API Reference Manual' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="prev" href="exceptions.html" /> |
| 13 | <link rel="parent" href="intro.html" /> |
| 14 | <link rel="next" href="veryhigh.html" /> |
| 15 | <meta name='aesop' content='information' /> |
| 16 | <title>1.4 Embedding Python </title> |
| 17 | </head> |
| 18 | <body> |
| 19 | <DIV CLASS="navigation"> |
| 20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 22 | <tr> |
| 23 | <td class='online-navigation'><a rel="prev" title="1.3 Exceptions" |
| 24 | href="exceptions.html"><img src='../icons/previous.png' |
| 25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 26 | <td class='online-navigation'><a rel="parent" title="1. Introduction" |
| 27 | href="intro.html"><img src='../icons/up.png' |
| 28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 29 | <td class='online-navigation'><a rel="next" title="2. The Very High" |
| 30 | href="veryhigh.html"><img src='../icons/next.png' |
| 31 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 32 | <td align="center" width="100%">Python/C API Reference Manual</td> |
| 33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 34 | href="contents.html"><img src='../icons/contents.png' |
| 35 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 36 | <td class='online-navigation'><img src='../icons/blank.png' |
| 37 | border='0' height='32' alt='' width='32' /></td> |
| 38 | <td class='online-navigation'><a rel="index" title="Index" |
| 39 | href="genindex.html"><img src='../icons/index.png' |
| 40 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 41 | </tr></table> |
| 42 | <div class='online-navigation'> |
| 43 | <b class="navlabel">Previous:</b> |
| 44 | <a class="sectref" rel="prev" href="exceptions.html">1.3 Exceptions</A> |
| 45 | <b class="navlabel">Up:</b> |
| 46 | <a class="sectref" rel="parent" href="intro.html">1. Introduction</A> |
| 47 | <b class="navlabel">Next:</b> |
| 48 | <a class="sectref" rel="next" href="veryhigh.html">2. The Very High</A> |
| 49 | </div> |
| 50 | <hr /></div> |
| 51 | </DIV> |
| 52 | <!--End of Navigation Panel--> |
| 53 | |
| 54 | <H1><A NAME="SECTION003400000000000000000"></A><A NAME="embedding"></A> |
| 55 | <BR> |
| 56 | 1.4 Embedding Python |
| 57 | </H1> |
| 58 | |
| 59 | <P> |
| 60 | The one important task that only embedders (as opposed to extension |
| 61 | writers) of the Python interpreter have to worry about is the |
| 62 | initialization, and possibly the finalization, of the Python |
| 63 | interpreter. Most functionality of the interpreter can only be used |
| 64 | after the interpreter has been initialized. |
| 65 | |
| 66 | <P> |
| 67 | The basic initialization function is |
| 68 | <tt class="cfunction">Py_Initialize()</tt><a id='l2h-30' xml:id='l2h-30'></a>. |
| 69 | This initializes the table of loaded modules, and creates the |
| 70 | fundamental modules <tt class="module">__builtin__</tt><a id='l2h-31' xml:id='l2h-31'></a>, |
| 71 | <tt class="module">__main__</tt><a id='l2h-32' xml:id='l2h-32'></a>, <tt class="module">sys</tt><a id='l2h-33' xml:id='l2h-33'></a>, |
| 72 | and <tt class="module">exceptions</tt>.<a id='l2h-34' xml:id='l2h-34'></a> It also initializes |
| 73 | the module search path (<code>sys.path</code>).<a id='l2h-27' xml:id='l2h-27'></a><a id='l2h-29' xml:id='l2h-29'></a> |
| 74 | <P> |
| 75 | <tt class="cfunction">Py_Initialize()</tt> does not set the ``script argument list'' |
| 76 | (<code>sys.argv</code>). If this variable is needed by Python code that |
| 77 | will be executed later, it must be set explicitly with a call to |
| 78 | <code>PySys_SetArgv(<var>argc</var>, |
| 79 | <var>argv</var>)</code><a id='l2h-35' xml:id='l2h-35'></a> subsequent to the call to |
| 80 | <tt class="cfunction">Py_Initialize()</tt>. |
| 81 | |
| 82 | <P> |
| 83 | On most systems (in particular, on <span class="Unix">Unix</span> and Windows, although the |
| 84 | details are slightly different), |
| 85 | <tt class="cfunction">Py_Initialize()</tt> calculates the module search path based |
| 86 | upon its best guess for the location of the standard Python |
| 87 | interpreter executable, assuming that the Python library is found in a |
| 88 | fixed location relative to the Python interpreter executable. In |
| 89 | particular, it looks for a directory named |
| 90 | <span class="file">lib/python2.4</span> relative to the parent directory where |
| 91 | the executable named <span class="file">python</span> is found on the shell command |
| 92 | search path (the environment variable <a class="envvar" id='l2h-36' xml:id='l2h-36'>PATH</a>). |
| 93 | |
| 94 | <P> |
| 95 | For instance, if the Python executable is found in |
| 96 | <span class="file">/usr/local/bin/python</span>, it will assume that the libraries are in |
| 97 | <span class="file">/usr/local/lib/python2.4</span>. (In fact, this particular path |
| 98 | is also the ``fallback'' location, used when no executable file named |
| 99 | <span class="file">python</span> is found along <a class="envvar" id='l2h-37' xml:id='l2h-37'>PATH</a>.) The user can override |
| 100 | this behavior by setting the environment variable <a class="envvar" id='l2h-38' xml:id='l2h-38'>PYTHONHOME</a>, |
| 101 | or insert additional directories in front of the standard path by |
| 102 | setting <a class="envvar" id='l2h-39' xml:id='l2h-39'>PYTHONPATH</a>. |
| 103 | |
| 104 | <P> |
| 105 | The embedding application can steer the search by calling |
| 106 | <code>Py_SetProgramName(<var>file</var>)</code><a id='l2h-40' xml:id='l2h-40'></a> <em>before</em> calling |
| 107 | <tt class="cfunction">Py_Initialize()</tt>. Note that <a class="envvar" id='l2h-41' xml:id='l2h-41'>PYTHONHOME</a> still |
| 108 | overrides this and <a class="envvar" id='l2h-42' xml:id='l2h-42'>PYTHONPATH</a> is still inserted in front of |
| 109 | the standard path. An application that requires total control has to |
| 110 | provide its own implementation of |
| 111 | <tt class="cfunction">Py_GetPath()</tt><a id='l2h-43' xml:id='l2h-43'></a>, |
| 112 | <tt class="cfunction">Py_GetPrefix()</tt><a id='l2h-44' xml:id='l2h-44'></a>, |
| 113 | <tt class="cfunction">Py_GetExecPrefix()</tt><a id='l2h-45' xml:id='l2h-45'></a>, and |
| 114 | <tt class="cfunction">Py_GetProgramFullPath()</tt><a id='l2h-46' xml:id='l2h-46'></a> (all |
| 115 | defined in <span class="file">Modules/getpath.c</span>). |
| 116 | |
| 117 | <P> |
| 118 | Sometimes, it is desirable to ``uninitialize'' Python. For instance, |
| 119 | the application may want to start over (make another call to |
| 120 | <tt class="cfunction">Py_Initialize()</tt>) or the application is simply done with its |
| 121 | use of Python and wants to free all memory allocated by Python. This |
| 122 | can be accomplished by calling <tt class="cfunction">Py_Finalize()</tt>. The function |
| 123 | <tt class="cfunction">Py_IsInitialized()</tt><a id='l2h-47' xml:id='l2h-47'></a> returns |
| 124 | true if Python is currently in the initialized state. More |
| 125 | information about these functions is given in a later chapter. |
| 126 | |
| 127 | <DIV CLASS="navigation"> |
| 128 | <div class='online-navigation'> |
| 129 | <p></p><hr /> |
| 130 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 131 | <tr> |
| 132 | <td class='online-navigation'><a rel="prev" title="1.3 Exceptions" |
| 133 | href="exceptions.html"><img src='../icons/previous.png' |
| 134 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 135 | <td class='online-navigation'><a rel="parent" title="1. Introduction" |
| 136 | href="intro.html"><img src='../icons/up.png' |
| 137 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 138 | <td class='online-navigation'><a rel="next" title="2. The Very High" |
| 139 | href="veryhigh.html"><img src='../icons/next.png' |
| 140 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 141 | <td align="center" width="100%">Python/C API Reference Manual</td> |
| 142 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 143 | href="contents.html"><img src='../icons/contents.png' |
| 144 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 145 | <td class='online-navigation'><img src='../icons/blank.png' |
| 146 | border='0' height='32' alt='' width='32' /></td> |
| 147 | <td class='online-navigation'><a rel="index" title="Index" |
| 148 | href="genindex.html"><img src='../icons/index.png' |
| 149 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 150 | </tr></table> |
| 151 | <div class='online-navigation'> |
| 152 | <b class="navlabel">Previous:</b> |
| 153 | <a class="sectref" rel="prev" href="exceptions.html">1.3 Exceptions</A> |
| 154 | <b class="navlabel">Up:</b> |
| 155 | <a class="sectref" rel="parent" href="intro.html">1. Introduction</A> |
| 156 | <b class="navlabel">Next:</b> |
| 157 | <a class="sectref" rel="next" href="veryhigh.html">2. The Very High</A> |
| 158 | </div> |
| 159 | </div> |
| 160 | <hr /> |
| 161 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 162 | </DIV> |
| 163 | <!--End of Navigation Panel--> |
| 164 | <ADDRESS> |
| 165 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 166 | </ADDRESS> |
| 167 | </BODY> |
| 168 | </HTML> |