[docs] Python Doc bug, 5.3 Pure Emebedding

Bob Reese reese at ece.msstate.edu
Sat Dec 4 15:06:55 CET 2010


FYI (great job on documentation!!) 

5.3 Pure Embedding code example. 

This is a good example but under 2.3 and 2.6 on my Linux systems, the example fails to run because the sys.path variable does not have the current working directory ('[]') on it, so the module is not found (the python code is in the cwd). I am not sure why this occurs, because if the normal python interpreter is executed and the sys.path variable printed, the current working directory is on that path. 

I don't know why starting up the interpreter under C code control produces a different sys.path value from starting up the Python interpret from a shell command line (everything is the same except for the cwd being present). 

Somebody else in web land has also experienced this problem, as evidenced by (this is the same behavior that I see): 
http://coding.derkeiler.com/Archive/Python/comp.lang.python/2006-10/msg01029.html 

The real fix would be to identify what is causing this difference in sys.path values, but I have not been able to do this. It is probable that this is just an environment issue of some kind, but I don't know what it is. 

My bogus fix is to modify the example to add the current working directory to the sys.path variable as follows: 

void fixSysPath(void) { 
PyObject *pSysModule; 
PyObject *pSysPath; 
PyObject *pCwd; 
int i; 

pSysModule = PyImport_ImportModule("sys"); 
if (pSysModule == NULL) { 
myErr("Import of sys module failed"); 
exit(-1); 
} 

pSysPath = PyObject_GetAttrString(pSysModule, "path"); 
if (!pSysPath) { 
myErr("Cannot find 'sys.path'"); 
exit(-1); 
} 
pCwd = PyString_FromString(""); //null string represents cwd 
PyList_Insert(pSysPath,0,pCwd); 

} 


And in the example....: 
Py_Initialize(); 
fixSysPath(); //<<<<---Added 


Thanks. Great job on the documentation! 



---------------------------------------------------------------- 
Bob Reese 662/325-3154 (voice) 
reese at ece.msstate.edu 662/325-2298 (fax) 
http://www.ece.msstate.edu/~reese 
Associate Prof/Electrical & Computer Engineering 
Mississippi State University 
US Mail Address: 
Box 9571, Miss State, MS 39762 
Street Address: 
309 Simrall Hall, Hardy Street, Miss State, MS 39762 
---------------------------------------------------------------- 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/docs/attachments/20101204/a5539c14/attachment.html>


More information about the docs mailing list