[Python.NET] PyEval_GetGlobals
Mark Tigges
mtigges at gmail.com
Fri May 2 18:25:58 CEST 2008
I am trying to write a C# program that runs a python script. It,
looks something like this:
PythonEngine.Initialize();
PyList pyargv = null;
PyObject sys = PythonEngine.ImportModule("sys");
if (sys.HasAttr("argv"))
{
pyargv = sys.GetAttr(new PyString("argv")) as PyList;
}
else
{
pyargv = new PyList();
foreach (string arg in argv)
{
pyargv.Append(new PyString(arg));
}
}
sys.SetAttr("argv", pyargv);
string dir = System.IO.Path.GetDirectoryName(argv[0]);
if (!String.IsNullOrEmpty(dir))
{
PyList path = new PyList(sys.GetAttr("path"));
PyObject pydir = new PyString(dir);
if(!path.Contains(pydir))
{
path.Append(pydir);
}
}
PyObject module =
PythonEngine.ImportModule(System.IO.Path.GetFileNameWithoutExtension(argv[0]));
if(module!=null) module.Dispose();
PythonEngine.Shutdown();
So far, so good. At this point you may ask why I'm bothering and not
just using python.exe. Just trust me, that it's a bit specialized,
and I need to do a few other things. Namely, make some changes to the
global namespace. So, I added a function to PythonEngine to return
new PyDict(Runtime.PyEval_GetGlobals());
So that I can start adding some objects to it that the script can make use of.
Unfortunately, PyEval_GetGlobals returns 0. It does so in
PythonEngine.RunString as well. This seems kind of strange to me.
What am I doing wrong?
Thanks,
Mark.
More information about the PythonDotNet
mailing list