[IronPython] How to set __name__ for scriptengine instance?

Dino Viehland dinov at microsoft.com
Sat Jul 18 00:12:19 CEST 2009


Michael wrote:
>
> Jesse Wiles wrote:
> > Hi,
> > I looked in the archives a bit but nothing jumped out.  I'm trying to
> > figure out how to set the __name__ for an embedded engine instance.
> > Here's what I'm working with...
> >
>
> __name__ should be set per scope. How about:
>
>     scope.SetVariable("__name__", "__main__");
>

The one gotcha here is that for a SourceCodeKind of File we will set
__name__ in the generated code.  Instead creating the code like:

var code = Engine.CreateScriptSourceFromFile(filename,
                                  Encoding.Default,
                                  SourceCodeKind.Statements);

code.Execute(scope);

should result in __name__ not being overwritten.



More information about the Ironpython-users mailing list