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).


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.  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.  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().


I think the right thing to do is:

1) Set variables in PyScope

2) Add the path to the script to the sys.path

3) Read the contents of the script file into a string

4) Use the PyScope to Exec() Execute() or Eval() as appropriate


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).


Steve


From: Denis Akhiyarov <denis.akhiyarov@gmail.com>
Sent: Monday, September 24, 2018 9:36:19 AM
To: Luke, Steve; A list for users and developers of Python for .NET
Cc: ywg
Subject: Re: [Python.NET] Using PyScope to set variables I can use in imported scripts
 
I copied Wenguang Yang (@yagweb), who developed PyScope.

Steve, did you look at the unit tests for PyScope?

Thanks,
Denis

On Mon, Sep 24, 2018 at 8:28 AM Luke, Steve <Steve.Luke@moldev.com> wrote:

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).


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.


This is the VB code I am trying to use:

        PythonEngine.Initialize()

        Using (Py.GIL())
            Dim moduleName As String = "PrintToBridge"
            Dim pythonCommandLine As String = "Not used yet"

            Dim scope As PyScope = Py.CreateScope()
            Try
                scope.Set("Bridge", bridge)
                scope.Set("SetupParams", cmdLine)

                Dim script As Object = scope.Import(moduleName)
                bridge.mm.PrintMsg("Scope has Bridge:" + Str(scope.Contains("Bridge")))
                script.Startup(pythonCommandLine)
            Finally
                scope.Dispose()
            End Try
        End Using

The Python script:

def Startup(param):
    Bridge.mm.PrintMsg('Startup called')

def Docommand(param):
    Bridge.mm.PrintMsg('docommand called')
    
def Shutdown():
    Bridge.mm.PrintMsg('Shutdown called')


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.


I have tried scope.Eval(moduleName + ".Startup('" + pythonCommandLine + "')") and Exec() with locals:

                Dim locals As PyDict = New PyDict()
                locals.SetItem("Bridge".ToPython(), bridge.ToPython())
                scope.Exec(moduleName + ".Startup('" + pythonCommandLine + "')", locals)

All with the same effect.  Any ideas on how to get set variables so scripts can gain access to them?


Thanks,

Steve


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.
_________________________________________________
Python.NET mailing list - PythonDotNet@python.org
https://mail.python.org/mailman/listinfo/pythondotnet
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.