importing in Dictionairy

Arno Baan a.baan at fokkerspace.nl
Fri Feb 1 07:29:21 EST 2002


I really don't understand this, please help me.
I've got the following problem, illustrated by the example code below.
When I embed the python interpreter and I want to compile and run some
simple python code. 
It stops when I try to import a module.
When I run the same code using PyRun_SimpleString it works the way I 
expect it to. But because I need to retrieve computed values I need 
the dict object.
Can someone explain to me why I can't import modules when I use the 
dict object??

#include<Python.h>
#include<compile.h>
#include<eval.h>
#include<string>

void main(int argc, char * argv[]){
PyObject * pcode, * pdict, * pmod;

string code1 = "print 'importing string'\nimport string\nprint 'done'";
Py_Initialize();

pdict = PyDict_New();
pcode = Py_CompileString((char *)code1.c_str(), "<embed>", Py_file_input);

(void) PyEval_EvalCode((PyCodeObject *)pcode, pdict, pdict);
}

Output:
importing string

Thanks a lot,

arno



More information about the Python-list mailing list