how to do "load script; run script" in a loop in embedded python?

Gabriel Genellina gagsl-py2 at
Mon Apr 7 02:00:08 EDT 2008

En Thu, 03 Apr 2008 20:52:31 -0300, <lipingffeng at> escribió:

> Hi, all,
> I am currently involved in a project that needs to load/run a python
> script dynamically in a C application. The sample code is as
> following:
> PyObject *LoadScript(char *file, char *func)
> {
>     PyObject *pName, *pModule, *pDict, *pFunc;
>     pName = PyString_FromString(file);
>     pModule = PyImport_Import(pName);
>     pDict = PyModule_GetDict(pModule);
>     pFunc = PyDict_GetItemString(pDict, func);
>     return pFunc;
> }

Remember to check all PyObject* return values, NULL means there was an  
And pay attention to reference counts! Read the section about reference  
counts in both books, Extending and Embedding, and the Python API  

> The first loop is perfectly ok, but on the second loop, script loading
> is successful but running will always fail.

"fail" in what form? A Python exception? The program freezes? A core dump?

Gabriel Genellina

More information about the Python-list mailing list