[Python.NET] Could you tell me how to call python from c#?
Karpenstein, Nissim (C-BASS)
Nissim.Karpenstein at C-BASS.COM
Mon Jun 4 16:08:29 CEST 2007
If you want to execute a python program from c-sharp you can just use
System.Diagnostics.Process to execute python.exe and you don't need
python for .net.
Python for .net is more appropriate for if you want to execute a python
function or create an instance of a python object and then run methods
and access attributes from your C# program.
The example you linked to is OK, except that he modified the
Python.Runtime.Dll which is not necessary.
If you had a Hello.py with a function called hello
Like this:
def hello():
return 'hello'
You could do this in C#:
PythonEngine.Initialize();
PyObject helloModule = PythonEngine.ImportModule("Hello");
String fromPython = helloModule.InvokeMethod("hello", new
PyTuple()).ToString();
PythonEngine.Shutdown();
// now fromPython = "hello"
If your function takes an argument:
def returnArg(arg):
return arg
PythonEngine.Initialize();
PyObject helloModule = PythonEngine.ImportModule("Hello");
String fromPython = helloModule.InvokeMethod("returnArg", new
PyObject[1] {new PyString("Test")}).ToString();
PythonEngine.Shutdown();
// now fromPython = "test"
> -----Original Message-----
> From: pythondotnet-bounces at python.org
> [mailto:pythondotnet-bounces at python.org] On Behalf Of SeungWeon Park
> Sent: Monday, June 04, 2007 4:22 AM
> To: pythondotnet at python.org
> Subject: [Python.NET] Could you tell me how to call python from c#?
>
> Hello, PythonNet!
>
> I'm looking for some sample code to call python from C#.
> Actually, I'd like to execute python program(let say
> 'Hello.py') from C# with parameters.
> Actually, I'm a newbie at both of languages. So, could you
> give sample source code? I was looking for some guide or
> source for several days, but I couldn't find it. What I've
> got so far from googling is
> http://mail.python.org/pipermail/pythondotnet/2003-November/00
> 0037.html,
> but I can't understand it and old one.
>
> Thank you in advance.
> Seungweon.
> _________________________________________________
> Python.NET mailing list - PythonDotNet at python.org
> http://mail.python.org/mailman/listinfo/pythondotnet
>
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
This e-mail (including any attachments) may contain
information that is private, confidential, or
protected by attorney-client or other privilege. If
you received this e-mail in error, please delete it
from your system without copying it and notify sender
by reply e-mail, so that our records can be corrected.
More information about the PythonDotNet
mailing list