[IronPython] System.Scripting.Runtime.UnboundNameException

Seshadri Pillailokam Vijayaraghavan seshapv at microsoft.com
Sun Aug 10 19:20:15 CEST 2008


The following edit to your code snippet would give you what you want

Comment out the current SetVariables call
        //env.Globals.SetVariable("name", "Huzaifa");

//Load the Forms assembly and set the variable in your defaultScope obj.
        env.LoadAssembly(typeof(System.Windows.Forms.MessageBox).Assembly);
        defaultScope.SetVariable("name", "Huzaifa");

From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Curt Hagenlocher
Sent: Sunday, August 10, 2008 1:32 AM
To: Discussion of IronPython
Subject: Re: [IronPython] System.Scripting.Runtime.UnboundNameException

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<mailto: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<mailto: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/f3ad56ed/attachment.html>


More information about the Ironpython-users mailing list