[IronPython] Embedding Python question

Boris Capitanu borice23 at yahoo.com
Mon Nov 28 07:41:13 CET 2005

Hello again.

This questions is regarding debugging a program
without restarting by refreshing the in-memory script
every time it has changed. 

Given the following scenario:

PythonEngine engine = new PythonEngine();

void DoSomething(object arg0, object arg1)

   Function f = engine.Evaluate("SomeScript.foo") as
   f.Call(arg0, arg1);

Now suppose that there is a form with a button on the
screen, and the button's click event is wired to call
"DoSomething".  Now, DoSomething makes a call to a
function "foo" in SomeScript.py.  Everything works
fine, i.e. when I run the application, function "foo"
is invoked with the correct arguments.  However, I'd
like to be able to change what foo does in the script,
without having to restart the application. That is,
when the button is clicked,
'engine.Import("SomeScript")' should check that
SomeScript.py has changed (the file modification time
is different than last time it was loaded), and reload
that script so that the updated 'foo' function is

Currently the only way I can achieve the above
behaviour is using engine.ExecuteFile(...) instead of
engine.Import(...), but the problem with that is that
functions I can no longer call "SomeScript.foo()", and
I have to call "foo()" directly. However, if I have
multiple "foo" defined in different script files, they
overwrite eachother.

To solve this problem, I added a new function called
ExecuteFileInNewModule which is basically identical
with the RunFileInNewModule function, with the only
addition being a line like
"topFrame.SetGlobal(moduleName, mod)".  Now this seems
to do the trick, however every time the execution gets
slower and slower, as the number of modules generated
increases, so it's not really a viable solution.

Any ideas?

Thank you,

Boris Capitanu

More information about the Ironpython-users mailing list