<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 14 (filtered medium)"><title>Message</title><style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
        {mso-style-priority:99;
        mso-style-link:"Balloon Text Char";
        margin:0in;
        margin-bottom:.0001pt;
        font-size:8.0pt;
        font-family:"Tahoma","sans-serif";}
span.EmailStyle17
        {mso-style-type:personal;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
span.BalloonTextChar
        {mso-style-name:"Balloon Text Char";
        mso-style-priority:99;
        mso-style-link:"Balloon Text";
        font-family:"Tahoma","sans-serif";}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>To pre-load various module imports into our execution environment, I am doing something like this at application startup (we have a number of modules that we pre-load):<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:Consolas'>&nbsp;&nbsp;&nbsp; dynamic builtin = Engine.GetBuiltinModule();<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:Consolas'>&nbsp;&nbsp;&nbsp; dynamic import = builtin.__import__;<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:Consolas'>&nbsp;&nbsp;&nbsp; var DefaultImports = new string[] { &#8220;math&#8221;, &#8220;clr&#8221;, &#8220;sys&#8221;, &#8220;string&#8221;, &#8220;cmath&#8221;, &#8220;re&#8221; /*etc&#8230;*/ };<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:Consolas'>&nbsp;&nbsp;&nbsp; CachedModules = new Dictionary&lt;string, PythonModule&gt;();<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:Consolas'>&nbsp;&nbsp;&nbsp; foreach (var mod in DefaultImports)<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:Consolas'>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;CachedModules.Add(mod, import(mod));<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>And then later on, just before executing a new script each time, I create a new empty scope using Engine.CreateScope() and then I inject all of the cached modules that were previously imported:<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:Consolas'>&nbsp;&nbsp;&nbsp; foreach (var mod in CachedModules)<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:Consolas'>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;LocalScope.SetVariable(mod.Key, mod.Value);<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:Consolas'>&nbsp;&nbsp;&nbsp; PreCompiled.Execute(LocalScope);<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>This allows each script invocation to reside in its own somewhat isolated sandbox, while only importing those commonly used modules once.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>You should be able to do something similar to the above, using the builtin __import__() function. From looking over the IronPython source, it looks like you should be able to call it like this (I haven&#8217;t tested it in this way, but I believe it should work):<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:Consolas'>&nbsp;&nbsp;&nbsp; dynamic AppModule = import(&#8220;App&#8221;, null, null, &#8220;AppFrame&#8221;);<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>This should effectively mimic exactly what happens when you run the statement &#8220;from AppFrame import App&#8221;.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Many Bothan spies have died in bringing you this information.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p>&nbsp;</o:p></span></p><div><p class=MsoNormal><b><span style='font-size:14.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Keith Rome<o:p></o:p></span></b></p><p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Senior Consultant and Architect<o:p></o:p></span></b></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>MCPD-EAD, MCSD, MCDBA, MCTS-WPF, MCTS-TFS, MCTS-WSS<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Wintellect | 770.617.4016 | <a href="mailto:rome@wintellect.com">krome@wintellect.com</a><o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><a href="http://www.wintellect.com/">www.wintellect.com</a></span><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p></o:p></span></p></div><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p>&nbsp;</o:p></span></p><div><div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> users-bounces@lists.ironpython.com [mailto:users-bounces@lists.ironpython.com] <b>On Behalf Of </b>Dino Viehland<br><b>Sent:</b> Monday, June 13, 2011 1:22 PM<br><b>To:</b> Discussion of IronPython<br><b>Subject:</b> Re: [IronPython] How to place static class in script name space<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>We specifically allow from staticclass import * because we treat a static class as if it were like a module.&nbsp; So you could simply run that line of code in the scope on behalf of the user.&nbsp; Otherwise you&#8217;ll need to re-create getting the dynamic members and then setting them in the scope.&nbsp; Then you can use PythonOps.GetAttrNames to get the defined names and finally PythonOps.GetBoundAttr to get the actual members which you&#8217;ll then set in the scope.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p>&nbsp;</o:p></span></p><div style='border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt'><div><div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> users-bounces@lists.ironpython.com [mailto:users-bounces@lists.ironpython.com] <b>On Behalf Of </b>Tom Unger<br><b>Sent:</b> Monday, June 13, 2011 10:16 AM<br><b>To:</b> 'users@lists.ironpython.com'<br><b>Subject:</b> [IronPython] How to place static class in script name space<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p>&nbsp;</o:p></p><div><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>I am using IronPython for scripting an application.&nbsp; To make writing scripts easier I want to place several objects in the script name space so they can be easily used.&nbsp; I got stuck with how to place a reference to a static object, &quot;App&quot;, in the name space.&nbsp; This is probably a simple operation and just don't know what it is.</span><o:p></o:p></p></div><div><p class=MsoNormal>&nbsp;<o:p></o:p></p></div><div><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>1. Python import works:</span><o:p></o:p></p></div><div><p class=MsoNormal>&nbsp;<o:p></o:p></p></div><div><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from AppFrame import App</span><o:p></o:p></p></div><div><p class=MsoNormal>&nbsp;<o:p></o:p></p></div><div><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>gives the script access to the static fields and methods on App.</span><o:p></o:p></p></div><div><p class=MsoNormal>&nbsp;<o:p></o:p></p></div><div><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>But I want to eliminate the need for each script to do that import by setting up the scope with:</span><o:p></o:p></p></div><div><p class=MsoNormal>&nbsp;<o:p></o:p></p></div><div><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>&nbsp;&nbsp; scriptScope.SetVariable(<span style='color:#A31515'>&quot;App&quot;</span>, App);</span><o:p></o:p></p></div><div><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>This does not compile because App is a static class, not an object.&nbsp; </span><o:p></o:p></p></div><div><p class=MsoNormal>&nbsp;<o:p></o:p></p></div><div><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>How does my hosing application place a reference to the static class in the script's scope such that it can be used as if it was imported?</span><o:p></o:p></p></div><div><p class=MsoNormal>&nbsp;<o:p></o:p></p></div><div><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>Thanks</span><o:p></o:p></p></div><div><p class=MsoNormal>&nbsp;<o:p></o:p></p></div><div><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>Tom</span><o:p></o:p></p></div><div><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>Seattle</span><o:p></o:p></p></div><div><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>&nbsp;<o:p></o:p></span></p></div></div></div></body></html>