[Python.NET] bridging C# and Python

Brian Lloyd brian.lloyd at revolutionhealth.com
Wed Nov 28 17:02:18 CET 2007


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" <matt.mackowski at slipg8.com> wrote:

> 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 at python.org
> http://mail.python.org/mailman/listinfo/pythondotnet


--------------------------
Brian Lloyd

brian.lloyd at revolutionhealth.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/pythondotnet/attachments/20071128/d9cc3ae6/attachment.htm 


More information about the PythonDotNet mailing list