[Python.NET] bridging C# and Python

Matt Mackowski matt.mackowski at slipg8.com
Tue Nov 27 20:20:55 CET 2007


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 

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


More information about the PythonDotNet mailing list