<HTML>
<HEAD>
<TITLE>Re: [Python.NET] bridging C# and Python</TITLE>
</HEAD>
<BODY>
<FONT FACE="Verdana, Helvetica, Arial"><SPAN STYLE='font-size:12.0px'>Python modules just need to be somewhere on the pythonpath (sys.path).<BR>
<BR>
You should probably be careful how crazy you get with python calling C# calling <BR>
back into python, etc. - its fairly easy to get into a deadlock situation that way <BR>
due to the python global interpreter lock...<BR>
<BR>
-Brian<BR>
<BR>
<BR>
On 11/27/07 2:20 PM, "Matt Mackowski" <matt.mackowski@slipg8.com> wrote:<BR>
<BR>
</SPAN></FONT><BLOCKQUOTE><FONT SIZE="4"><FONT FACE="Arial"><SPAN STYLE='font-size:13.0px'>Hi,<BR>
<BR>
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. <BR>
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. <BR>
<BR>
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.<BR>
<BR>
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 </SPAN></FONT><SPAN STYLE='font-size:13.0px'><FONT COLOR="#2B91AF"><FONT FACE="Courier New">PythonEngine</FONT></FONT><FONT FACE="Courier New">.ImportModule?<BR>
<BR>
In python my codes looks like this <BR>
<BR>
import clr <BR>
import pythoncom<BR>
clr.AddReference("ScriptEditor") <BR>
from ScriptEditor import ScriptEditor<BR>
self.app = ScriptEditor()<BR>
clr.AddReference("System.Windows.Forms")<BR>
from System.Windows.Forms import Application<BR>
Application.Run(self.app)<BR>
<BR>
ScriptEditor is my C# app. <BR>
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<BR>
<BR>
<FONT COLOR="#008000">//-------------------------------------------------------------------------------------------------<BR>
</FONT> <FONT COLOR="#0000FF">public</FONT> <FONT COLOR="#0000FF">void</FONT> usingPython()<BR>
<FONT COLOR="#008000">//-------------------------------------------------------------------------------------------------<BR>
</FONT> {<BR>
<FONT COLOR="#2B91AF">PythonEngine</FONT>.Initialize();<BR>
<FONT COLOR="#2B91AF">IntPtr</FONT> pyLock = <FONT COLOR="#2B91AF">PythonEngine</FONT>.AcquireLock();<BR>
<FONT COLOR="#2B91AF">PyObject</FONT> pyObj = <FONT COLOR="#2B91AF">PythonEngine</FONT>.ImportModule(<FONT COLOR="#A31515">"hello"</FONT>); <BR>
<BR>
pyObj.InvokeMethod(<FONT COLOR="#A31515">"helloCallback"</FONT>, <FONT COLOR="#0000FF">new</FONT> <FONT COLOR="#2B91AF">PyTuple</FONT>());<BR>
<BR>
<FONT COLOR="#2B91AF">PythonEngine</FONT>.ReleaseLock(pyLock); <BR>
<FONT COLOR="#2B91AF">PythonEngine</FONT>.Shutdown();<BR>
}<BR>
<BR>
That doesn’t work. It works when I call the function in C#.<BR>
Hello.py looks like this:<BR>
<BR>
def hello():<BR>
return 'hello'<BR>
<BR>
def helloCallback():<BR>
print "This is a Test and Of The python.net"<BR>
<BR>
Thanks<BR>
<BR>
Matt</FONT><FONT FACE="Arial"> <BR>
</FONT></SPAN></FONT><FONT FACE="Verdana, Helvetica, Arial"><SPAN STYLE='font-size:12.0px'><BR>
<HR ALIGN=CENTER SIZE="3" WIDTH="95%"></SPAN></FONT><FONT SIZE="2"><FONT FACE="Monaco, Courier New"><SPAN STYLE='font-size:10.0px'>_________________________________________________<BR>
Python.NET mailing list - PythonDotNet@python.org<BR>
<a href="http://mail.python.org/mailman/listinfo/pythondotnet">http://mail.python.org/mailman/listinfo/pythondotnet</a><BR>
</SPAN></FONT></FONT></BLOCKQUOTE><FONT SIZE="2"><FONT FACE="Monaco, Courier New"><SPAN STYLE='font-size:10.0px'><BR>
<BR>
</SPAN></FONT></FONT><FONT FACE="Verdana, Helvetica, Arial"><SPAN STYLE='font-size:12.0px'>--------------------------<BR>
Brian Lloyd<BR>
<BR>
brian.lloyd@revolutionhealth.com<BR>
</SPAN></FONT>
</BODY>
</HTML>