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 -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

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@python.org [mailto:pythondotnet-bounces@python.org] On Behalf Of Barrett, Joey Sent: Tuesday, June 05, 2007 8:11 PM To: pythondotnet@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.

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@C-BASS.COM] Sent: Wednesday, June 06, 2007 6:37 AM To: Barrett, Joey; pythondotnet@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@python.org [mailto:pythondotnet-bounces@python.org] On Behalf Of Barrett, Joey Sent: Tuesday, June 05, 2007 8:11 PM To: pythondotnet@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 -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

PyObject has a GetItem method that takes a string or an int to access the elements in a dictionary or a list. You can use GetAttr to get the attributes of a PyObject. Check out the PyObject class in the class browser to see the rest of the functionality. I think that you can set the PYTHONPATH through .net using system.environment or through PYTHON using sys.path.
-----Original Message----- From: Barrett, Joey [mailto:Joey.Barrett@itg.com] Sent: Wednesday, June 06, 2007 12:22 PM To: Karpenstein, Nissim (C-BASS); pythondotnet@python.org Subject: RE: [Python.NET] Could you tell me how to call python from c#?
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@C-BASS.COM] Sent: Wednesday, June 06, 2007 6:37 AM To: Barrett, Joey; pythondotnet@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@python.org [mailto:pythondotnet-bounces@python.org] On Behalf Of Barrett, Joey Sent: Tuesday, June 05, 2007 8:11 PM To: pythondotnet@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/00 0037.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 -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -+-+-+-+-+-+-
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 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.

Yes, you can set the PYTHONHOME variable from .net prior to calling the initialize. Many of the python runtimes set registry keys or are located in default locations. mike On 6/6/07, Barrett, Joey <Joey.Barrett@itg.com> wrote:
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@C-BASS.COM] Sent: Wednesday, June 06, 2007 6:37 AM To: Barrett, Joey; pythondotnet@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@python.org [mailto:pythondotnet-bounces@python.org] On Behalf Of Barrett, Joey Sent: Tuesday, June 05, 2007 8:11 PM To: pythondotnet@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 -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- _________________________________________________ Python.NET mailing list - PythonDotNet@python.org http://mail.python.org/mailman/listinfo/pythondotnet

Thank you for all the help. Loading a module located at C:\test\testModule.py is causing the following error: "Object reference not set to an instance of an object." TestModule.py def testMethod(self, options = {}): m = {} return m The last line of this code throws the error: // setting up PYTHONPATH System.Environment.SetEnvironmentVariable("PYTHONPATH",@"C:\test;"); PythonEngine.Initialize(); PyObject module = PythonEngine.ImportModule("testModule"); PyObject returnObject = module.InvokeMethod("testMethod", new PyTuple()); Is this the correct way to import a module or can a fully specified path be used? For example: PythonEngine.ImportModule("C:\test\testModule.py"); PythonEngine.Initialize() initializes the interpreter --- what interpreter is initialized? If Python is on the PATH, is that the interpreter initialized? Thanks, I really appreciate any tips. Joey J. Barrett Investment Technology Group, Inc. -----Original Message----- From: pythondotnet-bounces@python.org [mailto:pythondotnet-bounces@python.org] On Behalf Of Michael Eddington Sent: Wednesday, June 06, 2007 11:09 AM To: pythondotnet@python.org Subject: Re: [Python.NET] Could you tell me how to call python from c#? Yes, you can set the PYTHONHOME variable from .net prior to calling the initialize. Many of the python runtimes set registry keys or are located in default locations. mike On 6/6/07, Barrett, Joey <Joey.Barrett@itg.com> wrote:
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
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
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@C-BASS.COM] Sent: Wednesday, June 06, 2007 6:37 AM To: Barrett, Joey; pythondotnet@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
you the 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@python.org [mailto:pythondotnet-bounces@python.org] On Behalf Of Barrett, Joey Sent: Tuesday, June 05, 2007 8:11 PM To: pythondotnet@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
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -+-
_________________________________________________ Python.NET mailing list - PythonDotNet@python.org http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________ Python.NET mailing list - PythonDotNet@python.org http://mail.python.org/mailman/listinfo/pythondotnet -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- 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 -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
participants (3)
-
Barrett, Joey
-
Karpenstein, Nissim (C-BASS)
-
Michael Eddington