Merge issue #16370: Mention Py_SetProgramName in example for very high level embedding.

This commit is contained in:
Andrew Svetlov 2012-10-31 16:04:22 +02:00
commit 7258e1324e
1 changed files with 13 additions and 8 deletions

View File

@ -58,6 +58,7 @@ perform some operation on a file. ::
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
Py_SetProgramName(argv[0]); /* optional but recommended */
Py_Initialize(); Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n" PyRun_SimpleString("from time import time,ctime\n"
"print('Today is', ctime(time()))\n"); "print('Today is', ctime(time()))\n");
@ -65,14 +66,18 @@ perform some operation on a file. ::
return 0; return 0;
} }
The above code first initializes the Python interpreter with Function :c:func:`Py_SetProgramName` should be called before
:c:func:`Py_Initialize`, followed by the execution of a hard-coded Python script :c:func:`Py_Initialize` to inform the interpreter about paths to
that print the date and time. Afterwards, the :c:func:`Py_Finalize` call shuts Python run-time libraries. Next initialize the Python interpreter
the interpreter down, followed by the end of the program. In a real program, with :c:func:`Py_Initialize`, followed by the execution of a
you may want to get the Python script from another source, perhaps a text-editor hard-coded Python script that prints the date and time. Afterwards,
routine, a file, or a database. Getting the Python code from a file can better the :c:func:`Py_Finalize` call shuts the interpreter down, followed by
be done by using the :c:func:`PyRun_SimpleFile` function, which saves you the the end of the program. In a real program, you may want to get the
trouble of allocating memory space and loading the file contents. Python script from another source, perhaps a text-editor routine, a
file, or a database. Getting the Python code from a file can better
be done by using the :c:func:`PyRun_SimpleFile` function, which saves
you the trouble of allocating memory space and loading the file
contents.
.. _lower-level-embedding: .. _lower-level-embedding: