Linux/Unix > Python Essential Reference, Second Edition > B. Extending and Embedding Python > Embedding | See All Titles

< BACK | Make Note | Bookmark | CONTINUE >

155117184014003188065099048180054212144238241179195140058238111161104058145063173173115236

### Embedding

The Python interpreter can also be embedded into other applications. When embedding the interpreter on UNIX, you must include the file `config.c` (usually found in a place such as `<``python` `>/lib/python2.1/config/config.c`, where `<``python` `>` is the directory where Python was installed) and link against the library `libpython2.1.a`. (A comparable but more complex process is required on Windows and the Macintosh. Consult the online documentation for details.) The following functions are used to call the interpreter to execute code and control its operation:

    int PyRun_AnyFile(FILE *fp, char *filename)

If `fp` is an interactive device such as TTY in UNIX, this function calls `PyRun_InteractiveLoop()`. Otherwise, `PyRun_SimpleFile()` is called. If `filename` is `NULL`, a default string of `"???"` is used as the filename.

    int PyRun_SimpleString(char *command)

Executes `command` in the `_ _main_ _` module of the interpreter. Returns `0` on success, `-1` if an exception occurred.

    int PyRun_SimpleFile(FILE *fp, char *filename)

Similar to `PyRun_SimpleString()`, except that the program is read from a file `fp` .

    int PyRun_InteractiveOne(FILE *fp, char *filename)

Executes a single interactive command.

    int PyRun_InterativeLoop(FILE *fp, char *filename)

Runs the interpreter in interactive mode.

    int PyRun_String(char *str, int start, PyObject *globals, PyObject *locals)

Executes the code in `str` in the global and local namespaces defined by globals and locals. `start` is a start token to use when parsing the source code. Returns the result of execution or `NULL` if an error occurred.

    int PyRun_File(FILE *fp, char *filename, int start, PyObject *globals,
                  PyObject *locals)

Like `PyRun_String()` except that code is read from the file `fp` .

    PyObject *Py_CompileString(char *str, char *filename, int start)

Compiles code in `str` into a code object. `start` is the starting token and `filename` is the filename that will be set in the code object and used in tracebacks. Returns a code object on success, `NULL` on error.

    Py_Initialize(void)

Initializes the Python interpreter. This function should be called before using any other functions in the C API, with the exception of `Py_SetProgramName()`, `PyEval_InitThreads()`, `PyEval_ReleaseLock()`, and `PyEval_AcquireLock()`.

    int Py_IsInitialized(void)

Returns `1` if the interpreter has been initialized, `0` if not.

    Py_Finalize(void)

Cleans up the interpreter by destroying all of the sub-interpreters and objects that were created since calling `Py_Initialize()`. Normally, this function frees all the memory allocated by the interpreter. However, circular references and extension modules may introduce memory leaks that cannot be recovered by this function.

    void Py_SetProgramName(char *name)

Sets the program name that’s normally found in the `argv[0]` argument of the `sys` module. This function should only be called before `Py_Initialize()`.

    char *Py_GetProgramName(void)

Returns the program name as set by `Py_SetProgramName(``void` `)`.

    char *Py_GetPrefix(void)

Returns the prefix for installed platform-independent files.

    char *Py_GetExecPrefix(void)

Returns the `exec-prefix` for installed platform-dependent files.

    char *Py_GetProgramFullPath(void)

Returns the full pathname of the Python executable.

    char *Py_GetPath(void)

Returns the default module search path. The path is returned as a string consisting of directory names separated by a platform-dependent delimiters (`:` on UNIX, `;` on DOS/Windows, and `'\n'` on Macintosh).

    const char *Py_GetVersion(void)

Returns the version of the interpreter as a string.

    const char *Py_GetPlatform(void)

Returns the platform identifier string for the current platform.

    const char *Py_GetCopyright(void)

Returns the official copyright string.

    const char *Py_GetCompiler(void)

Returns the compiler string.

    const char *Py_GetBuildInfo(void)

Returns build information about the interpreter.

    int PySys_SetArgv(int argc, char **argv)

Sets command-line options used to populate the value of `sys.argv`. This should only be called by `Py_Initialize()`.

| Last updated on 3/28/2002 Python Essential Reference, Second Edition, © 2002 New Riders Publishing

< BACK | Make Note | Bookmark | CONTINUE >

© 2002, O'Reilly & Associates, Inc.