Re: [Python.NET] Could you tell me how to call python from c#?
From a thread titled "[Python.NET] embedding in aspx problem" (http://mail.python.org/pipermail/pythondotnet/2004-May/000154.html),
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) 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@C-BASS.COM] Sent: Wednesday, June 06, 2007 12:10 PM To: Barrett, Joey; Michael Eddington; pythondotnet@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@itg.com] Sent: Wednesday, June 06, 2007 3:02 PM To: Michael Eddington; Karpenstein, Nissim (C-BASS); pythondotnet@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@python.org [mailto:pythondotnet-bounces@python.org] On Behalf Of Michael Eddington Sent: Wednesday, June 06, 2007 11:09 AM To: pythondotnet@python.org Subject: Re: [Python.NET] Could you tell me how to call python from c#?
Yes, you can set the PYTHONHOME variable from .net prior to calling the initialize. Many of the python runtimes set registry keys or are located in default locations.
mike
On 6/6/07, Barrett, Joey <Joey.Barrett@itg.com> wrote:
Thank you for your response, this is helpful.
The methods called usually return lists or dictionaries and
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
Python for .net library, or would it be required to write a wrapper python module that adds to the path first? Maybe there is another way to handle this?
Thank you again
Joey J. Barrett Investment Technology Group, Inc.
-----Original Message----- From: Karpenstein, Nissim (C-BASS) [mailto:Nissim.Karpenstein@C-BASS.COM] Sent: Wednesday, June 06, 2007 6:37 AM To: Barrett, Joey; pythondotnet@python.org Subject: RE: [Python.NET] Could you tell me how to call python from c#?
I tried to post this to the list yesterday but it bounced because of address issues...hopefully this will work:
If you want to execute a python program from c-sharp you can just use System.Diagnostics.Process to execute python.exe and you don't need python for .net.
Python for .net is more appropriate for if you want to execute a
sometimes the python
function or create an instance of a python object and then run methods and access attributes from your C# program.
The example you linked to is OK, except that he modified the Python.Runtime.Dll which is not necessary.
If you had a Hello.py with a function called hello Like this:
def hello(): return 'hello'
You could do this in C#:
PythonEngine.Initialize(); PyObject helloModule = PythonEngine.ImportModule("Hello"); String fromPython = helloModule.InvokeMethod("hello", new PyTuple()).ToString(); PythonEngine.Shutdown();
// now fromPython = "hello"
If your function takes an argument:
def returnArg(arg): return arg
PythonEngine.Initialize(); PyObject helloModule = PythonEngine.ImportModule("Hello"); String fromPython = helloModule.InvokeMethod("returnArg", new PyObject[1] {new PyString("Test")}).ToString(); PythonEngine.Shutdown();
// now fromPython = "test"
________________________________
From: pythondotnet-bounces@python.org [mailto:pythondotnet-bounces@python.org] On Behalf Of Barrett, Joey Sent: Tuesday, June 05, 2007 8:11 PM To: pythondotnet@python.org Subject: Re: [Python.NET] Could you tell me how to call python from c#?
Hi,
I also would be very interested in seeing some examples to "call python from c#" using "Python for .NET". I have dug around Google and a number of forums---I can't find anything except the post from below. I really hope I'm not missing the obvious somewhere.
IronPython is great but my needs require using a few c++ extensions compiled for CPython (that can't be recompiled).
Also thanks in advance,
Joey J. Barrett
Investment Technology Group, Inc.
Hello, PythonNet!
I'm looking for some sample code to call python from C#. Actually, I'd like to execute python program(let say 'Hello.py') from C# with parameters. Actually, I'm a newbie at both of languages. So, could you give sample source code? I was looking for some guide or source for several days, but I couldn't find it. What I've got so far from googling is
http://mail.python.org/pipermail/pythondotnet/2003-November/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@python.org http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________ Python.NET mailing list - PythonDotNet@python.org http://mail.python.org/mailman/listinfo/pythondotnet -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -+-+-+-+-+-+- This message is for the named person's use only. This communication is for informational purposes only and has been obtained from sources believed to be reliable, but it is not necessarily complete and its accuracy cannot be guaranteed. It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction. Moreover, this material should not be construed to contain any recommendation regarding, or opinion concerning, any security. It may contain confidential, proprietary or legally privileged information. No confidentiality or privilege is waived or lost by any mistransmission. If you receive this message in error, please immediately delete it and all copies of it from your system, destroy any hard copies of it and notify the sender. You must not, directly or indirectly, use, disclose, distribute, print, or copy any part of this message if you are not the intended recipient. Any views expressed in this message are those of the individual sender, except where the message states otherwise and the sender is authorized to state them to be the views of any such entity.
Securities products and services provided to Canadian investors are offered by ITG Canada Corp. (member CIPF and IDA), an affiliate of Investment Technology Group, Inc.
ITG Inc. and/or its affiliates reserves the right to monitor and archive all electronic communications through its network.
ITG Inc. Member NASD, SIPC -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -+-+-+-+-+-+-
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 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 -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
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@itg.com] Sent: Wednesday, June 06, 2007 9:48 PM To: Karpenstein, Nissim (C-BASS); Michael Eddington; pythondotnet@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@C-BASS.COM] Sent: Wednesday, June 06, 2007 12:10 PM To: Barrett, Joey; Michael Eddington; pythondotnet@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@itg.com] Sent: Wednesday, June 06, 2007 3:02 PM To: Michael Eddington; Karpenstein, Nissim (C-BASS); pythondotnet@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@python.org [mailto:pythondotnet-bounces@python.org] On Behalf Of Michael Eddington Sent: Wednesday, June 06, 2007 11:09 AM To: pythondotnet@python.org Subject: Re: [Python.NET] Could you tell me how to call python from c#?
Yes, you can set the PYTHONHOME variable from .net prior to calling the initialize. Many of the python runtimes set registry keys or are located in default locations.
mike
On 6/6/07, Barrett, Joey <Joey.Barrett@itg.com> wrote:
Thank you for your response, this is helpful.
The methods called usually return lists or dictionaries and
other types too that need to be used within the C#. In
provided the return value from InvokeMethod(), is a string "fromPython", but I noticed InvokeMethod() actually returns a PyObject.
How can I look at that PyObject and extract the returned values?
The classes I use import many modules from a number of locations on disk that aren't known to the interpreter.
Is there a way to add to the PYTHONPATH programmatically in C# using
Python for .net library, or would it be required to write a wrapper python module that adds to the path first? Maybe there is another way to handle this?
Thank you again
Joey J. Barrett Investment Technology Group, Inc.
-----Original Message----- From: Karpenstein, Nissim (C-BASS) [mailto:Nissim.Karpenstein@C-BASS.COM] Sent: Wednesday, June 06, 2007 6:37 AM To: Barrett, Joey; pythondotnet@python.org Subject: RE: [Python.NET] Could you tell me how to call
sometimes the example you the python from c#?
I tried to post this to the list yesterday but it bounced
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
because of python
function or create an instance of a python object and then run methods and access attributes from your C# program.
The example you linked to is OK, except that he modified the Python.Runtime.Dll which is not necessary.
If you had a Hello.py with a function called hello Like this:
def hello(): return 'hello'
You could do this in C#:
PythonEngine.Initialize(); PyObject helloModule = PythonEngine.ImportModule("Hello"); String fromPython = helloModule.InvokeMethod("hello", new PyTuple()).ToString(); PythonEngine.Shutdown();
// now fromPython = "hello"
If your function takes an argument:
def returnArg(arg): return arg
PythonEngine.Initialize(); PyObject helloModule = PythonEngine.ImportModule("Hello"); String fromPython = helloModule.InvokeMethod("returnArg", new PyObject[1] {new PyString("Test")}).ToString(); PythonEngine.Shutdown();
// now fromPython = "test"
________________________________
From: pythondotnet-bounces@python.org [mailto:pythondotnet-bounces@python.org] On Behalf Of Barrett, Joey Sent: Tuesday, June 05, 2007 8:11 PM To: pythondotnet@python.org Subject: Re: [Python.NET] Could you tell me how to call python from c#?
Hi,
I also would be very interested in seeing some examples to "call python from c#" using "Python for .NET". I have dug around Google and a number of forums---I can't find anything except the post from below. I really hope I'm not missing the obvious somewhere.
IronPython is great but my needs require using a few c++ extensions compiled for CPython (that can't be recompiled).
Also thanks in advance,
Joey J. Barrett
Investment Technology Group, Inc.
Hello, PythonNet!
I'm looking for some sample code to call python from C#. Actually, I'd like to execute python program(let say 'Hello.py') from C# with parameters. Actually, I'm a newbie at both of languages. So, could you give sample source code? I was looking for some guide or source for several days, but I couldn't find it. What I've got so far from googling is
http://mail.python.org/pipermail/pythondotnet/2003-November/00 0037.html,
but I can't understand it and old one.
Thank you in advance. Seungweon.
-+- This message is for the named person's use only. This communication is for informational purposes only and has been obtained from sources believed to be reliable, but it is not necessarily complete and its accuracy cannot be guaranteed. It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction. Moreover, this material should not be construed to contain any recommendation regarding, or opinion concerning, any security. It may contain confidential, proprietary or legally privileged information. No confidentiality or privilege is waived or lost by any mistransmission. If you receive this message in error, please immediately delete it and all copies of it from your system, destroy any hard copies of it and notify the sender. You must not, directly or indirectly, use, disclose, distribute, print, or copy any part of this message if you are not the intended recipient. Any views expressed in this message are those of
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -+-+-+-+-+ the
individual sender, except where the message states otherwise and the sender is authorized to state them to be the views of any such entity.
Securities products and services provided to Canadian investors are offered by ITG Canada Corp. (member CIPF and IDA), an affiliate of Investment Technology Group, Inc.
ITG Inc. and/or its affiliates reserves the right to monitor and archive all electronic communications through its network.
ITG Inc. Member NASD, SIPC
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -+-+-+-+-+
-+-
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
This e-mail (including any attachments) may contain information that is private, confidential, or protected by attorney-client or other privilege. If you received this e-mail in error, please delete it from your system without copying it and notify sender by reply e-mail, so that our records can be corrected.
This message is for the named person's use only. This communication is for informational purposes only and has been obtained from sources believed to be reliable, but it is not necessarily complete and its accuracy cannot be guaranteed. It is not intended as an offer or solicitation for the
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
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -+-+-+-+-+ -+- purchase the intended
recipient. Any views expressed in this message are those of the individual sender, except where the message states otherwise and the sender is authorized to state them to be the views of any such entity.
Securities products and services provided to Canadian investors are offered by ITG Canada Corp. (member CIPF and IDA), an affiliate of Investment Technology Group, Inc.
ITG Inc. and/or its affiliates reserves the right to monitor and archive all electronic communications through its network.
ITG Inc. Member NASD, SIPC
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -+-+-+-+-+ -+-
_________________________________________________ Python.NET mailing list - PythonDotNet@python.org http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________ Python.NET mailing list - PythonDotNet@python.org http://mail.python.org/mailman/listinfo/pythondotnet -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -+-+-+-+-+-+- This message is for the named person's use only. This communication is for informational purposes only and has been obtained from sources believed to be reliable, but it is not necessarily complete and its accuracy cannot be guaranteed. It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction. Moreover, this material should not be construed to contain any recommendation regarding, or opinion concerning, any security. It may contain confidential, proprietary or legally privileged information. No confidentiality or privilege is waived or lost by any mistransmission. If you receive this message in error, please immediately delete it and all copies of it from your system, destroy any hard copies of it and notify the sender. You must not, directly or indirectly, use, disclose, distribute, print, or copy any part of this message if you are not the intended recipient. Any views expressed in this message are those of the individual sender, except where the message states otherwise and the sender is authorized to state them to be the views of any such entity.
Securities products and services provided to Canadian investors are offered by ITG Canada Corp. (member CIPF and IDA), an affiliate of Investment Technology Group, Inc.
ITG Inc. and/or its affiliates reserves the right to monitor and archive all electronic communications through its network.
ITG Inc. Member NASD, SIPC -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -+-+-+-+-+-+-
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
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.
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@C-BASS.COM] Sent: Thursday, June 07, 2007 6:08 AM To: Barrett, Joey; Michael Eddington; pythondotnet@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@itg.com] Sent: Wednesday, June 06, 2007 9:48 PM To: Karpenstein, Nissim (C-BASS); Michael Eddington; pythondotnet@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@C-BASS.COM] Sent: Wednesday, June 06, 2007 12:10 PM To: Barrett, Joey; Michael Eddington; pythondotnet@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@itg.com] Sent: Wednesday, June 06, 2007 3:02 PM To: Michael Eddington; Karpenstein, Nissim (C-BASS); pythondotnet@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@python.org [mailto:pythondotnet-bounces@python.org] On Behalf Of Michael Eddington Sent: Wednesday, June 06, 2007 11:09 AM To: pythondotnet@python.org Subject: Re: [Python.NET] Could you tell me how to call python from c#?
Yes, you can set the PYTHONHOME variable from .net prior to calling the initialize. Many of the python runtimes set registry keys or are located in default locations.
mike
On 6/6/07, Barrett, Joey <Joey.Barrett@itg.com> wrote:
Thank you for your response, this is helpful.
The methods called usually return lists or dictionaries and
other types too that need to be used within the C#. In
provided the return value from InvokeMethod(), is a string "fromPython", but I noticed InvokeMethod() actually returns a PyObject.
How can I look at that PyObject and extract the returned values?
The classes I use import many modules from a number of locations on disk that aren't known to the interpreter.
Is there a way to add to the PYTHONPATH programmatically in C# using
Python for .net library, or would it be required to write a wrapper python module that adds to the path first? Maybe there is another way to handle this?
Thank you again
Joey J. Barrett Investment Technology Group, Inc.
-----Original Message----- From: Karpenstein, Nissim (C-BASS) [mailto:Nissim.Karpenstein@C-BASS.COM] Sent: Wednesday, June 06, 2007 6:37 AM To: Barrett, Joey; pythondotnet@python.org Subject: RE: [Python.NET] Could you tell me how to call
sometimes the example you the python from c#?
I tried to post this to the list yesterday but it bounced
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
because of python
function or create an instance of a python object and then run methods and access attributes from your C# program.
The example you linked to is OK, except that he modified the Python.Runtime.Dll which is not necessary.
If you had a Hello.py with a function called hello Like this:
def hello(): return 'hello'
You could do this in C#:
PythonEngine.Initialize(); PyObject helloModule = PythonEngine.ImportModule("Hello"); String fromPython = helloModule.InvokeMethod("hello", new PyTuple()).ToString(); PythonEngine.Shutdown();
// now fromPython = "hello"
If your function takes an argument:
def returnArg(arg): return arg
PythonEngine.Initialize(); PyObject helloModule = PythonEngine.ImportModule("Hello"); String fromPython = helloModule.InvokeMethod("returnArg", new PyObject[1] {new PyString("Test")}).ToString(); PythonEngine.Shutdown();
// now fromPython = "test"
________________________________
From: pythondotnet-bounces@python.org [mailto:pythondotnet-bounces@python.org] On Behalf Of Barrett, Joey Sent: Tuesday, June 05, 2007 8:11 PM To: pythondotnet@python.org Subject: Re: [Python.NET] Could you tell me how to call python from c#?
Hi,
I also would be very interested in seeing some examples to "call python from c#" using "Python for .NET". I have dug around Google and a number of forums---I can't find anything except the post from below. I really hope I'm not missing the obvious somewhere.
IronPython is great but my needs require using a few c++ extensions compiled for CPython (that can't be recompiled).
Also thanks in advance,
Joey J. Barrett
Investment Technology Group, Inc.
Hello, PythonNet!
I'm looking for some sample code to call python from C#. Actually, I'd like to execute python program(let say 'Hello.py') from C# with parameters. Actually, I'm a newbie at both of languages. So, could you give sample source code? I was looking for some guide or source for several days, but I couldn't find it. What I've got so far from googling is
http://mail.python.org/pipermail/pythondotnet/2003-November/00 0037.html,
but I can't understand it and old one.
Thank you in advance. Seungweon.
-+- This message is for the named person's use only. This communication is for informational purposes only and has been obtained from sources believed to be reliable, but it is not necessarily complete and its accuracy cannot be guaranteed. It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction. Moreover, this material should not be construed to contain any recommendation regarding, or opinion concerning, any security. It may contain confidential, proprietary or legally privileged information. No confidentiality or privilege is waived or lost by any mistransmission. If you receive this message in error, please immediately delete it and all copies of it from your system, destroy any hard copies of it and notify the sender. You must not, directly or indirectly, use, disclose, distribute, print, or copy any part of this message if you are not the intended recipient. Any views expressed in this message are those of
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -+-+-+-+-+ the
individual sender, except where the message states otherwise and the sender is authorized to state them to be the views of any such entity.
Securities products and services provided to Canadian investors are offered by ITG Canada Corp. (member CIPF and IDA), an affiliate of Investment Technology Group, Inc.
ITG Inc. and/or its affiliates reserves the right to monitor and archive all electronic communications through its network.
ITG Inc. Member NASD, SIPC
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -+-+-+-+-+
-+-
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
This e-mail (including any attachments) may contain information that is private, confidential, or protected by attorney-client or other privilege. If you received this e-mail in error, please delete it from your system without copying it and notify sender by reply e-mail, so that our records can be corrected.
This message is for the named person's use only. This communication is for informational purposes only and has been obtained from sources believed to be reliable, but it is not necessarily complete and its accuracy cannot be guaranteed. It is not intended as an offer or solicitation for the
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
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -+-+-+-+-+ -+- purchase the intended
recipient. Any views expressed in this message are those of the individual sender, except where the message states otherwise and the sender is authorized to state them to be the views of any such entity.
Securities products and services provided to Canadian investors are offered by ITG Canada Corp. (member CIPF and IDA), an affiliate of Investment Technology Group, Inc.
ITG Inc. and/or its affiliates reserves the right to monitor and archive all electronic communications through its network.
ITG Inc. Member NASD, SIPC
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -+-+-+-+-+ -+-
_________________________________________________ Python.NET mailing list - PythonDotNet@python.org http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________ Python.NET mailing list - PythonDotNet@python.org http://mail.python.org/mailman/listinfo/pythondotnet -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -+-+-+-+-+-+- This message is for the named person's use only. This communication is for informational purposes only and has been obtained from sources believed to be reliable, but it is not necessarily complete and its accuracy cannot be guaranteed. It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction. Moreover, this material should not be construed to contain any recommendation regarding, or opinion concerning, any security. It may contain confidential, proprietary or legally privileged information. No confidentiality or privilege is waived or lost by any mistransmission. If you receive this message in error, please immediately delete it and all copies of it from your system, destroy any hard copies of it and notify the sender. You must not, directly or indirectly, use, disclose, distribute, print, or copy any part of this message if you are not the intended recipient. Any views expressed in this message are those of the individual sender, except where the message states otherwise and the sender is authorized to state them to be the views of any such entity.
Securities products and services provided to Canadian investors are offered by ITG Canada Corp. (member CIPF and IDA), an affiliate of Investment Technology Group, Inc.
ITG Inc. and/or its affiliates reserves the right to monitor and archive all electronic communications through its network.
ITG Inc. Member NASD, SIPC -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -+-+-+-+-+-+-
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
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 -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
participants (2)
-
Barrett, Joey
-
Karpenstein, Nissim (C-BASS)