[IronPython] Statefulness of CompiledCode

Korbinian Abenthum lists.ironpython.com at ka.weltenschmiede.com
Mon May 26 19:43:58 CEST 2008

Greg Parker wrote:
> for (int i = 0; i < 10; i++) {
>    compiledScript.Execute();
> }

AFAIK the import statement creates a new Scope for the imported module 
that lives in parallel to the ScriptScope passed to Execute (in IP2, I 
don't know about 1.1). Since import will check if the module was 
imported before, it is only imported during the first iteration and 
the class variable of SomeClass will persist during all you calls to 

It should work to "import SomeInclude; reload(SomeInclude)" at the 
beginning or end of each execution. On the C# side, if I add
before or after each call to execute, it removes the (references to 
the) imported scopes and will load them anew during the next execution,
but I do not know if this is the cleanest way to do so.

| ScriptEngine python = IronPython.Hosting.PythonEngine.CurrentEngine;
| CompiledCode compiledCode = python.CreateScriptSourceFromFile("Program.py").Compile();
| ScriptScope scope = python.CreateScope();
| for (int i = 0; i < 10; i++)
| {
|     compiledCode.Execute(scope);
|     python.Runtime.Globals.ClearVariables();
| }

More information about the Ironpython-users mailing list