Hi, I would like to run a python application have it call a C# application and have the two applications be able to call functions in the other application. I have gotten the python to open and run the C# application and I am able to run any function from the C# app in python. What I am having trouble with is calling python functions from the C# when the C# app was started by a python call. When I run the C# alone I can call the python functions. Part of my problem I think is that the C# app doesn't know where to fine the python modules. Where do they need to be if I want to use the PythonEngine.ImportModule? In python my codes looks like this import clr import pythoncom clr.AddReference("ScriptEditor") from ScriptEditor import ScriptEditor self.app = ScriptEditor() clr.AddReference("System.Windows.Forms") from System.Windows.Forms import Application Application.Run(self.app) ScriptEditor is my C# app. I can call most of the C# functions from python with the above code. The C# functions I cannot call are ones that use python for example //---------------------------------------------------------------------- --------------------------- public void usingPython() //---------------------------------------------------------------------- --------------------------- { PythonEngine.Initialize(); IntPtr pyLock = PythonEngine.AcquireLock(); PyObject pyObj = PythonEngine.ImportModule("hello"); pyObj.InvokeMethod("helloCallback", new PyTuple()); PythonEngine.ReleaseLock(pyLock); PythonEngine.Shutdown(); } That doesn't work. It works when I call the function in C#. Hello.py looks like this: def hello(): return 'hello' def helloCallback(): print "This is a Test and Of The python.net" Thanks Matt
Python modules just need to be somewhere on the pythonpath (sys.path).
You should probably be careful how crazy you get with python calling C#
calling
back into python, etc. - its fairly easy to get into a deadlock situation
that way
due to the python global interpreter lock...
-Brian
On 11/27/07 2:20 PM, "Matt Mackowski"
Hi,
I would like to run a python application have it call a C# application and have the two applications be able to call functions in the other application. I have gotten the python to open and run the C# application and I am able to run any function from the C# app in python.
What I am having trouble with is calling python functions from the C# when the C# app was started by a python call. When I run the C# alone I can call the python functions.
Part of my problem I think is that the C# app doesn¹t know where to fine the python modules. Where do they need to be if I want to use the PythonEngine.ImportModule?
In python my codes looks like this
import clr import pythoncom clr.AddReference("ScriptEditor") from ScriptEditor import ScriptEditor self.app = ScriptEditor() clr.AddReference("System.Windows.Forms") from System.Windows.Forms import Application Application.Run(self.app)
ScriptEditor is my C# app. I can call most of the C# functions from python with the above code. The C# functions I cannot call are ones that use python for example
//---------------------------------------------------------------------------- --------------------- public void usingPython() //---------------------------------------------------------------------------- --------------------- { PythonEngine.Initialize(); IntPtr pyLock = PythonEngine.AcquireLock(); PyObject pyObj = PythonEngine.ImportModule("hello");
pyObj.InvokeMethod("helloCallback", new PyTuple());
PythonEngine.ReleaseLock(pyLock); PythonEngine.Shutdown(); }
That doesn¹t work. It works when I call the function in C#. Hello.py looks like this:
def hello(): return 'hello'
def helloCallback(): print "This is a Test and Of The python.net"
Thanks
Matt
_________________________________________________ Python.NET mailing list - PythonDotNet@python.org http://mail.python.org/mailman/listinfo/pythondotnet
-------------------------- Brian Lloyd brian.lloyd@revolutionhealth.com
Have you tried using callbacks through delegates instead? In practice I haven't had any problems doing stuff like that. For example the following works fine:
def callback(sender, args):
print 'You clicked on', sender.Text
btn = Button()
btn.Text = 'Click me!'
btn.Click += callback
Doing it this way means the C# code doesn't need to worry about how to find any Python modules.
- Feihong
Matt Mackowski
participants (3)
-
Brian Lloyd
-
Feihong Hsu
-
Matt Mackowski