I have the following C++ code and am attempting to embed Python 2.5, but although the "import sys" statement works, attempting to reference "sys.path" from inside a function after that point fails. It's as if it's not treating it as a normal module but as any other global variable which I'd have to explicitly qualify.
Py_InitializeEx(0); // the zero skips registration of signal handlers. PyObject* ourNamespace_ = PyDict_New(); PyDict_SetItemString(ourNamespace_, "__builtins__", PyEval_GetBuiltins()); PyObject* locals = PyDict_New(); const char* scriptStr = "print '1'\n" "import sys\n" "print sys.path\n" "def debug_path_info():\n" " print 'These are the directories Python looks into for modules and source files:'\n" " print '2'\n" " for folder in sys.path:\n" " print folder\n" " print '--------------'\n" " print 'This would be your present working folder/ directory:'\n" " print '3'\n" " print sys.path[0]\n" "debug_path_info()\n"; PyObject* scriptResult = PyRun_String( scriptStr, // Python code to execute Py_file_input, ourNamespace_, // globals dictionary locals); // locals dictionary if (!scriptResult) { std::cerr << "Python error: " << "Unhandled Python exception from script." << std::endl; PyErr_Print(); } else { Py_DECREF(scriptResult); // don't need result any more } Py_DECREF(locals); Py_DECREF(ourNamespace_); Py_Finalize(); And the output is like this: 1 ['E:\\code\\Python25\\lib\\site-packages\\turbokid-1.0.4-py2.5.egg', 'E:\\code\\ Python25\\lib\\site-packages\\turbocheetah-1.0-py2.5.egg', 'E:\\code\ \Python25\\ lib\\site-packages\\simplejson-1.8.1-py2.5-win32.egg', 'E:\\code\ \Python25\\lib\ \site-packages\\ruledispatch-0.5a0.dev_r2306-py2.5-win32.egg', 'E:\ \code\\Python 25\\lib\\site-packages\\pastescript-1.6.2-py2.5.egg', 'E:\\code\ \Python25\\lib\\ site-packages\\formencode-1.0.1-py2.5.egg', 'E:\\code\\Python25\\lib\ \site-packa ges\\decoratortools-1.7-py2.5.egg', 'E:\\code\\Python25\\lib\\site- packages\\con figobj-4.5.2-py2.5.egg', 'E:\\code\\Python25\\lib\\site-packages\ \cherrypy-2.3.0 -py2.5.egg', 'E:\\code\\Python25\\lib\\site-packages\\kid-0.9.6- py2.5.egg', 'E:\ \code\\Python25\\lib\\site-packages\\cheetah-2.0.1-py2.5-win32.egg', 'E:\\code\\ Python25\\lib\\site-packages\\pyprotocols-1.0a0-py2.5-win32.egg', 'E:\ \code\\Pyt hon25\\lib\\site-packages\\pastedeploy-1.3.1-py2.5.egg', 'E:\\code\ \Python25\\li b\\site-packages\\paste-1.6-py2.5.egg', 'E:\\code\\Python25\\lib\\site- packages\ \sqlobject-0.10.0-py2.5.egg', 'E:\\code\\Python25\\lib\\site-packages\ \tgfastdat a-0.9a7-py2.5.egg', 'E:\\code\\Python25\\lib\\site-packages\ \webhelpers-0.6-py2. 5.egg', 'E:\\code\\Python25\\lib\\site-packages\\shove-0.1.3- py2.5.egg', 'E:\\co de\\Python25\\lib\\site-packages\\boto-1.3a-py2.5.egg', 'E:\\code\ \Python25\\lib \\site-packages\\sqlalchemy-0.5.0beta3-py2.5.egg', 'E:\\code\\Python25\ \lib\\sit e-packages\\turbojson-1.1.4-py2.5.egg', 'E:\\code\\Python25\\lib\\site- packages\ \setuptools-0.6c9-py2.5.egg', 'E:\\code\\Python25\\lib\\site-packages\ \turbogear s-1.0.8-py2.5.egg', 'C:\\WINDOWS\\system32\\python25_d.zip', 'E:\\code\ \Python25 \\Lib', 'E:\\code\\Python25\\DLLs', 'E:\\code\\Python25\\Lib\\lib-tk', 'e:\\Visu al Studio 2008\\Projects\\StacklessEmbed\\StacklessEmbed', 'e:\\Visual Studio 20 08\\Projects\\StacklessEmbed\\Debug', 'E:\\code\\Python25', 'E:\\code\ \Python25\ \lib\\site-packages', 'E:\\code\\Python25\\lib\\site-packages\\PIL', 'E:\\code\\ Python25\\lib\\site-packages\\wx-2.8-msw-unicode'] These are the directories Python looks into for modules and source files: 2 Python error: Unhandled Python exception from script. Traceback (most recent call last): File "<string>", line 13, in <module> File "<string>", line 7, in debug_path_info NameError: global name 'sys' is not defined [12532 refs] (Incidentally, the Stackless references are because I was originally trying to embed Stackless, but I reverted to vanilla 2.5 to see if it was a Stackless specific issue, which it appears not.) Another interesting thing is that sys.path[0] doesn't appear to be the current working directory, despite several sources online suggesting it should be. What am I doing wrong? -- Ben Sizer -- http://mail.python.org/mailman/listinfo/python-list