<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
</head>
<body>
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
p.m-1433265528472523567xm4818856398437586376xmsonormal, li.m-1433265528472523567xm4818856398437586376xmsonormal, div.m-1433265528472523567xm4818856398437586376xmsonormal
        {mso-style-name:m_-1433265528472523567x_m_4818856398437586376x_msonormal;
        mso-margin-top-alt:auto;
        margin-right:0in;
        mso-margin-bottom-alt:auto;
        margin-left:0in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style>
<div class="WordSection1">
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I too am working (slowly) on adding pythonnet as scripting environment for a .NET application.  When investigating this I wrote a VS project that tested all the assumptions I was making about how the interface worked.  That is available
 on GitHub at:  https://github.com/tomunger/pythonnet.embedingtest</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Below is the method I use to load a module.  Note that I don’t
<u>import</u> the module but <u>execute</u> it in the PyScope.  </p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Tom</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">                                private PyScope LoadModule(string name, string fileName, Dictionary<string, object> globals)</p>
<p class="MsoNormal">                                {</p>
<p class="MsoNormal">                                                PyScope scope = null;</p>
<p class="MsoNormal">                                                using (Py.GIL())</p>
<p class="MsoNormal">                                                {</p>
<p class="MsoNormal">                                                                // Create a new scope</p>
<p class="MsoNormal">                                                                scope = Py.CreateScope(name);</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">                                                                // Assign any globals.</p>
<p class="MsoNormal">                                                                if (globals != null)</p>
<p class="MsoNormal">                                                                {</p>
<p class="MsoNormal">                                                                                foreach (string gname in globals.Keys)</p>
<p class="MsoNormal">                                                                                                scope.Set(gname, globals[gname]);</p>
<p class="MsoNormal">                                                                }</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">                                                                // Load python code, compile, then execute it in the scope.</p>
<p class="MsoNormal">                                                                string scriptText = File.ReadAllText(fileName);</p>
<p class="MsoNormal">                                                                PyObject code = PythonEngine.Compile(scriptText, fileName);</p>
<p class="MsoNormal">                                                                dynamic r = scope.Execute(code);</p>
<p class="MsoNormal">                                                }</p>
<p class="MsoNormal">                                                return scope;</p>
<p class="MsoNormal">                                }</p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> PythonDotNet <pythondotnet-bounces+tunger=mitem.com@python.org> on behalf of Mohamed Koubaa <mohamed.koubaa@ansys.com><br>
<b>Sent:</b> Monday, September 24, 2018 7:03:52 AM<br>
<b>To:</b> pythondotnet@python.org<br>
<b>Cc:</b> hbtmdxywg@126.com<br>
<b>Subject:</b> Re: [Python.NET] Using PyScope to set variables I can use in imported scripts</font>
<div> </div>
</div>
<div>
<div dir="ltr">Steve,
<div><br>
</div>
<div>My organization is interested in migrating from IronPython to PythonNet so I'll add my two cents.  We do this:</div>
<div><br>
</div>
<div>1.  (during startup) Run a short python file which imports some .NET namespaces into a scope</div>
<div>2.  (during startup) Add some variables to that scope (globals which give scripts access to our data model)</div>
<div>3.  (at arbitrary times later) Run python code in the above scope so that there isn't a need to import the namespaces or discover the data model</div>
<div><br>
</div>
<div>Thanks,</div>
<div>Mohamed Koubaa</div>
<div>ANSYS Inc </div>
</div>
<br>
<div class="gmail_quote">
<div dir="ltr">On Mon, Sep 24, 2018 at 8:59 AM Luke, Steve <<a href="mailto:Steve.Luke@moldev.com">Steve.Luke@moldev.com</a>> wrote:<br>
</div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">
<div id="m_-1433265528472523567divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif" dir="ltr">
<p style="margin-top:0;margin-bottom:0">Yes, I did.  All the unit tests for PyScope use the Eval(), Exec(), or Execute() functions to get code to execute in the PyScope.  They don't use Import the way I had been (hoping to do).</p>
<p style="margin-top:0;margin-bottom:0"><br>
</p>
<p style="margin-top:0;margin-bottom:0">I am probably using it differently than intended/expected.  I am importing a script into the scope and expect to be able to inject variables into the imported module.  <span style="font-size:12pt">That doesn't seem to
 be what PyScope is used for.  Instead, PyScope seems to be a container for the code and variables like the __main__ scope that gets created when running Python from the command prompt.  It looks like when you Import it creates a new nested scope which doesn't
 have access to the scope created by PyScope</span><span style="font-size:12pt">.  I had expected it to work as PyScope was the scope in which the Imported code would be executed, not the parent scope.  I think the intended way to use PyScope and access the
 variables it holds is to pass the code in as Strings using Exec().</span></p>
<p style="margin-top:0;margin-bottom:0"><br>
</p>
<p style="margin-top:0;margin-bottom:0">I think the right thing to do is:</p>
<p style="margin-top:0;margin-bottom:0"></p>
<p style="font-family:Calibri,Helvetica,sans-serif,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">
1) Set variables in PyScope</p>
<div><span style="font-size:12pt">2) Add the path to the script to the sys.path</span><br>
</div>
<p></p>
<p style="margin-top:0;margin-bottom:0"><span style="font-size:12pt">3) Read the contents of the script file into a string</span><br>
</p>
<p style="margin-top:0;margin-bottom:0">4) Use the PyScope to Exec() Execute() or Eval() as appropriate</p>
<p style="margin-top:0;margin-bottom:0"><br>
</p>
<p style="margin-top:0;margin-bottom:0">This works it is just not the same as what I was used to with IronPython (which is fine if that is really how it is intended to be used).</p>
<p style="margin-top:0;margin-bottom:0"><br>
</p>
<p style="margin-top:0;margin-bottom:0">Steve</p>
</div>
<hr style="display:inline-block;width:98%">
<div id="m_-1433265528472523567divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Denis Akhiyarov <<a href="mailto:denis.akhiyarov@gmail.com" target="_blank">denis.akhiyarov@gmail.com</a>><br>
<b>Sent:</b> Monday, September 24, 2018 9:36:19 AM<br>
<b>To:</b> Luke, Steve; A list for users and developers of Python for .NET<br>
<b>Cc:</b> ywg<br>
<b>Subject:</b> Re: [Python.NET] Using PyScope to set variables I can use in imported scripts</font>
<div> </div>
</div>
<div>
<div dir="ltr">
<div dir="ltr">I copied Wenguang Yang (@yagweb), who developed PyScope.</div>
<div dir="ltr"><br>
</div>
<div>Steve, did you look at the unit tests for PyScope?</div>
<div><br>
</div>
<div>Thanks,</div>
<div>Denis</div>
</div>
<br>
<div class="m_-1433265528472523567x_gmail_quote">
<div dir="ltr">On Mon, Sep 24, 2018 at 8:28 AM Luke, Steve <<a href="mailto:Steve.Luke@moldev.com" target="_blank">Steve.Luke@moldev.com</a>> wrote:<br>
</div>
<blockquote class="m_-1433265528472523567x_gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">
<div id="m_-1433265528472523567x_m_4818856398437586376divtagdefaultwrapper" dir="ltr" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols">
<p style="margin-top:0;margin-bottom:0">I am new to Python.Net and trying to convert an application that used IronPython to embed a Python environment into a .Net application and allow the user to run Python scripts inside our application.  The part that embeds
 the Python environment is VB.Net (but could be C# if that would be better).</p>
<p style="margin-top:0;margin-bottom:0"><br>
</p>
<p style="margin-top:0;margin-bottom:0">I am running into problems setting variables in the scope such that they can be used in the Python scripts we call.  I keep getting an error: NameError: global name 'Bridge' is not defined.</p>
<p style="margin-top:0;margin-bottom:0"><br>
</p>
<p style="margin-top:0;margin-bottom:0">This is the VB code I am trying to use:</p>
<p style="margin-top:0;margin-bottom:0"></p>
<div><span style="font-size:12pt"><span style="font-family:"Courier New",monospace;font-size:16px">        </span><span style="font-family:"Courier New",monospace">PythonEngine.Initialize()</span></span><br>
</div>
<div><br>
</div>
<div><span style="font-family:"Courier New",monospace">        Using (Py.GIL())</span></div>
<div><span style="font-family:"Courier New",monospace">            Dim moduleName As String = "PrintToBridge"</span></div>
<div><span style="font-family:"Courier New",monospace">            Dim pythonCommandLine As String = "Not used yet"</span></div>
<div><br>
</div>
<div><span style="font-family:"Courier New",monospace">            Dim scope As PyScope = Py.CreateScope()</span></div>
<div><span style="font-family:"Courier New",monospace">            Try</span></div>
<div><span style="font-family:"Courier New",monospace">                <span>scope.Set("Bridge", bridge)</span></span></div>
<div><span style="font-family:"Courier New",monospace">                scope.Set("SetupParams", cmdLine</span><span style="font-family:"Courier New",monospace">)</span></div>
<div><br>
</div>
<div><span style="font-size:12pt;font-family:"Courier New",monospace">                Dim script As Object = scope.Import(moduleName)</span><br>
</div>
<div><span style="font-family:"Courier New",monospace">                bridge.mm.PrintMsg("Scope has Bridge:" + Str(scope.Contains("Bridge")))</span></div>
<div><span style="font-size:12pt;font-family:"Courier New",monospace">                script.Startup(pythonCommandLine)</span><br>
</div>
<div><span style="font-family:"Courier New",monospace">            Finally</span></div>
<div><span style="font-family:"Courier New",monospace">                scope.Dispose()</span></div>
<div><span style="font-family:"Courier New",monospace">            End Try</span></div>
<div><span style="font-family:"Courier New",monospace">        End Using</span></div>
<div><br>
</div>
The Python script:
<p></p>
<p style="margin-top:0;margin-bottom:0"></p>
<div><span style="font-family:"Courier New",monospace">def Startup(param):</span></div>
<div><span style="font-family:"Courier New",monospace">    Bridge.mm.PrintMsg('Startup called')</span></div>
<div><br>
</div>
<div><span style="font-family:"Courier New",monospace">def Docommand(param):</span></div>
<div><span style="font-family:"Courier New",monospace">    Bridge.mm.PrintMsg('docommand called')</span></div>
<div><span style="font-family:"Courier New",monospace">    </span></div>
<div><span style="font-family:"Courier New",monospace">def Shutdown():</span></div>
<div><span style="font-family:"Courier New",monospace">    Bridge.mm.PrintMsg('Shutdown called')</span></div>
<div><br>
</div>
<br>
<p></p>
<p style="margin-top:0;margin-bottom:0">How can I set a variable such that it would be recognized by the called Python script?  Importing the script works, I can force python-y things to happen.  And the check to see if the object I want is in scope returns
 true.  But it still isn't available in Python.</p>
<p style="margin-top:0;margin-bottom:0"><br>
</p>
<p style="margin-top:0;margin-bottom:0">I have tried <span><span style="font-family:"Courier New",monospace">scope.Eval(moduleName + ".Startup('" + pythonCommandLine + "')")</span> and Exec() with locals:</span></p>
<p style="margin-top:0;margin-bottom:0"><span></span></p>
<div><span style="font-family:Calibri,Helvetica,sans-serif,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;font-size:16px">                </span>Dim locals As PyDict = New PyDict()</div>
<div>                locals.SetItem("Bridge".ToPython(), bridge.ToPython())</div>
<div>                scope.Exec(moduleName + ".Startup('" + pythonCommandLine + "')", locals)</div>
<br>
<p></p>
<p style="margin-top:0;margin-bottom:0"><span>All with the same effect.  Any ideas on how to get set variables so scripts can gain access to them?</span></p>
<p style="margin-top:0;margin-bottom:0"><br>
</p>
<p style="margin-top:0;margin-bottom:0">Thanks,</p>
<div id="m_-1433265528472523567x_m_4818856398437586376Signature">
<div id="m_-1433265528472523567x_m_4818856398437586376divtagdefaultwrapper" dir="ltr" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols">
<p style="margin-top:0;margin-bottom:0"></p>
<p class="m_-1433265528472523567x_m_4818856398437586376x_MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif;color:rgb(33,33,33)">
<span style="font-size:10pt;font-family:Arial,sans-serif,serif,EmojiFont">Steve</span></p>
<p class="m_-1433265528472523567x_m_4818856398437586376x_MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif;color:rgb(33,33,33)">
<br>
</p>
<p></p>
</div>
</div>
</div>
Please be advised that this email may contain confidential information. If you are not the intended recipient, please notify us by email by replying to the sender and delete this message. The sender disclaims that the content of this email constitutes an offer
 to enter into, or the acceptance of, any agreement; provided that the foregoing does not invalidate the binding effect of any digital or other electronic reproduction of a manual signature that is included in any attachment.
</div>
_________________________________________________<br>
Python.NET mailing list - <a href="mailto:PythonDotNet@python.org" target="_blank">
PythonDotNet@python.org</a><br>
<a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__mail.python.org_mailman_listinfo_pythondotnet&d=DwMFaQ&c=9mghv0deYPYDGP-W745IEdQLV1kHpn4XJRvR6xMRXtA&r=XBqO7p7APNCVKpdDR1v4BCUJqDo0CwXYsXZCFrPPxqg&m=9mSyhuL5_TpZgYqhGupLEDwQ8mpZrrmrs3zDjcmbhHk&s=vkTiivd8fJevlpD6Kn7hi5R5vSunthrCXQwRwuD3xYo&e=" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/pythondotnet</a><br>
</blockquote>
</div>
</div>
Please be advised that this email may contain confidential information. If you are not the intended recipient, please notify us by email by replying to the sender and delete this message. The sender disclaims that the content of this email constitutes an offer
 to enter into, or the acceptance of, any agreement; provided that the foregoing does not invalidate the binding effect of any digital or other electronic reproduction of a manual signature that is included in any attachment.
</div>
_________________________________________________<br>
Python.NET mailing list - <a href="mailto:PythonDotNet@python.org" target="_blank">
PythonDotNet@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/pythondotnet" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/pythondotnet</a><br>
</blockquote>
</div>
</div>
</body>
</html>