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 …
[View More]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.
[View Less]
Hello,
I am using the 2.3 release branch.
I have C# code like this:
public class A {
public A() {...}
}
public class B {
public B() {...}
public static void Func(IEnumerable<A> input){
}
}
I want to pass in a python list to Func but get this error:
>>> b=B()
>>> b.Func([A(), A()])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: No method matches given arguments
I tried …
[View More]both IEnumerable and IList in the signature of 'Func'. I know that
I can import System and create some .NET collection type and it works well.
Did I miss anything? Is this usage pattern within the design goals of
pythonnet? I assume this would have to be fixed in MethodBinder but I
wanted to try asking here first.
Thanks!
Mohamed Koubaa
[View Less]