Embedding Python question
David Max
max at neomeo.com
Wed Jan 12 13:40:58 EST 2000
Hello everyone. I am developing an application that embeds Python, and I am
having some difficulty sorting out some of the Python/C API. I have noticed
that
PyRun_SimpleString("import os\nprint 'Current dir is %s' % os.getcwd()\n");
is not the same thing as:
PyRun_SimpleString("import os\n");
PyRun_SimpleString("print 'Current dir is %s' % os.getcwd()\n");
However, if I use PyRun_String(...) instead and pass an appropriate pointer
for the globals, then it works.
I think I understand why this is the case, but I am still sketchy on where
exactly those pointers to the globals and locals is supposed to come from.
This seems to work:
PyObject *main_module = PyImport_ImportModule ("__main__");
PyObject *main_dict = PyModule_GetDict(main_module);
PyRun_String("import os\n", Py_file_input, main_dict, NULL);
PyRun_String("print 'Current dir is %s' % os.getcwd()\n", Py_file_input,
main_dict, NULL);
This works, but I'm not sure that I understand what I'm doing. For example,
I am leaving the locals as NULL. What should go there? And how would I get a
handle to it? What does it mean to use Py_file_input as the start tag as
opposed to using Py_single_input or Py_eval_input?
Also, I have tried to replace the call to execute the string "import os"
with a call to one of the PyImport functions. Nothing I've hit on so far
seems to work, and I'm not sure that I can tell the difference between some
of the functions like PyImport_ImportModuleEx, PyImport_Import and
PyImport_AddModule. If anybody can help me figure out which call I should
use, I would be appreciative.
Thanks in advance,
- David
More information about the Python-list
mailing list