[Python.NET] Could you tell me how to call python from c#?

Barrett, Joey Joey.Barrett at itg.com
Wed Jun 6 18:21:42 CEST 2007


Thank you for your response, this is helpful.

The methods called usually return lists or dictionaries and sometimes
other types too that need to be used within the C#.  In the example you
provided the return value from InvokeMethod(), is a string "fromPython",
but I noticed InvokeMethod() actually returns a PyObject.

How can I look at that PyObject and extract the returned values?

The classes I use import many modules from a number of locations on disk
that aren't known to the interpreter.

Is there a way to add to the PYTHONPATH programmatically in C# using the
Python for .net library, or would it be required to write a wrapper
python module that adds to the path first?  Maybe there is another way
to handle this?

Thank you again

Joey J. Barrett
Investment Technology Group, Inc.

-----Original Message-----
From: Karpenstein, Nissim (C-BASS)
[mailto:Nissim.Karpenstein at C-BASS.COM] 
Sent: Wednesday, June 06, 2007 6:37 AM
To: Barrett, Joey; pythondotnet at python.org
Subject: RE: [Python.NET] Could you tell me how to call python from c#?

I tried to post this to the list yesterday but it bounced because of
address issues...hopefully this will work:
 
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"

________________________________

	From: pythondotnet-bounces at python.org
[mailto:pythondotnet-bounces at python.org] On Behalf Of Barrett, Joey
	Sent: Tuesday, June 05, 2007 8:11 PM
	To: pythondotnet at python.org
	Subject: Re: [Python.NET] Could you tell me how to call python
from c#?
	
	

	Hi,

	 

	I also would be very interested in seeing some examples to "call
python from c#" using "Python for .NET".  I have dug around Google and a
number of forums---I can't find anything except the post from below.  I
really hope I'm not missing the obvious somewhere.

	 

	IronPython is great but my needs require using a few c++
extensions compiled for CPython (that can't be recompiled).

	 

	Also thanks in advance,

	 

	Joey J. Barrett

	Investment Technology Group, Inc.

	 

	 

	 

	 

	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/000037.html,

	but I can't understand it and old one.
	 
	Thank you in advance.
	Seungweon.

	 

	 

	
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-+-
	This message is for the named person's use only. This
communication is for
	informational purposes only and has been obtained from sources
believed to
	be reliable, but it is not necessarily complete and its accuracy
cannot be
	guaranteed. It is not intended as an offer or solicitation for
the purchase
	or sale of any financial instrument or as an official
confirmation of any
	transaction. Moreover, this material should not be construed to
contain any
	recommendation regarding, or opinion concerning, any security.
It may
	contain confidential, proprietary or legally privileged
information. No
	confidentiality or privilege is waived or lost by any
mistransmission. If
	you receive this message in error, please immediately delete it
and all
	copies of it from your system, destroy any hard copies of it and
notify the
	sender. You must not, directly or indirectly, use, disclose,
distribute,
	print, or copy any part of this message if you are not the
intended
	recipient.  Any views expressed in this message are those of the
individual
	sender, except where the message states otherwise and the sender
is
	authorized to state them to be the views of any such entity.
	
	Securities products and services provided to Canadian investors
are offered
	by ITG Canada Corp. (member CIPF and IDA), an affiliate of
Investment
	Technology Group, Inc.
	
	ITG Inc. and/or its affiliates reserves the right to monitor and
archive
	all electronic communications through its network.
	
	ITG Inc. Member NASD, SIPC
	
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-+-
	

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

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.
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
This message is for the named person's use only. This communication is for
informational purposes only and has been obtained from sources believed to
be reliable, but it is not necessarily complete and its accuracy cannot be
guaranteed. It is not intended as an offer or solicitation for the purchase
or sale of any financial instrument or as an official confirmation of any
transaction. Moreover, this material should not be construed to contain any
recommendation regarding, or opinion concerning, any security. It may
contain confidential, proprietary or legally privileged information. No
confidentiality or privilege is waived or lost by any mistransmission. If
you receive this message in error, please immediately delete it and all
copies of it from your system, destroy any hard copies of it and notify the
sender. You must not, directly or indirectly, use, disclose, distribute,
print, or copy any part of this message if you are not the intended
recipient.  Any views expressed in this message are those of the individual
sender, except where the message states otherwise and the sender is
authorized to state them to be the views of any such entity.

Securities products and services provided to Canadian investors are offered
by ITG Canada Corp. (member CIPF and IDA), an affiliate of Investment
Technology Group, Inc.

ITG Inc. and/or its affiliates reserves the right to monitor and archive
all electronic communications through its network.

ITG Inc. Member NASD, SIPC
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-


More information about the PythonDotNet mailing list