From swpark71 at gmail.com Mon Jun 4 10:21:36 2007 From: swpark71 at gmail.com (SeungWeon Park) Date: Mon, 04 Jun 2007 01:21:36 -0700 Subject: [Python.NET] Could you tell me how to call python from c#? Message-ID: <4663CB90.907@gmail.com> 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. From Joey.Barrett at itg.com Wed Jun 6 02:11:05 2007 From: Joey.Barrett at itg.com (Barrett, Joey) Date: Tue, 5 Jun 2007 20:11:05 -0400 Subject: [Python.NET] Could you tell me how to call python from c#? Message-ID: <335220DDCAA2CF44970497DD3666C7C701ED330B@MSG-EX01-SSI.win.itginc.com> 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 -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/pythondotnet/attachments/20070605/ca8d5f58/attachment.html From Joey.Barrett at itg.com Wed Jun 6 18:21:42 2007 From: Joey.Barrett at itg.com (Barrett, Joey) Date: Wed, 6 Jun 2007 12:21:42 -0400 Subject: [Python.NET] Could you tell me how to call python from c#? In-Reply-To: <10022919EEF6C54B8A63E6115F0DBF8D030C9689@NYCMAIL1.NYC.CBASS.INT> References: <335220DDCAA2CF44970497DD3666C7C701ED330B@MSG-EX01-SSI.win.itginc.com> <10022919EEF6C54B8A63E6115F0DBF8D030C9689@NYCMAIL1.NYC.CBASS.INT> Message-ID: <335220DDCAA2CF44970497DD3666C7C701ED34FD@MSG-EX01-SSI.win.itginc.com> 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 -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- From meddington at gmail.com Wed Jun 6 20:08:48 2007 From: meddington at gmail.com (Michael Eddington) Date: Wed, 6 Jun 2007 11:08:48 -0700 Subject: [Python.NET] Could you tell me how to call python from c#? In-Reply-To: <335220DDCAA2CF44970497DD3666C7C701ED34FD@MSG-EX01-SSI.win.itginc.com> References: <335220DDCAA2CF44970497DD3666C7C701ED330B@MSG-EX01-SSI.win.itginc.com> <10022919EEF6C54B8A63E6115F0DBF8D030C9689@NYCMAIL1.NYC.CBASS.INT> <335220DDCAA2CF44970497DD3666C7C701ED34FD@MSG-EX01-SSI.win.itginc.com> Message-ID: <2db0cefa0706061108p5cea647fw542f61ac628da87e@mail.gmail.com> 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 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 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 > -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > http://mail.python.org/mailman/listinfo/pythondotnet > From Joey.Barrett at itg.com Wed Jun 6 21:01:56 2007 From: Joey.Barrett at itg.com (Barrett, Joey) Date: Wed, 6 Jun 2007 15:01:56 -0400 Subject: [Python.NET] Could you tell me how to call python from c#? In-Reply-To: <2db0cefa0706061108p5cea647fw542f61ac628da87e@mail.gmail.com> References: <335220DDCAA2CF44970497DD3666C7C701ED330B@MSG-EX01-SSI.win.itginc.com><10022919EEF6C54B8A63E6115F0DBF8D030C9689@NYCMAIL1.NYC.CBASS.INT><335220DDCAA2CF44970497DD3666C7C701ED34FD@MSG-EX01-SSI.win.itginc.com> <2db0cefa0706061108p5cea647fw542f61ac628da87e@mail.gmail.com> Message-ID: <335220DDCAA2CF44970497DD3666C7C701ED36EF@MSG-EX01-SSI.win.itginc.com> 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 at python.org [mailto:pythondotnet-bounces at python.org] On Behalf Of Michael Eddington Sent: Wednesday, June 06, 2007 11:09 AM To: pythondotnet at 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 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 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 > -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -+- > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > http://mail.python.org/mailman/listinfo/pythondotnet > _________________________________________________ Python.NET mailing list - PythonDotNet at 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 -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- From Joey.Barrett at itg.com Thu Jun 7 03:48:19 2007 From: Joey.Barrett at itg.com (Barrett, Joey) Date: Wed, 6 Jun 2007 21:48:19 -0400 Subject: [Python.NET] Could you tell me how to call python from c#? In-Reply-To: <10022919EEF6C54B8A63E6115F0DBF8D030C968C@NYCMAIL1.NYC.CBASS.INT> References: <335220DDCAA2CF44970497DD3666C7C701ED36EF@MSG-EX01-SSI.win.itginc.com> <10022919EEF6C54B8A63E6115F0DBF8D030C968C@NYCMAIL1.NYC.CBASS.INT> Message-ID: <335220DDCAA2CF44970497DD3666C7C701ED3968@MSG-EX01-SSI.win.itginc.com> Thanks for working through this with me. Lots of questions, but I promise to write a nice HOW-TO on all this once I get it working from .NET. ... if (module == null) { throw new PythonException(); } catch (PythonException e) { result += e.Message + "\n" + e.StackTrace + "\n" + e.Type + "\n" + e.Value + e.Traceback; } result.toString() yields: Exception of type 'Python.Runtime.PythonException' was thrown. at PythonTester.PythonTest2() in C:\Documents and Settings\user\workspace\PythonTester.cs:line 180 75319696 7542755275491888 1. How do I get the error from the interpreter? (NOTE: I am not "finalizing/shutting-down" the interpreter so the errors should be available) >From a thread titled "[Python.NET] embedding in aspx problem" (http://mail.python.org/pipermail/pythondotnet/2004-May/000154.html), the user fixed the problem by adding a fully qualified path to the sys.path. My sys.path: 'C:\\Documents and Settings\\jbarrett\\Desktop\\Kryptonite\\Kryptonite\\bin\\python23.zip', '.\\DLLs', '.\\lib', '.\\lib\\plat-win', '.\\lib\\lib-tk', 'C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727' 2. What is python23.zip? I don't see it anywhere. 3. Are the relative paths (ie. '.\\DLLs', '.\\lib', ...) supposed to live off the root of the interpreter? 4. What is 'C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727' on the path for? Thanks again for any help you can offer. 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 12:10 PM To: Barrett, Joey; Michael Eddington; pythondotnet at python.org Subject: RE: [Python.NET] Could you tell me how to call python from c#? You can get the error message from ImportModule like this: // After ImportModule if (module == null) throw new PythonException() That will check the error codes from the Python Interpreter and populate fields in the Exception. The python interpreter being executed is the .dll version probably in your windows\system32 directory. I would try first to get the environemtn working the same outside of vs.net. Add c:\test to your pythonpath and make sure you can call the method from the python prompt in a shell window. Then, if that's working, restart your vs and it should work. I'm not sure how to modify the pythonpath on the fly or if that's really the problem. > -----Original Message----- > From: Barrett, Joey [mailto:Joey.Barrett at itg.com] > Sent: Wednesday, June 06, 2007 3:02 PM > To: Michael Eddington; Karpenstein, Nissim (C-BASS); > pythondotnet at python.org > Subject: RE: [Python.NET] Could you tell me how to call > python from c#? > > 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 at python.org > [mailto:pythondotnet-bounces at python.org] On Behalf Of Michael > Eddington > Sent: Wednesday, June 06, 2007 11:09 AM > To: pythondotnet at 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 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 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/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 > > > -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ > -+-+-+-+-+ > -+- > > _________________________________________________ > > Python.NET mailing list - PythonDotNet at python.org > > http://mail.python.org/mailman/listinfo/pythondotnet > > > _________________________________________________ > Python.NET mailing list - PythonDotNet at 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 > -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ > -+-+-+-+-+-+- > _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 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 -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- From jvm_cop at spamcop.net Thu Jun 7 16:37:48 2007 From: jvm_cop at spamcop.net (J. Merrill) Date: Thu, 07 Jun 2007 09:37:48 -0500 Subject: [Python.NET] Question about operators In-Reply-To: References: Message-ID: <7.0.1.0.2.20070607092517.076faa70@wheresmymailserver.com> I don't know of a way for you to get the .Net operators to work from Python, but I certainly wouldn't want to try to read code that used + to do an operation that you seem to think should be named Or. Why not add an Or method (or any name you want) to the BDDNode class, one that takes a BDDNode parameter, and write it so that others can figure out that code is calling a method of the BDDNode class? BDDNode A = new BDDNode(whatever); BDDNode B = new BDDNode(whatever); A = A.Or(B) gives more of an indication of what's happening than does A+B I'd have a bit of sympathy if you were trying to use + to call an operator that actually did some form of addition! BTW, does BDDOr create a new BDDNode object and return it, or does it modify "this" and then return a reference to it? Those are very different ways of doing things... At 05:20 PM 5/2/2007, Justin Frost wrote >Hi Folks, >Is there a way to write a C# class that has operators that work in Python? Currently, if I have a class with a method like the following: > >public static BDDNode operator +(BDDNode A, BDDNode B) > { > return A.BDDOr(B); > } > >and try to add two BDDNode Objects in Python ith the "+" operator, I get the following: > >TypeError: unsupported operand type(s) for +: 'BDDNode' and 'BDDNode' > >Any way around this? > >Thanks, >-Justin J. Merrill / Analytical Software Corp From Nissim.Karpenstein at C-BASS.COM Mon Jun 4 16:08:29 2007 From: Nissim.Karpenstein at C-BASS.COM (Karpenstein, Nissim (C-BASS)) Date: Mon, 4 Jun 2007 10:08:29 -0400 Subject: [Python.NET] Could you tell me how to call python from c#? In-Reply-To: <4663CB90.907@gmail.com> Message-ID: <10022919EEF6C54B8A63E6115F0DBF8D030C967F@NYCMAIL1.NYC.CBASS.INT> 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. From Nissim.Karpenstein at C-BASS.COM Wed Jun 6 15:37:22 2007 From: Nissim.Karpenstein at C-BASS.COM (Karpenstein, Nissim (C-BASS)) Date: Wed, 6 Jun 2007 09:37:22 -0400 Subject: [Python.NET] Could you tell me how to call python from c#? In-Reply-To: <335220DDCAA2CF44970497DD3666C7C701ED330B@MSG-EX01-SSI.win.itginc.com> Message-ID: <10022919EEF6C54B8A63E6115F0DBF8D030C9689@NYCMAIL1.NYC.CBASS.INT> 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. From Nissim.Karpenstein at C-BASS.COM Wed Jun 6 18:36:34 2007 From: Nissim.Karpenstein at C-BASS.COM (Karpenstein, Nissim (C-BASS)) Date: Wed, 6 Jun 2007 12:36:34 -0400 Subject: [Python.NET] Could you tell me how to call python from c#? In-Reply-To: <335220DDCAA2CF44970497DD3666C7C701ED34FD@MSG-EX01-SSI.win.itginc.com> Message-ID: <10022919EEF6C54B8A63E6115F0DBF8D030C968B@NYCMAIL1.NYC.CBASS.INT> 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 at itg.com] > Sent: Wednesday, June 06, 2007 12:22 PM > To: Karpenstein, Nissim (C-BASS); pythondotnet at 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 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/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. From Nissim.Karpenstein at C-BASS.COM Thu Jun 7 15:07:52 2007 From: Nissim.Karpenstein at C-BASS.COM (Karpenstein, Nissim (C-BASS)) Date: Thu, 7 Jun 2007 09:07:52 -0400 Subject: [Python.NET] Could you tell me how to call python from c#? In-Reply-To: <335220DDCAA2CF44970497DD3666C7C701ED3968@MSG-EX01-SSI.win.itginc.com> Message-ID: <10022919EEF6C54B8A63E6115F0DBF8D030C9690@NYCMAIL1.NYC.CBASS.INT> Those numbers in your exception message are pointers to Python Strings in the unmanaged side. Use this: catch (PythonException ex) { String typ = new PyObject(ex.Type).ToString(); String val = new PyObject(ex.Value).ToString(); String trace = new PyObject(ex.Traceback).ToString(); MessageBox.Show(typ + "\n" + val + "\n" + trace); } I'm not sure of the answer to any of your other questions. > -----Original Message----- > From: Barrett, Joey [mailto:Joey.Barrett at itg.com] > Sent: Wednesday, June 06, 2007 9:48 PM > To: Karpenstein, Nissim (C-BASS); Michael Eddington; > pythondotnet at python.org > Subject: RE: [Python.NET] Could you tell me how to call > python from c#? > > Thanks for working through this with me. Lots of questions, > but I promise to write a nice HOW-TO on all this once I get > it working from .NET. > > ... > if (module == null) > { > throw new PythonException(); > } > catch (PythonException e) > { > result += e.Message + "\n" + e.StackTrace + "\n" + e.Type + "\n" > + e.Value + e.Traceback; > } > > result.toString() yields: > > Exception of type 'Python.Runtime.PythonException' was thrown. at > PythonTester.PythonTest2() in C:\Documents and > Settings\user\workspace\PythonTester.cs:line 180 75319696 > 7542755275491888 > > 1. How do I get the error from the interpreter? (NOTE: I am > not "finalizing/shutting-down" the interpreter so the errors should be > available) > > > From a thread titled "[Python.NET] embedding in aspx problem" > (http://mail.python.org/pipermail/pythondotnet/2004-May/000154.html), > the user fixed the problem by adding a fully qualified path > to the sys.path. > > My sys.path: > > 'C:\\Documents and > Settings\\jbarrett\\Desktop\\Kryptonite\\Kryptonite\\bin\\pyth > on23.zip', > '.\\DLLs', '.\\lib', '.\\lib\\plat-win', '.\\lib\\lib-tk', > 'C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727' > > 2. What is python23.zip? I don't see it anywhere. > > 3. Are the relative paths (ie. '.\\DLLs', '.\\lib', ...) > supposed to live off the root of the interpreter? > > 4. What is > 'C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727' on the path for? > > Thanks again for any help you can offer. > > 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 12:10 PM > To: Barrett, Joey; Michael Eddington; pythondotnet at python.org > Subject: RE: [Python.NET] Could you tell me how to call > python from c#? > > You can get the error message from ImportModule like this: > > // After ImportModule > if (module == null) > throw new PythonException() > > That will check the error codes from the Python Interpreter > and populate fields in the Exception. > The python interpreter being executed is the .dll version > probably in your windows\system32 directory. > > I would try first to get the environemtn working the same > outside of vs.net. Add c:\test to your pythonpath and make > sure you can call the method from the python prompt in a > shell window. Then, if that's working, restart your vs and > it should work. I'm not sure how to modify the pythonpath on > the fly or if that's really the problem. > > > > > -----Original Message----- > > From: Barrett, Joey [mailto:Joey.Barrett at itg.com] > > Sent: Wednesday, June 06, 2007 3:02 PM > > To: Michael Eddington; Karpenstein, Nissim (C-BASS); > > pythondotnet at python.org > > Subject: RE: [Python.NET] Could you tell me how to call python from > > c#? > > > > 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 at python.org > > [mailto:pythondotnet-bounces at python.org] On Behalf Of Michael > > Eddington > > Sent: Wednesday, June 06, 2007 11:09 AM > > To: pythondotnet at 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 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 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/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 > > > > > -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ > > -+-+-+-+-+ > > -+- > > > _________________________________________________ > > > Python.NET mailing list - PythonDotNet at python.org > > > http://mail.python.org/mailman/listinfo/pythondotnet > > > > > _________________________________________________ > > Python.NET mailing list - PythonDotNet at 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 > > -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ > > -+-+-+-+-+-+- > > > _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ > > 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. From Joey.Barrett at itg.com Thu Jun 7 20:09:10 2007 From: Joey.Barrett at itg.com (Barrett, Joey) Date: Thu, 7 Jun 2007 14:09:10 -0400 Subject: [Python.NET] Could you tell me how to call python from c#? In-Reply-To: <10022919EEF6C54B8A63E6115F0DBF8D030C9690@NYCMAIL1.NYC.CBASS.INT> References: <335220DDCAA2CF44970497DD3666C7C701ED3968@MSG-EX01-SSI.win.itginc.com> <10022919EEF6C54B8A63E6115F0DBF8D030C9690@NYCMAIL1.NYC.CBASS.INT> Message-ID: <335220DDCAA2CF44970497DD3666C7C701ED3C40@MSG-EX01-SSI.win.itginc.com> Yay! Success! Karpenstein, the details of PythonException really helped me sort importing out. THANK YOU SO MUCH! Locally, everything works great---but when the project is deployed to another environment it breaks. The bin of my project contains: Python.Runtime.dll python24.dll The deployed environment is installed with the same python as locally in addition 'python' is in the path. PythonEngine.Initialize(); Causes: "Unable to load one or more of the requested types." What am I missing? How do you package up Python for .net appropriately? Thanks in advance, Joey J. Barrett Investment Technology Group, Inc. -----Original Message----- From: Karpenstein, Nissim (C-BASS) [mailto:Nissim.Karpenstein at C-BASS.COM] Sent: Thursday, June 07, 2007 6:08 AM To: Barrett, Joey; Michael Eddington; pythondotnet at python.org Subject: RE: [Python.NET] Could you tell me how to call python from c#? Those numbers in your exception message are pointers to Python Strings in the unmanaged side. Use this: catch (PythonException ex) { String typ = new PyObject(ex.Type).ToString(); String val = new PyObject(ex.Value).ToString(); String trace = new PyObject(ex.Traceback).ToString(); MessageBox.Show(typ + "\n" + val + "\n" + trace); } I'm not sure of the answer to any of your other questions. > -----Original Message----- > From: Barrett, Joey [mailto:Joey.Barrett at itg.com] > Sent: Wednesday, June 06, 2007 9:48 PM > To: Karpenstein, Nissim (C-BASS); Michael Eddington; > pythondotnet at python.org > Subject: RE: [Python.NET] Could you tell me how to call > python from c#? > > Thanks for working through this with me. Lots of questions, > but I promise to write a nice HOW-TO on all this once I get > it working from .NET. > > ... > if (module == null) > { > throw new PythonException(); > } > catch (PythonException e) > { > result += e.Message + "\n" + e.StackTrace + "\n" + e.Type + "\n" > + e.Value + e.Traceback; > } > > result.toString() yields: > > Exception of type 'Python.Runtime.PythonException' was thrown. at > PythonTester.PythonTest2() in C:\Documents and > Settings\user\workspace\PythonTester.cs:line 180 75319696 > 7542755275491888 > > 1. How do I get the error from the interpreter? (NOTE: I am > not "finalizing/shutting-down" the interpreter so the errors should be > available) > > > From a thread titled "[Python.NET] embedding in aspx problem" > (http://mail.python.org/pipermail/pythondotnet/2004-May/000154.html), > the user fixed the problem by adding a fully qualified path > to the sys.path. > > My sys.path: > > 'C:\\Documents and > Settings\\jbarrett\\Desktop\\Kryptonite\\Kryptonite\\bin\\pyth > on23.zip', > '.\\DLLs', '.\\lib', '.\\lib\\plat-win', '.\\lib\\lib-tk', > 'C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727' > > 2. What is python23.zip? I don't see it anywhere. > > 3. Are the relative paths (ie. '.\\DLLs', '.\\lib', ...) > supposed to live off the root of the interpreter? > > 4. What is > 'C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727' on the path for? > > Thanks again for any help you can offer. > > 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 12:10 PM > To: Barrett, Joey; Michael Eddington; pythondotnet at python.org > Subject: RE: [Python.NET] Could you tell me how to call > python from c#? > > You can get the error message from ImportModule like this: > > // After ImportModule > if (module == null) > throw new PythonException() > > That will check the error codes from the Python Interpreter > and populate fields in the Exception. > The python interpreter being executed is the .dll version > probably in your windows\system32 directory. > > I would try first to get the environemtn working the same > outside of vs.net. Add c:\test to your pythonpath and make > sure you can call the method from the python prompt in a > shell window. Then, if that's working, restart your vs and > it should work. I'm not sure how to modify the pythonpath on > the fly or if that's really the problem. > > > > > -----Original Message----- > > From: Barrett, Joey [mailto:Joey.Barrett at itg.com] > > Sent: Wednesday, June 06, 2007 3:02 PM > > To: Michael Eddington; Karpenstein, Nissim (C-BASS); > > pythondotnet at python.org > > Subject: RE: [Python.NET] Could you tell me how to call python from > > c#? > > > > 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 at python.org > > [mailto:pythondotnet-bounces at python.org] On Behalf Of Michael > > Eddington > > Sent: Wednesday, June 06, 2007 11:09 AM > > To: pythondotnet at 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 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 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/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 > > > > > -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ > > -+-+-+-+-+ > > -+- > > > _________________________________________________ > > > Python.NET mailing list - PythonDotNet at python.org > > > http://mail.python.org/mailman/listinfo/pythondotnet > > > > > _________________________________________________ > > Python.NET mailing list - PythonDotNet at 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 > > -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ > > -+-+-+-+-+-+- > > > _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ > > 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. -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- 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 -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- From flatline403 at gmail.com Tue Jun 26 20:34:42 2007 From: flatline403 at gmail.com (Dylan Flaherty) Date: Tue, 26 Jun 2007 11:34:42 -0700 Subject: [Python.NET] How to run a .py file from C# Message-ID: <48da3c9b0706261134t11bc17e1r344560aece56f3d2@mail.gmail.com> Hi, I saw this somewhat old thread on an IronPython forum (Link: http://mail.python.org/pipermail/pythondotnet/2006-June/000504.html, also shown below.) and I am trying to do exactly the same thing as the person who originally posed the question: I would like to run .py files from within a C# program, using pythonEngine. The .py files contain several python functions that I would like to be able to call. I was wondering if the newer versions of IronPython have fixed this problem and if there is now a solution for what I'm trying to do? Also, is there somewhere that I might find documentation as to the functions/API for pythonEngine? Thanks, Dylan -----------------------ORIGINAL THREAD----------------------------------------------------------------------------------------------------------- Have you tried using the PythonEngine.RunString (not RunSimpleString) method? Still not as convenient as having a RunFile, but that should do what you need for now. -Brian >* -----Original Message----- *>* From: pythondotnet-bounces at python.org *>* [mailto:pythondotnet-bounces at python.org ] On Behalf Of LIVERNAIS Sylvie *>* Sent: Thursday, June 01, 2006 11:08 AM *>* To: pythondotnet at python.org *>* Subject: [Python.NET] How to run a .py file from C# *>* *>* Hi, *>* *>* I'm a newbie in C# and python and try to run a .py file from *>* a C# program. *>* *>* I tried the same thing from a C++ program using *>* PyRun_SimpleFile and in the console, I could see the python *>* execution (the print from python). *>* *>* Using Python.Runtime.dll from Python.NET, I saw that such a *>* fonction is not available, and should use *>* PythonEngine.ImportModule and PythonEngine.RunString or *>* PythonEngine.RunSimpleString. The thing is that it only works *>* for a command written over one line, but if I have a "def" *>* (which is written over several lines), the RunString doesn't *>* work any more... So how can I run the whole file, and not *>* only line by line ? *>* *>* By the way, what does exactly the ImportModule function ? *>* Maybe the .py file is already executed but I can't see it ? *>* Is there a way to get the output and display it in the console ? *>* *>* Thanks for you answer *>* *>* __ Sylvie LIVERNAIS ___________ *>* Applications Cartes ? Puce *>* EID *>* * : +33 (0) 388 14 28 75 * -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/pythondotnet/attachments/20070626/d137ec34/attachment.html From mc at mclaveau.com Wed Jun 27 07:29:50 2007 From: mc at mclaveau.com (Michel Claveau) Date: Wed, 27 Jun 2007 07:29:50 +0200 Subject: [Python.NET] How to run a .py file from C# References: <48da3c9b0706261134t11bc17e1r344560aece56f3d2@mail.gmail.com> Message-ID: <000e01c7b87c$308beb00$0901a8c0@ServeurMCI> Hi! > I would like to run .py files from within a C# program, using > pythonEngine. Perso, I had make a COM-server with Python+PyWin32. I can call this soft, from C#, with "late-binding" techno. And, a COM-server-Python-script can run .py's files (execfile, import, etc.) But, when I know Python, I use very very very little C# # sorry for my bad english :-( @-salutations Michel Claveau