<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, &quot;Matt Mackowski&quot; &lt;matt.mackowski@slipg8.com&gt; wrote:<BR>
<BR>
</SPAN></FONT><BLOCKQUOTE><FONT SIZE="4"><FONT FACE="Arial"><SPAN STYLE='font-size:13.0px'>Hi,<BR>
&nbsp;<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. &nbsp;<BR>
&nbsp;<BR>
What I am having trouble with is calling python functions from the C# when the C# app was started by a python call. &nbsp;When I run the C# alone I can call the python functions.<BR>
&nbsp;<BR>
Part of my problem I think is that the C# app doesn&#8217;t know where to fine the python modules. &nbsp;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>
&nbsp;<BR>
In python my codes looks like this <BR>
&nbsp;<BR>
import clr &nbsp;<BR>
import pythoncom<BR>
clr.AddReference(&quot;ScriptEditor&quot;) &nbsp;&nbsp;&nbsp;<BR>
from ScriptEditor import ScriptEditor<BR>
self.app = ScriptEditor()<BR>
clr.AddReference(&quot;System.Windows.Forms&quot;)<BR>
from System.Windows.Forms import Application<BR>
Application.Run(self.app)<BR>
&nbsp;<BR>
ScriptEditor is my C# app. <BR>
I can call most of the C# functions from python with the above code. &nbsp;The C# functions I cannot call are ones that use python for example<BR>
&nbsp;<BR>
<FONT COLOR="#008000">//-------------------------------------------------------------------------------------------------<BR>
</FONT> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT COLOR="#0000FF">public</FONT> <FONT COLOR="#0000FF">void</FONT> usingPython()<BR>
<FONT COLOR="#008000">//-------------------------------------------------------------------------------------------------<BR>
</FONT> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT COLOR="#2B91AF">PythonEngine</FONT>.Initialize();<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT COLOR="#2B91AF">IntPtr</FONT> pyLock = <FONT COLOR="#2B91AF">PythonEngine</FONT>.AcquireLock();<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT COLOR="#2B91AF">PyObject</FONT> pyObj = &nbsp;<FONT COLOR="#2B91AF">PythonEngine</FONT>.ImportModule(<FONT COLOR="#A31515">&quot;hello&quot;</FONT>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pyObj.InvokeMethod(<FONT COLOR="#A31515">&quot;helloCallback&quot;</FONT>, <FONT COLOR="#0000FF">new</FONT> <FONT COLOR="#2B91AF">PyTuple</FONT>());<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT COLOR="#2B91AF">PythonEngine</FONT>.ReleaseLock(pyLock); <BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT COLOR="#2B91AF">PythonEngine</FONT>.Shutdown();<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>
&nbsp;<BR>
That doesn&#8217;t work. It works when I call the function in C#.<BR>
Hello.py looks like this:<BR>
&nbsp;<BR>
def hello():<BR>
&nbsp;&nbsp;&nbsp;&nbsp;return 'hello'<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
def helloCallback():<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;This is a Test and Of The python.net&quot;<BR>
&nbsp;<BR>
Thanks<BR>
&nbsp;<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>