[IronPython] System.Scripting.Runtime.UnboundNameException

Curt Hagenlocher curt at hagenlocher.org
Sun Aug 10 10:31:46 CEST 2008


A hosting API global is not the same as a Python global.  After all, Python
globals aren't really global; they're scoped to the module you're in.  I
can't provide sample code offhand, but to do what you want, you'll need to
do something like the following steps:

1) Create an empty script scope
2) Set the "name" variable in that scope
3) Load your source into that already-created scope instead of creating a
new one.

There are probably better ways to structure this code, but it looks like
you're just working out a test program so I won't be pedantic about it.

In this sample code, you also haven't loaded System.Windows.Forms.dll; you
can do that either through Python with clr.AddReference or from the hosting
code with env.LoadAssembly.

On Sun, Aug 10, 2008 at 1:22 AM, Huzaifa <huzi_1982 at hotmail.com> wrote:

>
> i am trying this code and it is giving me exception
> "$exception     {"name 'name' is not defined"}  System.Exception
> {System.Scripting.Runtime.UnboundNameException}
> "
> CODE:
>
> ScriptRuntime env = ScriptRuntime.Create ( );
> ScriptScope defaultScope = env.CreateScope ( );
> ScriptEngine curEngine = env.GetEngine ("py");
> ScriptSource input;
>
> env.LoadAssembly (typeof (System.Diagnostics.Debug).Assembly);
> env.Globals.SetVariable("name","Huzaifa");
>
> input = curEngine.CreateScriptSourceFromString (
>                @"import clr
> from System import *
> from System.Windows.Forms import *
>
> MessageBox.Show(name)
> ", SourceCodeKind.Statements);
>
> input.Execute (defaultScope);
>
>
> --
> View this message in context:
> http://www.nabble.com/System.Scripting.Runtime.UnboundNameException-tp18911599p18911599.html
> Sent from the IronPython mailing list archive at Nabble.com.
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20080810/b7c5c552/attachment.html>


More information about the Ironpython-users mailing list