From denis.akhiyarov at gmail.com Sun Aug 3 06:50:52 2014 From: denis.akhiyarov at gmail.com (Denis Akhiyarov) Date: Sat, 2 Aug 2014 23:50:52 -0500 Subject: [Python.NET] How to embed properly using Python for .NET Message-ID: When I try to use PythonEngine.ImportModule(mymodulename) some of the optional modules in dependencies are attempted to be loaded (not required for module use without embedding). This results in return null from this method because some of these optional dependencies are not required and hence not available. What is the proper method to use in this PythonNET API for loading user-written module which depends on multiple other modules? I tried importhook.cs from both versions: https://github.com/pythonnet/pythonnet https://github.com/renshawbay/pythonnet http://stackoverflow.com/questions/25101718/how-to-embed-properly-using-python-for-net -------------- next part -------------- An HTML attachment was scrubbed... URL: From siddhupiddu at gmail.com Mon Aug 4 20:11:14 2014 From: siddhupiddu at gmail.com (Mika S) Date: Mon, 4 Aug 2014 11:11:14 -0700 Subject: [Python.NET] How to embed properly using Python for .NET In-Reply-To: References: Message-ID: What dependencies are missing? I find that having a basic python installation on the target machine is necessary. If that's not the case then copy your python folder with the modules and they eggs on the target machine and set the PYTHONPATH environment var from c#. On Sat, Aug 2, 2014 at 9:50 PM, Denis Akhiyarov wrote: > When I try to use > > PythonEngine.ImportModule(mymodulename) > > some of the optional modules in dependencies are attempted to be loaded > (not required for module use without embedding). This results in return > null from this method because some of these optional dependencies are not > required and hence not available. What is the proper method to use in this > PythonNET API for loading user-written module which depends on multiple > other modules? > > I tried importhook.cs from both versions: > > https://github.com/pythonnet/pythonnet > > https://github.com/renshawbay/pythonnet > > > > http://stackoverflow.com/questions/25101718/how-to-embed-properly-using-python-for-net > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jonnojohnson at gmail.com Thu Aug 7 14:59:12 2014 From: jonnojohnson at gmail.com (Jonno) Date: Thu, 7 Aug 2014 07:59:12 -0500 Subject: [Python.NET] Create a System.Int32[] collection Message-ID: The dll I'm talking to is unable to accept as an argument a python list of integers. I can't find how to create a System.Int32[] collection. Can anyone help? -------------- next part -------------- An HTML attachment was scrubbed... URL: From serge.weinstock at uk.bnpparibas.com Wed Aug 13 15:35:30 2014 From: serge.weinstock at uk.bnpparibas.com (Serge WEINSTOCK) Date: Wed, 13 Aug 2014 13:35:30 +0000 Subject: [Python.NET] how to use an embedded python intepreter? Message-ID: I'm trying to use an embedded python interpreter from C# using pythonnet (the python3 compatible version found at https://github.com/renshawbay/pythonnet) My interpreter is located in D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime and has the "Lib" and "Libs" folder from the python distribution. I've tested using the following code: //==================================================================== PythonEngine.PythonHome = @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; PythonEngine.ProgramName = "PythonRuntime"; PythonEngine.Initialize(); using (Py.GIL()) { PythonEngine.RunSimpleString("print(1)"); } //==================================================================== But, it doesn't work. I get a "SystemError: PyEvalCodeEx: NULL globals". Everytime I try to get an object from python, the code fails. What am I doing wrong? ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. -------------- next part -------------- An HTML attachment was scrubbed... URL: From serge.weinstock at uk.bnpparibas.com Tue Aug 19 15:22:01 2014 From: serge.weinstock at uk.bnpparibas.com (Serge WEINSTOCK) Date: Tue, 19 Aug 2014 13:22:01 +0000 Subject: [Python.NET] Running an embedded interpreter Message-ID: Hi, I'm trying to use Python3.2 using the Python.Net version found at: https://github.com/renshawbay/pythonnet I'm using the following simple test program: //======================================================================= using System; using System.IO; using Python.Runtime; namespace TestPythonNet { class Program { static void Main(string[] args) { string binDir = Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(Program)).Location); string pyHome = @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; PythonEngine.PythonHome = @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; PythonEngine.ProgramName = "PythonRuntime"; Environment.SetEnvironmentVariable("PYTHONPATH", Path.GetFullPath(Path.Combine(pyHome, "DLLs")) + ";" + Path.GetFullPath(Path.Combine(pyHome, "Lib")) + ";" + Path.GetFullPath(Path.Combine(pyHome, "Lib", "site-packages")) + ";" + binDir ); Environment.SetEnvironmentVariable("PYTHONVERBOSE", "1"); PythonEngine.Initialize(); PythonEngine.ImportModule("clr"); using (Py.GIL()) { PythonEngine.RunSimpleString( "import clr; " + "a = clr.AddReference('System'); " + "print(a.Location);" + "from System import Environment;" + "print(Environment.MachineName);"); } } } } //======================================================================= "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime" is a folder where I've copied the DLLs and Lib folder from a python 3.2 x86 distribution. When I run it from Visual Studio it works fine (I guess it may be related to the fact that I'm using python tools for Visual Studio). But when I run it from the console, it fails with the output: //======================================================================= Traceback (most recent call last): File "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", line 481, in execsitecustomize import sitecustomize UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character Traceback (most recent call last): File "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", line 497, in execusercustomize import usercustomize UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll Traceback (most recent call last): File "", line 1, in UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character //======================================================================= The "print(a.Location);" works but not the "from System import Environment". There are also all these errors about mbcs. Any idea on what I'm doing wrong? Thanks, Serge Weinstock ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tony at pyxll.com Tue Aug 19 18:15:38 2014 From: tony at pyxll.com (Tony Roberts) Date: Tue, 19 Aug 2014 17:15:38 +0100 Subject: [Python.NET] Running an embedded interpreter In-Reply-To: References: Message-ID: Hi Serge, 'mbcs' is what python uses to mean the current configured encoding. I would guess that the encoding of sys.stdout is different when using visual studio output console than the console. You could try a different encoding method by setting the PYTHONIOENCODING environment variable before starting your exe, eg: SET PYTHONIOENCODING=utf-8:ignore Look for PYTHONIOENCODING here https://docs.python.org/3/using/cmdline.html for more details. Tony On Tue, Aug 19, 2014 at 2:22 PM, Serge WEINSTOCK < serge.weinstock at uk.bnpparibas.com> wrote: > Hi, > > > > I?m trying to use Python3.2 using the Python.Net version found at: > https://github.com/renshawbay/pythonnet > > > > I?m using the following simple test program: > > > > //======================================================================= > > using System; > > using System.IO; > > using Python.Runtime; > > > > namespace TestPythonNet > > { > > class Program > > { > > static void Main(string[] args) > > { > > string binDir = > Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(Program)).Location); > > string pyHome = > @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; > > PythonEngine.PythonHome = > @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; > > PythonEngine.ProgramName = "PythonRuntime"; > > Environment.SetEnvironmentVariable("PYTHONPATH", > > Path.GetFullPath(Path.Combine(pyHome, "DLLs")) + ";" + > > Path.GetFullPath(Path.Combine(pyHome, "Lib")) + ";" + > > Path.GetFullPath(Path.Combine(pyHome, "Lib", > "site-packages")) + ";" + > > binDir > > ); > > Environment.SetEnvironmentVariable("PYTHONVERBOSE", "1"); > > PythonEngine.Initialize(); > > PythonEngine.ImportModule("clr"); > > using (Py.GIL()) > > { > > PythonEngine.RunSimpleString( > > "import clr; " + > > "a = clr.AddReference('System'); " + > > "print(a.Location);" + > > "from System import Environment;" + > > "print(Environment.MachineName);"); > > } > > } > > } > > } > > //======================================================================= > > > > ?D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime? is a folder > where I?ve copied the DLLs and Lib folder from a python 3.2 x86 > distribution. > > > > > > When I run it from Visual Studio it works fine (I guess it may be related > to the fact that I?m using python tools for Visual Studio). > > > > But when I run it from the console, it fails with the output: > > > > //======================================================================= > > Traceback (most recent call last): > > File > "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", > line 481, in execsitecustomize > > import sitecustomize > > UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: > invalid character > > Traceback (most recent call last): > > File > "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", > line 497, in execusercustomize > > import usercustomize > > UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: > invalid character > > > C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll > > Traceback (most recent call last): > > File "", line 1, in > > UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: > invalid character > > //======================================================================= > > > > The ?print(a.Location);" works but not the ?from System import > Environment?. There are also all these errors about mbcs. > > > > Any idea on what I?m doing wrong? > > > > Thanks, > > Serge Weinstock > > > > > ___________________________________________________________ > This e-mail may contain confidential and/or privileged information. If you > are not the intended recipient (or have received this e-mail in error) > please notify the sender immediately and delete this e-mail. Any > unauthorised copying, disclosure or distribution of the material in this > e-mail is prohibited. > > Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for > additional disclosures. > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > -------------- next part -------------- An HTML attachment was scrubbed... URL: From serge.weinstock at uk.bnpparibas.com Tue Aug 19 18:45:03 2014 From: serge.weinstock at uk.bnpparibas.com (Serge WEINSTOCK) Date: Tue, 19 Aug 2014 16:45:03 +0000 Subject: [Python.NET] Running an embedded interpreter In-Reply-To: References: Message-ID: Hi Tony, I?ve tried your suggestion but it doesn?t work. The issue seems to be more ?fundamental? as the import of the .Net System assembly doesn?t work. Serge From: pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] Sent: 19 August 2014 17:16 To: pythondotnet at python.org Subject: Re: [Python.NET] Running an embedded interpreter Hi Serge, 'mbcs' is what python uses to mean the current configured encoding. I would guess that the encoding of sys.stdout is different when using visual studio output console than the console. You could try a different encoding method by setting the PYTHONIOENCODING environment variable before starting your exe, eg: SET PYTHONIOENCODING=utf-8:ignore Look for PYTHONIOENCODING here https://docs.python.org/3/using/cmdline.html for more details. Tony On Tue, Aug 19, 2014 at 2:22 PM, Serge WEINSTOCK > wrote: Hi, I?m trying to use Python3.2 using the Python.Net version found at: https://github.com/renshawbay/pythonnet I?m using the following simple test program: //======================================================================= using System; using System.IO; using Python.Runtime; namespace TestPythonNet { class Program { static void Main(string[] args) { string binDir = Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(Program)).Location); string pyHome = @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; PythonEngine.PythonHome = @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; PythonEngine.ProgramName = "PythonRuntime"; Environment.SetEnvironmentVariable("PYTHONPATH", Path.GetFullPath(Path.Combine(pyHome, "DLLs")) + ";" + Path.GetFullPath(Path.Combine(pyHome, "Lib")) + ";" + Path.GetFullPath(Path.Combine(pyHome, "Lib", "site-packages")) + ";" + binDir ); Environment.SetEnvironmentVariable("PYTHONVERBOSE", "1"); PythonEngine.Initialize(); PythonEngine.ImportModule("clr"); using (Py.GIL()) { PythonEngine.RunSimpleString( "import clr; " + "a = clr.AddReference('System'); " + "print(a.Location);" + "from System import Environment;" + "print(Environment.MachineName);"); } } } } //======================================================================= ?D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime? is a folder where I?ve copied the DLLs and Lib folder from a python 3.2 x86 distribution. When I run it from Visual Studio it works fine (I guess it may be related to the fact that I?m using python tools for Visual Studio). But when I run it from the console, it fails with the output: //======================================================================= Traceback (most recent call last): File "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", line 481, in execsitecustomize import sitecustomize UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character Traceback (most recent call last): File "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", line 497, in execusercustomize import usercustomize UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll Traceback (most recent call last): File "", line 1, in UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character //======================================================================= The ?print(a.Location);" works but not the ?from System import Environment?. There are also all these errors about mbcs. Any idea on what I?m doing wrong? Thanks, Serge Weinstock ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. _________________________________________________ Python.NET mailing list - PythonDotNet at python.org https://mail.python.org/mailman/listinfo/pythondotnet ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. -------------- next part -------------- An HTML attachment was scrubbed... URL: From hpj at lodam.com Wed Aug 20 13:37:57 2014 From: hpj at lodam.com (Hans Peter Jepsen) Date: Wed, 20 Aug 2014 11:37:57 +0000 Subject: [Python.NET] Problem loading clr.pyd? Message-ID: <8BA14503034EAE47B93AE568C5A9B470470F4EAD@exch1srv.lodam.local> Hi I have a problem with python.net. I have with py2exe build a few programs, that uses Python.NET. I have tried to run the programs build on 3 PCs. On 2 of PCs the programs run without problems. These PCs have Windows 7 Professional with Service Pack 1 (64bit). The programs are built on of these machines. On the 3rd PC I get this error message: Unhandled Exception: System.BadImageFormatException: Could not load file or assembly 'clr.pyd' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded. File name: 'clr.pyd' This PC has Windows XP Professional with Service Pack 2. Highest version of installed Microsoft.NET framework is 3.5. Version of Python.Net is pythonnet-2.0-Beta0-clr4. Python version is 2.7.3. My questions now are: * What is the exact reason for the error message on 3rd PC? * Is the any way to solve this problem. * If I am using a too new version of Python.NET, is there a version, that would work on all 3 PC? I hope, that you can help me further? Kind regards Hans Peter -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.akhiyarov at gmail.com Wed Aug 20 17:33:22 2014 From: denis.akhiyarov at gmail.com (Denis Akhiyarov) Date: Wed, 20 Aug 2014 10:33:22 -0500 Subject: [Python.NET] Problem loading clr.pyd? In-Reply-To: <8BA14503034EAE47B93AE568C5A9B470470F4EAD@exch1srv.lodam.local> References: <8BA14503034EAE47B93AE568C5A9B470470F4EAD@exch1srv.lodam.local> Message-ID: probably you need CLR/.NET 4.0 On Wed, Aug 20, 2014 at 6:37 AM, Hans Peter Jepsen wrote: > Hi > > I have a problem with python.net. > > I have with py2exe build a few programs, that uses Python.NET. I have > tried to run the programs build on 3 PCs. > > On 2 of PCs the programs run without problems. These PCs have Windows 7 > Professional with Service Pack 1 (64bit). The programs are built on of > these machines. > > On the 3rd PC I get this error message: > > Unhandled Exception: System.BadImageFormatException: Could not load file > or assembly 'clr.pyd' or one of its dependencies. This assembly is built by > a runtime newer than the currently loaded runtime and cannot be loaded. > File name: 'clr.pyd' > > This PC has Windows XP Professional with Service Pack 2. Highest version > of installed Microsoft.NET framework is 3.5. > > Version of Python.Net is pythonnet-2.0-Beta0-clr4. Python version is > 2.7.3. > > My questions now are: > > - What is the exact reason for the error message on 3rd PC? > - Is the any way to solve this problem. > - If I am using a too new version of Python.NET, is there a version, > that would work on all 3 PC? > > > I hope, that you can help me further? > > > Kind regards > > Hans Peter > > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > -------------- next part -------------- An HTML attachment was scrubbed... URL: From siddhupiddu at gmail.com Wed Aug 20 19:21:45 2014 From: siddhupiddu at gmail.com (Mika S) Date: Wed, 20 Aug 2014 10:21:45 -0700 Subject: [Python.NET] converting double array to numpy list Message-ID: How do I pass a double array as a numpy list from C# (calling python code from c#) or passing something that can become a numpy list with minimal overhead. Right now, the naive way would be to create a new PyList. And to fill it with elements from the double array. But I read on a thread on this list ( https://mail.python.org/pipermail/pythondotnet/2014-May/001525.html) that Marshal.copy was faster. So, I tried Marshal.copy but I can't figure out how to instantiate a numpy array in python. Numpy can instantiate a new array from native C array by calling numpy.ctypeslib.as_array. So, theoretically if i can do Marshal.Copy(csharparray,0,addrofnative,csharparray.Length) and pass the addrofnative (which is a IntPtr) I could be done. But I can only pass PyObject when calling a module function. Any ideas ? -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeff at coderforlife.com Wed Aug 20 21:23:36 2014 From: jeff at coderforlife.com (Jeffrey Bush) Date: Wed, 20 Aug 2014 12:23:36 -0700 Subject: [Python.NET] converting double array to numpy list In-Reply-To: References: Message-ID: Numpy arrays are almost always created in Python and there are dozens of methods. That post you link to has the best solution for this case: import numpy as np dest = np.empty(len(src)) That creates a new numpy array of the same length of src with undefined ("empty") values at every index. You could substitute len(src) for whatever length you want (or a tuple if you want something not 1D). We use empty since we are about to set every value to something so we don't care that they have undefined values at that point. Overall I don't understand your problem, can you be more clear? I don't know if you are trying to do the copy in Python or C#, I am guessing you are copying from C# array to a new Python NumPy array, which is exactly what that post did (in Python). Jeff On Wed, Aug 20, 2014 at 10:21 AM, Mika S wrote: > How do I pass a double array as a numpy list from C# (calling python code > from c#) or passing something that can become a numpy list with minimal > overhead. > > Right now, the naive way would be to create a new PyList. And to fill it > with elements from the double array. > > But I read on a thread on this list ( > https://mail.python.org/pipermail/pythondotnet/2014-May/001525.html) that > Marshal.copy was faster. > So, I tried Marshal.copy but I can't figure out how to instantiate a numpy > array in python. Numpy can instantiate a new array from native C array by > calling numpy.ctypeslib.as_array. > So, theoretically if i can do > Marshal.Copy(csharparray,0,addrofnative,csharparray.Length) and pass the > addrofnative (which is a IntPtr) I could be done. But I can only pass > PyObject when calling a module function. > > Any ideas ? > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > -------------- next part -------------- An HTML attachment was scrubbed... URL: From siddhupiddu at gmail.com Wed Aug 20 23:45:25 2014 From: siddhupiddu at gmail.com (Mika S) Date: Wed, 20 Aug 2014 14:45:25 -0700 Subject: [Python.NET] converting double array to numpy list In-Reply-To: References: Message-ID: Thanks for replying. Here are some details that may be helpful in understanding the problem. I am using pythonnet to call python code from c#. This involves doing something of this sort: PythonEngine.Initialize(); PyObject testModule = PythonEngine.ImportModule("myModule"); var zero = new PyFloat(0.0); var Lock = PythonEngine.AcquireLock(); var f = testModule.InvokeMethod("myModule", new PyObject[1] { zero }); var g = (double)f[0].AsManagedObject(typeof(double)); PythonEngine.ReleaseLock(Lock); PythonEngine.Shutdown(); This passed a python float with a value of 0 to the python module. Now if my module, uses numpy arrays I need to pass at best a python list that can be converted to a numpy array. The python list can be created using new PyList and then assigning every double in the c# array into the pylist. This should be inefficient I presume. So one way that I thought was to marshal this into an unmanaged array and then pass the unmanaged array into python. The python program can use numpy.ctypeslib.as_array on the unmanaged array. Unfortunately, while calling python from c# I can only pass a pyobject array while calling invokemethod - so that won't work. In short: I want to call python code from a c# program. My python code uses numpy arrays. I have a multidimensional array of doubles in the c#program that I would like to pass to the python code. Basically, the linked post already had the c# array in python using the clr module. But in my case when I call python from c#, I do that using InvokeMethod that would only allow me to pass an array of PyObjects. So that's the challenge. On Wed, Aug 20, 2014 at 12:23 PM, Jeffrey Bush wrote: > Numpy arrays are almost always created in Python and there are dozens of > methods. That post you link to has the best solution for this case: > > import numpy as np > dest = np.empty(len(src)) > > That creates a new numpy array of the same length of src with > undefined ("empty") values at every index. You could substitute len(src) > for whatever length you want (or a tuple if you want something not 1D). We > use empty since we are about to set every value to something so we don't > care that they have undefined values at that point. > > Overall I don't understand your problem, can you be more clear? I don't > know if you are trying to do the copy in Python or C#, I am guessing you > are copying from C# array to a new Python NumPy array, which is exactly > what that post did (in Python). > > Jeff > > > On Wed, Aug 20, 2014 at 10:21 AM, Mika S wrote: > >> How do I pass a double array as a numpy list from C# (calling python code >> from c#) or passing something that can become a numpy list with minimal >> overhead. >> >> Right now, the naive way would be to create a new PyList. And to fill it >> with elements from the double array. >> >> But I read on a thread on this list ( >> https://mail.python.org/pipermail/pythondotnet/2014-May/001525.html) >> that Marshal.copy was faster. >> So, I tried Marshal.copy but I can't figure out how to instantiate a >> numpy array in python. Numpy can instantiate a new array from native C >> array by calling numpy.ctypeslib.as_array. >> So, theoretically if i can do >> Marshal.Copy(csharparray,0,addrofnative,csharparray.Length) and pass the >> addrofnative (which is a IntPtr) I could be done. But I can only pass >> PyObject when calling a module function. >> >> Any ideas ? >> >> _________________________________________________ >> Python.NET mailing list - PythonDotNet at python.org >> https://mail.python.org/mailman/listinfo/pythondotnet >> > > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > -------------- next part -------------- An HTML attachment was scrubbed... URL: From serge.weinstock at uk.bnpparibas.com Thu Aug 21 11:36:05 2014 From: serge.weinstock at uk.bnpparibas.com (Serge WEINSTOCK) Date: Thu, 21 Aug 2014 09:36:05 +0000 Subject: [Python.NET] Running an embedded interpreter References: Message-ID: Hi Tony, I?ve noticed that you are the main contributor for this branch of Python for .Net. Thanks a lot for that contribution. Maybe you can help me a little more with my issue. I think the main issue is due to the fact that I?m using Python 3.2. I?ve done the following tests: * Python 3.2 x86: * calling .Net libraries from standard python interpreter: works fine. * running embedded interpreter from .Net application: * from Visual Studio: works fine. Setting PYTHONHOME is enough. No need to set PYTHONPATH * from command line: doesn't work. Setting PYTHONPATH improves a little things. * Python 3.2 x64: * calling .Net libraries from standard python interpreter: works fine. * running embedded interpreter from .Net application: * from Visual Studio: doesn't work. Setting PYTHONPATH improves a little things. * from command line: doesn't work. Setting PYTHONPATH improves a little things. * Python 3.3 x86: * calling .Net libraries from standard python interpreter: works fine. * running embedded interpreter from .Net application: * from Visual Studio: works fine. * from command line: works fine. I've also compared for the VS run or for the command line run: * the paths given by 'sys.modules'. They are the same. * the paths of the loaded dlls as given by 'listdlls'. They are the same. Maybe you have a clue on why running an embedded interpreter works with 3.3 but not 3.3 Thanks, Serge From: Serge WEINSTOCK Sent: 19 August 2014 17:45 To: 'pythondotnet at python.org' Subject: RE: [Python.NET] Running an embedded interpreter Hi Tony, I?ve tried your suggestion but it doesn?t work. The issue seems to be more ?fundamental? as the import of the .Net System assembly doesn?t work. Serge From: pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] Sent: 19 August 2014 17:16 To: pythondotnet at python.org Subject: Re: [Python.NET] Running an embedded interpreter Hi Serge, 'mbcs' is what python uses to mean the current configured encoding. I would guess that the encoding of sys.stdout is different when using visual studio output console than the console. You could try a different encoding method by setting the PYTHONIOENCODING environment variable before starting your exe, eg: SET PYTHONIOENCODING=utf-8:ignore Look for PYTHONIOENCODING here?https://docs.python.org/3/using/cmdline.html for more details. Tony On Tue, Aug 19, 2014 at 2:22 PM, Serge WEINSTOCK wrote: Hi, ? I?m trying to use Python3.2 using the Python.Net version found at: https://github.com/renshawbay/pythonnet ? I?m using the following simple test program: ? //======================================================================= using System; using System.IO; using Python.Runtime; ? namespace TestPythonNet { ??? class Program ??? { ??????? static void Main(string[] args) ??????? { ??????????? string binDir = Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(Program)).Location); ??????????? string pyHome = @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; ??????????? PythonEngine.PythonHome = @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; ??????????? PythonEngine.ProgramName = "PythonRuntime"; ??????????? Environment.SetEnvironmentVariable("PYTHONPATH", ??????????????? Path.GetFullPath(Path.Combine(pyHome, "DLLs")) + ";" + ??????????????? Path.GetFullPath(Path.Combine(pyHome, "Lib")) + ";" + ??????????????? Path.GetFullPath(Path.Combine(pyHome, "Lib", "site-packages")) + ";" + ??????????????? binDir ??????????????? ); ??????????? Environment.SetEnvironmentVariable("PYTHONVERBOSE", "1"); ??????????? PythonEngine.Initialize(); ??????????? PythonEngine.ImportModule("clr"); ??????????? using (Py.GIL()) ??????????? { ??????????????? PythonEngine.RunSimpleString( ??????????????????? "import clr; " + ???????????????????"a = clr.AddReference('System'); " + ??????????????????? "print(a.Location);" + ??????????????????? "from System import Environment;" + ??????????????????? "print(Environment.MachineName);"); ??????????? } ??????? } ??? } } //======================================================================= ? ?D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime? is a folder where I?ve copied the DLLs and Lib folder from a python 3.2 x86 distribution. ? ? When I run it from Visual Studio it works fine (I guess it may be related to the fact that I?m using python tools for Visual Studio). ? But when I run it from the console, it fails with the output: ? //======================================================================= Traceback (most recent call last): ? File "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", line 481, in execsitecustomize ??? import sitecustomize UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character Traceback (most recent call last): ? File "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", line 497, in execusercustomize ??? import usercustomize UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll Traceback (most recent call last): ? File "", line 1, in UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character //======================================================================= ? The ?print(a.Location);" works but not the ?from System import Environment?. There are also all these errors about mbcs. ? Any idea on what I?m doing wrong? ? Thanks, Serge Weinstock ? ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. _________________________________________________ Python.NET mailing list - PythonDotNet at python.org https://mail.python.org/mailman/listinfo/pythondotnet ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. From tony at pyxll.com Thu Aug 21 15:43:27 2014 From: tony at pyxll.com (Tony Roberts) Date: Thu, 21 Aug 2014 14:43:27 +0100 Subject: [Python.NET] Running an embedded interpreter In-Reply-To: References: Message-ID: Hi Serge, sorry, not sure why one would work and not the other. For what it's worth, I've been using the 3.2 x64 version for some time now, both for calling .NET from python and for embedding Python into a .NET application without any problem like the ones you describe. What I suggest you try is grabbing the latest code from the renshawbay repo and build that using setupwin.py - you might want to edit that file to build the debug project. Then you will be able to step through and see exactly where it's going wrong. You can build it from visual studio if you prefer, but you will have to be careful to set some of the defines correctly; look at setupwin.py to see what needs setting. You can also download the pdb files and python source from python.org, which should allow you step into the python source code without having to build python yourself. Best regards, Tony On Thu, Aug 21, 2014 at 10:36 AM, Serge WEINSTOCK < serge.weinstock at uk.bnpparibas.com> wrote: > Hi Tony, > > I?ve noticed that you are the main contributor for this branch of Python > for .Net. Thanks a lot for that contribution. > > Maybe you can help me a little more with my issue. I think the main issue > is due to the fact that I?m using Python 3.2. > > I?ve done the following tests: > * Python 3.2 x86: > * calling .Net libraries from standard python interpreter: works fine. > * running embedded interpreter from .Net application: > * from Visual Studio: works fine. Setting PYTHONHOME is enough. > No need to set PYTHONPATH > * from command line: doesn't work. Setting PYTHONPATH improves a > little things. > * Python 3.2 x64: > * calling .Net libraries from standard python interpreter: works fine. > * running embedded interpreter from .Net application: > * from Visual Studio: doesn't work. Setting PYTHONPATH improves > a little things. > * from command line: doesn't work. Setting PYTHONPATH improves a > little things. > * Python 3.3 x86: > * calling .Net libraries from standard python interpreter: works fine. > * running embedded interpreter from .Net application: > * from Visual Studio: works fine. > * from command line: works fine. > > I've also compared for the VS run or for the command line run: > * the paths given by 'sys.modules'. They are the same. > * the paths of the loaded dlls as given by 'listdlls'. They are the same. > > Maybe you have a clue on why running an embedded interpreter works with > 3.3 but not 3.3 > > Thanks, > Serge > > > From: Serge WEINSTOCK > Sent: 19 August 2014 17:45 > To: 'pythondotnet at python.org' > Subject: RE: [Python.NET] Running an embedded interpreter > > Hi Tony, > > I?ve tried your suggestion but it doesn?t work. > > The issue seems to be more ?fundamental? as the import of the .Net System > assembly doesn?t work. > > Serge > > From: pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org > [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] > Sent: 19 August 2014 17:16 > To: pythondotnet at python.org > Subject: Re: [Python.NET] Running an embedded interpreter > > Hi Serge, > > 'mbcs' is what python uses to mean the current configured encoding. I > would guess that the encoding of sys.stdout is different when using visual > studio output console than the console. > > You could try a different encoding method by setting the PYTHONIOENCODING > environment variable before starting your exe, eg: > SET PYTHONIOENCODING=utf-8:ignore > > Look for PYTHONIOENCODING here > https://docs.python.org/3/using/cmdline.html for more details. > > Tony > > > > On Tue, Aug 19, 2014 at 2:22 PM, Serge WEINSTOCK < > serge.weinstock at uk.bnpparibas.com> wrote: > Hi, > > I?m trying to use Python3.2 using the Python.Net version found at: > https://github.com/renshawbay/pythonnet > > I?m using the following simple test program: > > //======================================================================= > using System; > using System.IO; > using Python.Runtime; > > namespace TestPythonNet > { > class Program > { > static void Main(string[] args) > { > string binDir = > Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(Program)).Location); > string pyHome = > @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; > PythonEngine.PythonHome = > @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; > PythonEngine.ProgramName = "PythonRuntime"; > Environment.SetEnvironmentVariable("PYTHONPATH", > Path.GetFullPath(Path.Combine(pyHome, "DLLs")) + ";" + > Path.GetFullPath(Path.Combine(pyHome, "Lib")) + ";" + > Path.GetFullPath(Path.Combine(pyHome, "Lib", > "site-packages")) + ";" + > binDir > ); > Environment.SetEnvironmentVariable("PYTHONVERBOSE", "1"); > PythonEngine.Initialize(); > PythonEngine.ImportModule("clr"); > using (Py.GIL()) > { > PythonEngine.RunSimpleString( > "import clr; " + > "a = clr.AddReference('System'); " + > "print(a.Location);" + > "from System import Environment;" + > "print(Environment.MachineName);"); > } > } > } > } > //======================================================================= > > ?D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime? is a folder > where I?ve copied the DLLs and Lib folder from a python 3.2 x86 > distribution. > > > When I run it from Visual Studio it works fine (I guess it may be related > to the fact that I?m using python tools for Visual Studio). > > But when I run it from the console, it fails with the output: > > //======================================================================= > Traceback (most recent call last): > File > "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", > line 481, in execsitecustomize > import sitecustomize > UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: > invalid character > Traceback (most recent call last): > File > "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", > line 497, in execusercustomize > import usercustomize > UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: > invalid character > > C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll > Traceback (most recent call last): > File "", line 1, in > UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: > invalid character > //======================================================================= > > The ?print(a.Location);" works but not the ?from System import > Environment?. There are also all these errors about mbcs. > > Any idea on what I?m doing wrong? > > Thanks, > Serge Weinstock > > > ___________________________________________________________ > This e-mail may contain confidential and/or privileged information. If you > are not the intended recipient (or have received this e-mail in error) > please notify the sender immediately and delete this e-mail. Any > unauthorised copying, disclosure or distribution of the material in this > e-mail is prohibited. > Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for > additional disclosures. > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > > > ___________________________________________________________ > This e-mail may contain confidential and/or privileged information. If you > are not the intended recipient (or have received this e-mail in error) > please notify the sender immediately and delete this e-mail. Any > unauthorised copying, disclosure or distribution of the material in this > e-mail is prohibited. > > Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for > additional disclosures. > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeff at coderforlife.com Fri Aug 22 01:04:19 2014 From: jeff at coderforlife.com (Jeffrey Bush) Date: Thu, 21 Aug 2014 16:04:19 -0700 Subject: [Python.NET] converting double array to numpy list In-Reply-To: References: Message-ID: Hi, So I have never used Python from C# (only the other way) however here are my ideas: 1. numpy.ctypeslib.as_array is dangerous in this situation, you can only use it safely on a pinned pointer, and as long as the returned array exists you should not unpin that pointer, and pinned pointers should not be kept around for very long. However, if you are using it quickly this is fine. 2. IntPtr is just a wrapper for a C# int or long, you can call the ToInt64() function on it to get the value, and a long should easily be sendable to Python. The process would likely be the following (untested code): C#: var module = ...; double[,] array = new double[,] { { ... } }; GCHandle pin = GCHandle.Alloc(array, GCHandleType.Pinned); try: long ptr = pin.AddrOfPinnedObject().ToInt64(); var output = module.InvokeMethod("func", new PyObject[3] { new PyLong(ptr), new PyInt(array.GetLength(0)), new PyInt(array.GetLength(1)) }); finally: if (pin.IsAllocated) { pin.Free(); } // work with output here Python: def func(ptr, rows, cols): array = numpy.ctypeslib.as_array(ctypes.POINTER(ctypes.c_double).from_address(ptr), (rows, cols)) # operate on array - can read and write, changing the C# array if you write output = numpy.sum(array) # for example # after we return from this function, we can NEVER touch this 'array' again unless we copy the data in it return output Jeff On Wed, Aug 20, 2014 at 2:45 PM, Mika S wrote: > Thanks for replying. Here are some details that may be helpful in > understanding the problem. > > I am using pythonnet to call python code from c#. This involves doing > something of this sort: > > PythonEngine.Initialize(); > PyObject testModule = PythonEngine.ImportModule("myModule"); > > var zero = new PyFloat(0.0); > var Lock = PythonEngine.AcquireLock(); > var f = testModule.InvokeMethod("myModule", new PyObject[1] { > zero }); > var g = (double)f[0].AsManagedObject(typeof(double)); > PythonEngine.ReleaseLock(Lock); > PythonEngine.Shutdown(); > > This passed a python float with a value of 0 to the python module. Now if > my module, uses numpy arrays I need to pass at best a python list that can > be converted to a numpy array. The python list can be created using new > PyList and then assigning every double in the c# array into the pylist. > This should be inefficient I presume. So one way that I thought was to > marshal this into an unmanaged array and then pass the unmanaged array into > python. The python program can use numpy.ctypeslib.as_array on the > unmanaged array. Unfortunately, while calling python from c# I can only > pass a pyobject array while calling invokemethod - so that won't work. > > In short: I want to call python code from a c# program. My python code > uses numpy arrays. I have a multidimensional array of doubles in the > c#program that I would like to pass to the python code. > > Basically, the linked post already had the c# array in python using the > clr module. But in my case when I call python from c#, I do that using > InvokeMethod that would only allow me to pass an array of PyObjects. So > that's the challenge. > > > > On Wed, Aug 20, 2014 at 12:23 PM, Jeffrey Bush > wrote: > >> Numpy arrays are almost always created in Python and there are dozens of >> methods. That post you link to has the best solution for this case: >> >> import numpy as np >> dest = np.empty(len(src)) >> >> That creates a new numpy array of the same length of src with >> undefined ("empty") values at every index. You could substitute len(src) >> for whatever length you want (or a tuple if you want something not 1D). We >> use empty since we are about to set every value to something so we don't >> care that they have undefined values at that point. >> >> Overall I don't understand your problem, can you be more clear? I don't >> know if you are trying to do the copy in Python or C#, I am guessing you >> are copying from C# array to a new Python NumPy array, which is exactly >> what that post did (in Python). >> >> Jeff >> >> >> On Wed, Aug 20, 2014 at 10:21 AM, Mika S wrote: >> >>> How do I pass a double array as a numpy list from C# (calling python >>> code from c#) or passing something that can become a numpy list with >>> minimal overhead. >>> >>> Right now, the naive way would be to create a new PyList. And to fill it >>> with elements from the double array. >>> >>> But I read on a thread on this list ( >>> https://mail.python.org/pipermail/pythondotnet/2014-May/001525.html) >>> that Marshal.copy was faster. >>> So, I tried Marshal.copy but I can't figure out how to instantiate a >>> numpy array in python. Numpy can instantiate a new array from native C >>> array by calling numpy.ctypeslib.as_array. >>> So, theoretically if i can do >>> Marshal.Copy(csharparray,0,addrofnative,csharparray.Length) and pass the >>> addrofnative (which is a IntPtr) I could be done. But I can only pass >>> PyObject when calling a module function. >>> >>> Any ideas ? >>> >>> _________________________________________________ >>> Python.NET mailing list - PythonDotNet at python.org >>> https://mail.python.org/mailman/listinfo/pythondotnet >>> >> >> >> _________________________________________________ >> Python.NET mailing list - PythonDotNet at python.org >> https://mail.python.org/mailman/listinfo/pythondotnet >> > > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > -------------- next part -------------- An HTML attachment was scrubbed... URL: From daverz at gmail.com Sat Aug 23 16:28:27 2014 From: daverz at gmail.com (Dave Cook) Date: Sat, 23 Aug 2014 07:28:27 -0700 Subject: [Python.NET] converting double array to numpy list In-Reply-To: References: Message-ID: On Wed, Aug 20, 2014 at 10:21 AM, Mika S wrote: How do I pass a double array as a numpy list from C# (calling python code > from c#) or passing something that can become a numpy list with minimal > overhead. > > Based on input from an earlier thread here, I settled on using Marshal.Copy: def ndarray_ptr(a): '''Return a .NET pointer to the underlying ndarray data.''' return IntPtr.__overloads__[int](a.__array_interface__['data'][0])def marshal_to_ndarray(src): n = len(src) dest = np.empty(n, dtype='f8') Marshal.Copy(src, 0, ndarray_ptr(dest), n) return dest Dave Cook -------------- next part -------------- An HTML attachment was scrubbed... URL: From serge.weinstock at uk.bnpparibas.com Tue Aug 26 02:07:17 2014 From: serge.weinstock at uk.bnpparibas.com (Serge WEINSTOCK) Date: Tue, 26 Aug 2014 00:07:17 +0000 Subject: [Python.NET] Running an embedded interpreter In-Reply-To: References: Message-ID: Hi Tony, I think I've found the issue: The signatures of Py_SetPythonHome, Py_GetPythonHome, Py_SetProgramName and Py_GetProgramName have changed from python 2.x to python 3.x: the strings are now Unicode strings. I've done the following patches (I've also added support for Py_SetPath and Py_GetPath as I needed to set up myself sys.path): * runtime.cs //================================================================== #if PYTHON32 || PYTHON33 || PYTHON34 [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] [return: MarshalAs(UnmanagedType.LPWStr)] internal unsafe static extern string Py_GetProgramName(); [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] internal unsafe static extern void Py_SetProgramName([MarshalAsAttribute(UnmanagedType.LPWStr)]string name); [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] [return: MarshalAs(UnmanagedType.LPWStr)] internal unsafe static extern string Py_GetPythonHome(); [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] internal unsafe static extern void Py_SetPythonHome([MarshalAsAttribute(UnmanagedType.LPWStr)]string home); [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] [return: MarshalAs(UnmanagedType.LPWStr)] internal unsafe static extern string Py_GetPath(); [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] internal unsafe static extern void Py_SetPath([MarshalAsAttribute(UnmanagedType.LPWStr)]string home); #else [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern string Py_GetProgramName(); [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern void Py_SetProgramName(string name); [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern string Py_GetPythonHome(); [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern void Py_SetPythonHome(string home); [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern string Py_GetPath(); [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern void Py_SetPath(string home); #endif //================================================================== PythonEngine.cs //================================================================== public static string PythonPath { get { string result = Runtime.Py_GetPath(); if (result == null) { return ""; } return result; } set { Runtime.Py_SetPath(value); } } //================================================================== Could you add these patches to the source repository? Thanks, Serge From: pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] Sent: 21 August 2014 14:43 To: pythondotnet at python.org Subject: Re: [Python.NET] Running an embedded interpreter Hi Serge, sorry, not sure why one would work and not the other. For what it's worth, I've been using the 3.2 x64 version for some time now, both for calling .NET from python and for embedding Python into a .NET application without any problem like the ones you describe. What I suggest you try is grabbing the latest code from the renshawbay repo and build that using setupwin.py - you might want to edit that file to build the debug project. Then you will be able to step through and see exactly where it's going wrong. You can build it from visual studio if you prefer, but you will have to be careful to set some of the defines correctly; look at setupwin.py to see what needs setting. You can also download the pdb files and python source from python.org, which should allow you step into the python source code without having to build python yourself. Best regards, Tony On Thu, Aug 21, 2014 at 10:36 AM, Serge WEINSTOCK wrote: Hi Tony, I?ve noticed that you are the main contributor for this branch of Python for .Net. Thanks a lot for that contribution. Maybe you can help me a little more with my issue. I think the main issue is due to the fact that I?m using Python 3.2. I?ve done the following tests: *? Python 3.2 x86: ? ? * calling .Net libraries from standard python interpreter: works fine. ? ? * running embedded interpreter from .Net application: ? ? ? ? ? * from Visual Studio: works fine. Setting PYTHONHOME is enough. No need to set PYTHONPATH ? ? ? ? ? * from command line: doesn't work. Setting PYTHONPATH improves a little things. *? Python 3.2 x64: ? ? * calling .Net libraries from standard python interpreter: works fine. ? ? * running embedded interpreter from .Net application: ? ? ? ? ? * from Visual Studio: doesn't work. Setting PYTHONPATH improves a little things. ? ? ? ? ? * from command line: doesn't work. Setting PYTHONPATH improves a little things. *? Python 3.3 x86: ? ? * calling .Net libraries from standard python interpreter: works fine. ? ? * running embedded interpreter from .Net application: ? ? ? ? ? * from Visual Studio: works fine. ? ? ? ? ? * from command line: works fine. I've also compared for the VS run or for the command line run: ?* the paths given by 'sys.modules'.? They are the same. ?* the paths of the loaded dlls as given by 'listdlls'. They are the same. Maybe you have a clue on why running an embedded interpreter works with 3.3 but not 3.3 Thanks, Serge From: Serge WEINSTOCK Sent: 19 August 2014 17:45 To: 'pythondotnet at python.org' Subject: RE: [Python.NET] Running an embedded interpreter Hi Tony, I?ve tried your suggestion but it doesn?t work. The issue seems to be more ?fundamental? as the import of the .Net System assembly doesn?t work. Serge From: pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] Sent: 19 August 2014 17:16 To: pythondotnet at python.org Subject: Re: [Python.NET] Running an embedded interpreter Hi Serge, 'mbcs' is what python uses to mean the current configured encoding. I would guess that the encoding of sys.stdout is different when using visual studio output console than the console. You could try a different encoding method by setting the PYTHONIOENCODING environment variable before starting your exe, eg: SET PYTHONIOENCODING=utf-8:ignore Look for PYTHONIOENCODING here?https://docs.python.org/3/using/cmdline.html for more details. Tony On Tue, Aug 19, 2014 at 2:22 PM, Serge WEINSTOCK wrote: Hi, ? I?m trying to use Python3.2 using the Python.Net version found at: https://github.com/renshawbay/pythonnet ? I?m using the following simple test program: ? //======================================================================= using System; using System.IO; using Python.Runtime; ? namespace TestPythonNet { ??? class Program ??? { ??????? static void Main(string[] args) ??????? { ??????????? string binDir = Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(Program)).Location); ??????????? string pyHome = @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; ??????????? PythonEngine.PythonHome = @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; ??????????? PythonEngine.ProgramName = "PythonRuntime"; ??????????? Environment.SetEnvironmentVariable("PYTHONPATH", ??????????????? Path.GetFullPath(Path.Combine(pyHome, "DLLs")) + ";" + ??????????????? Path.GetFullPath(Path.Combine(pyHome, "Lib")) + ";" + ??????????????? Path.GetFullPath(Path.Combine(pyHome, "Lib", "site-packages")) + ";" + ??????????????? binDir ??????????????? ); ??????????? Environment.SetEnvironmentVariable("PYTHONVERBOSE", "1"); ??????????? PythonEngine.Initialize(); ??????????? PythonEngine.ImportModule("clr"); ??????????? using (Py.GIL()) ??????????? { ??????????????? PythonEngine.RunSimpleString( ??????????????????? "import clr; " + ???????????????????"a = clr.AddReference('System'); " + ??????????????????? "print(a.Location);" + ??????????????????? "from System import Environment;" + ??????????????????? "print(Environment.MachineName);"); ??????????? } ??????? } ??? } } //======================================================================= ? ?D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime? is a folder where I?ve copied the DLLs and Lib folder from a python 3.2 x86 distribution. ? ? When I run it from Visual Studio it works fine (I guess it may be related to the fact that I?m using python tools for Visual Studio). ? But when I run it from the console, it fails with the output: ? //======================================================================= Traceback (most recent call last): ? File "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", line 481, in execsitecustomize ??? import sitecustomize UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character Traceback (most recent call last): ? File "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", line 497, in execusercustomize ??? import usercustomize UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll Traceback (most recent call last): ? File "", line 1, in UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character //======================================================================= ? The ?print(a.Location);" works but not the ?from System import Environment?. There are also all these errors about mbcs. ? Any idea on what I?m doing wrong? ? Thanks, Serge Weinstock ? ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. _________________________________________________ Python.NET mailing list - PythonDotNet at python.org https://mail.python.org/mailman/listinfo/pythondotnet ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. _________________________________________________ Python.NET mailing list - PythonDotNet at python.org https://mail.python.org/mailman/listinfo/pythondotnet ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. From tony at pyxll.com Tue Aug 26 17:53:49 2014 From: tony at pyxll.com (Tony Roberts) Date: Tue, 26 Aug 2014 16:53:49 +0100 Subject: [Python.NET] Running an embedded interpreter In-Reply-To: References: Message-ID: Hi Serge, ah great, good spot. Certainly, please send a pull request to the github repo and I'll merge it. thanks, Tony On Tue, Aug 26, 2014 at 1:07 AM, Serge WEINSTOCK < serge.weinstock at uk.bnpparibas.com> wrote: > Hi Tony, > > I think I've found the issue: > The signatures of Py_SetPythonHome, Py_GetPythonHome, Py_SetProgramName > and Py_GetProgramName have changed from python 2.x to python 3.x: the > strings are now Unicode strings. > > I've done the following patches (I've also added support for Py_SetPath > and Py_GetPath as I needed to set up myself sys.path): > * runtime.cs > //================================================================== > #if PYTHON32 || PYTHON33 || PYTHON34 > [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, > ExactSpelling=true, CharSet=CharSet.Ansi)] > [return: MarshalAs(UnmanagedType.LPWStr)] > internal unsafe static extern string > Py_GetProgramName(); > > [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, > ExactSpelling=true, CharSet=CharSet.Ansi)] > internal unsafe static extern void > Py_SetProgramName([MarshalAsAttribute(UnmanagedType.LPWStr)]string > name); > > [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, > ExactSpelling=true, CharSet=CharSet.Ansi)] > [return: MarshalAs(UnmanagedType.LPWStr)] > internal unsafe static extern string > Py_GetPythonHome(); > > [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, > ExactSpelling=true, CharSet=CharSet.Ansi)] > internal unsafe static extern void > Py_SetPythonHome([MarshalAsAttribute(UnmanagedType.LPWStr)]string > home); > > [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, > ExactSpelling=true, CharSet=CharSet.Ansi)] > [return: MarshalAs(UnmanagedType.LPWStr)] > internal unsafe static extern string > Py_GetPath(); > > [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, > ExactSpelling=true, CharSet=CharSet.Ansi)] > internal unsafe static extern void > Py_SetPath([MarshalAsAttribute(UnmanagedType.LPWStr)]string home); > #else > [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, > ExactSpelling = true, CharSet = CharSet.Ansi)] > internal unsafe static extern string > Py_GetProgramName(); > > [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, > ExactSpelling = true, CharSet = CharSet.Ansi)] > internal unsafe static extern void > Py_SetProgramName(string name); > > [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, > ExactSpelling = true, CharSet = CharSet.Ansi)] > internal unsafe static extern string > Py_GetPythonHome(); > > [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, > ExactSpelling = true, CharSet = CharSet.Ansi)] > internal unsafe static extern void > Py_SetPythonHome(string home); > > [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, > ExactSpelling = true, CharSet = CharSet.Ansi)] > internal unsafe static extern string > Py_GetPath(); > > [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, > ExactSpelling = true, CharSet = CharSet.Ansi)] > internal unsafe static extern void > Py_SetPath(string home); > #endif > //================================================================== > PythonEngine.cs > //================================================================== > public static string PythonPath { > get > { > string result = Runtime.Py_GetPath(); > if (result == null) > { > return ""; > } > return result; > } > set > { > Runtime.Py_SetPath(value); > } > } > //================================================================== > > Could you add these patches to the source repository? > > Thanks, > Serge > > > > From: pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org > [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] > Sent: 21 August 2014 14:43 > To: pythondotnet at python.org > Subject: Re: [Python.NET] Running an embedded interpreter > > Hi Serge, > > sorry, not sure why one would work and not the other. For what it's worth, > I've been using the 3.2 x64 version for some time now, both for calling > .NET from python and for embedding Python into a .NET application without > any problem like the ones you describe. > > What I suggest you try is grabbing the latest code from the renshawbay > repo and build that using setupwin.py - you might want to edit that file to > build the debug project. Then you will be able to step through and see > exactly where it's going wrong. You can build it from visual studio if you > prefer, but you will have to be careful to set some of the defines > correctly; look at setupwin.py to see what needs setting. > > You can also download the pdb files and python source from python.org, > which should allow you step into the python source code without having to > build python yourself. > > Best regards, > Tony > > > On Thu, Aug 21, 2014 at 10:36 AM, Serge WEINSTOCK < > serge.weinstock at uk.bnpparibas.com> wrote: > Hi Tony, > > I?ve noticed that you are the main contributor for this branch of Python > for .Net. Thanks a lot for that contribution. > > Maybe you can help me a little more with my issue. I think the main issue > is due to the fact that I?m using Python 3.2. > > I?ve done the following tests: > * Python 3.2 x86: > * calling .Net libraries from standard python interpreter: works fine. > * running embedded interpreter from .Net application: > * from Visual Studio: works fine. Setting PYTHONHOME is enough. > No need to set PYTHONPATH > * from command line: doesn't work. Setting PYTHONPATH improves a > little things. > * Python 3.2 x64: > * calling .Net libraries from standard python interpreter: works fine. > * running embedded interpreter from .Net application: > * from Visual Studio: doesn't work. Setting PYTHONPATH improves > a little things. > * from command line: doesn't work. Setting PYTHONPATH improves a > little things. > * Python 3.3 x86: > * calling .Net libraries from standard python interpreter: works fine. > * running embedded interpreter from .Net application: > * from Visual Studio: works fine. > * from command line: works fine. > > I've also compared for the VS run or for the command line run: > * the paths given by 'sys.modules'. They are the same. > * the paths of the loaded dlls as given by 'listdlls'. They are the same. > > Maybe you have a clue on why running an embedded interpreter works with > 3.3 but not 3.3 > > Thanks, > Serge > > > From: Serge WEINSTOCK > Sent: 19 August 2014 17:45 > To: 'pythondotnet at python.org' > Subject: RE: [Python.NET] Running an embedded interpreter > > Hi Tony, > > I?ve tried your suggestion but it doesn?t work. > > The issue seems to be more ?fundamental? as the import of the .Net System > assembly doesn?t work. > > Serge > > From: pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org > [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] > Sent: 19 August 2014 17:16 > To: pythondotnet at python.org > Subject: Re: [Python.NET] Running an embedded interpreter > Hi Serge, > > 'mbcs' is what python uses to mean the current configured encoding. I > would guess that the encoding of sys.stdout is different when using visual > studio output console than the console. > > You could try a different encoding method by setting the PYTHONIOENCODING > environment variable before starting your exe, eg: > SET PYTHONIOENCODING=utf-8:ignore > > Look for PYTHONIOENCODING here > https://docs.python.org/3/using/cmdline.html for more details. > > Tony > > > > On Tue, Aug 19, 2014 at 2:22 PM, Serge WEINSTOCK < > serge.weinstock at uk.bnpparibas.com> wrote: > Hi, > > I?m trying to use Python3.2 using the Python.Net version found at: > https://github.com/renshawbay/pythonnet > > I?m using the following simple test program: > > //======================================================================= > using System; > using System.IO; > using Python.Runtime; > > namespace TestPythonNet > { > class Program > { > static void Main(string[] args) > { > string binDir = > Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(Program)).Location); > string pyHome = > @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; > PythonEngine.PythonHome = > @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; > PythonEngine.ProgramName = "PythonRuntime"; > Environment.SetEnvironmentVariable("PYTHONPATH", > Path.GetFullPath(Path.Combine(pyHome, "DLLs")) + ";" + > Path.GetFullPath(Path.Combine(pyHome, "Lib")) + ";" + > Path.GetFullPath(Path.Combine(pyHome, "Lib", > "site-packages")) + ";" + > binDir > ); > Environment.SetEnvironmentVariable("PYTHONVERBOSE", "1"); > PythonEngine.Initialize(); > PythonEngine.ImportModule("clr"); > using (Py.GIL()) > { > PythonEngine.RunSimpleString( > "import clr; " + > "a = clr.AddReference('System'); " + > "print(a.Location);" + > "from System import Environment;" + > "print(Environment.MachineName);"); > } > } > } > } > //======================================================================= > > ?D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime? is a folder > where I?ve copied the DLLs and Lib folder from a python 3.2 x86 > distribution. > > > When I run it from Visual Studio it works fine (I guess it may be related > to the fact that I?m using python tools for Visual Studio). > > But when I run it from the console, it fails with the output: > > //======================================================================= > Traceback (most recent call last): > File > "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", > line 481, in execsitecustomize > import sitecustomize > UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: > invalid character > Traceback (most recent call last): > File > "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", > line 497, in execusercustomize > import usercustomize > UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: > invalid character > > C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll > Traceback (most recent call last): > File "", line 1, in > UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: > invalid character > //======================================================================= > > The ?print(a.Location);" works but not the ?from System import > Environment?. There are also all these errors about mbcs. > > Any idea on what I?m doing wrong? > > Thanks, > Serge Weinstock > > > ___________________________________________________________ > This e-mail may contain confidential and/or privileged information. If you > are not the intended recipient (or have received this e-mail in error) > please notify the sender immediately and delete this e-mail. Any > unauthorised copying, disclosure or distribution of the material in this > e-mail is prohibited. > Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for > additional disclosures. > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > > > ___________________________________________________________ > This e-mail may contain confidential and/or privileged information. If you > are not the intended recipient (or have received this e-mail in error) > please notify the sender immediately and delete this e-mail. Any > unauthorised copying, disclosure or distribution of the material in this > e-mail is prohibited. > > Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for > additional disclosures. > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > > > ___________________________________________________________ > This e-mail may contain confidential and/or privileged information. If you > are not the intended recipient (or have received this e-mail in error) > please notify the sender immediately and delete this e-mail. Any > unauthorised copying, disclosure or distribution of the material in this > e-mail is prohibited. > > Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for > additional disclosures. > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > -------------- next part -------------- An HTML attachment was scrubbed... URL: From serge.weinstock at uk.bnpparibas.com Wed Aug 27 15:02:03 2014 From: serge.weinstock at uk.bnpparibas.com (Serge WEINSTOCK) Date: Wed, 27 Aug 2014 13:02:03 +0000 Subject: [Python.NET] Running an embedded interpreter In-Reply-To: References: Message-ID: Hi Tony, I?ve submitted the fix. As I?m new to git and github, I?m not sure I?ve done the right steps. Let me know if you can?t find my patch Thanks, Serge From: pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] Sent: 26 August 2014 16:54 To: pythondotnet at python.org Subject: Re: [Python.NET] Running an embedded interpreter Hi Serge, ah great, good spot. Certainly, please send a pull request to the github repo and I'll merge it. thanks, Tony On Tue, Aug 26, 2014 at 1:07 AM, Serge WEINSTOCK > wrote: Hi Tony, I think I've found the issue: The signatures of Py_SetPythonHome, Py_GetPythonHome, Py_SetProgramName and Py_GetProgramName have changed from python 2.x to python 3.x: the strings are now Unicode strings. I've done the following patches (I've also added support for Py_SetPath and Py_GetPath as I needed to set up myself sys.path): * runtime.cs //================================================================== #if PYTHON32 || PYTHON33 || PYTHON34 [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] [return: MarshalAs(UnmanagedType.LPWStr)] internal unsafe static extern string Py_GetProgramName(); [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] internal unsafe static extern void Py_SetProgramName([MarshalAsAttribute(UnmanagedType.LPWStr)]string name); [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] [return: MarshalAs(UnmanagedType.LPWStr)] internal unsafe static extern string Py_GetPythonHome(); [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] internal unsafe static extern void Py_SetPythonHome([MarshalAsAttribute(UnmanagedType.LPWStr)]string home); [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] [return: MarshalAs(UnmanagedType.LPWStr)] internal unsafe static extern string Py_GetPath(); [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] internal unsafe static extern void Py_SetPath([MarshalAsAttribute(UnmanagedType.LPWStr)]string home); #else [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern string Py_GetProgramName(); [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern void Py_SetProgramName(string name); [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern string Py_GetPythonHome(); [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern void Py_SetPythonHome(string home); [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern string Py_GetPath(); [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern void Py_SetPath(string home); #endif //================================================================== PythonEngine.cs //================================================================== public static string PythonPath { get { string result = Runtime.Py_GetPath(); if (result == null) { return ""; } return result; } set { Runtime.Py_SetPath(value); } } //================================================================== Could you add these patches to the source repository? Thanks, Serge From: pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] Sent: 21 August 2014 14:43 To: pythondotnet at python.org Subject: Re: [Python.NET] Running an embedded interpreter Hi Serge, sorry, not sure why one would work and not the other. For what it's worth, I've been using the 3.2 x64 version for some time now, both for calling .NET from python and for embedding Python into a .NET application without any problem like the ones you describe. What I suggest you try is grabbing the latest code from the renshawbay repo and build that using setupwin.py - you might want to edit that file to build the debug project. Then you will be able to step through and see exactly where it's going wrong. You can build it from visual studio if you prefer, but you will have to be careful to set some of the defines correctly; look at setupwin.py to see what needs setting. You can also download the pdb files and python source from python.org, which should allow you step into the python source code without having to build python yourself. Best regards, Tony On Thu, Aug 21, 2014 at 10:36 AM, Serge WEINSTOCK > wrote: Hi Tony, I?ve noticed that you are the main contributor for this branch of Python for .Net. Thanks a lot for that contribution. Maybe you can help me a little more with my issue. I think the main issue is due to the fact that I?m using Python 3.2. I?ve done the following tests: * Python 3.2 x86: * calling .Net libraries from standard python interpreter: works fine. * running embedded interpreter from .Net application: * from Visual Studio: works fine. Setting PYTHONHOME is enough. No need to set PYTHONPATH * from command line: doesn't work. Setting PYTHONPATH improves a little things. * Python 3.2 x64: * calling .Net libraries from standard python interpreter: works fine. * running embedded interpreter from .Net application: * from Visual Studio: doesn't work. Setting PYTHONPATH improves a little things. * from command line: doesn't work. Setting PYTHONPATH improves a little things. * Python 3.3 x86: * calling .Net libraries from standard python interpreter: works fine. * running embedded interpreter from .Net application: * from Visual Studio: works fine. * from command line: works fine. I've also compared for the VS run or for the command line run: * the paths given by 'sys.modules'. They are the same. * the paths of the loaded dlls as given by 'listdlls'. They are the same. Maybe you have a clue on why running an embedded interpreter works with 3.3 but not 3.3 Thanks, Serge From: Serge WEINSTOCK Sent: 19 August 2014 17:45 To: 'pythondotnet at python.org' Subject: RE: [Python.NET] Running an embedded interpreter Hi Tony, I?ve tried your suggestion but it doesn?t work. The issue seems to be more ?fundamental? as the import of the .Net System assembly doesn?t work. Serge From: pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] Sent: 19 August 2014 17:16 To: pythondotnet at python.org Subject: Re: [Python.NET] Running an embedded interpreter Hi Serge, 'mbcs' is what python uses to mean the current configured encoding. I would guess that the encoding of sys.stdout is different when using visual studio output console than the console. You could try a different encoding method by setting the PYTHONIOENCODING environment variable before starting your exe, eg: SET PYTHONIOENCODING=utf-8:ignore Look for PYTHONIOENCODING here https://docs.python.org/3/using/cmdline.html for more details. Tony On Tue, Aug 19, 2014 at 2:22 PM, Serge WEINSTOCK > wrote: Hi, I?m trying to use Python3.2 using the Python.Net version found at: https://github.com/renshawbay/pythonnet I?m using the following simple test program: //======================================================================= using System; using System.IO; using Python.Runtime; namespace TestPythonNet { class Program { static void Main(string[] args) { string binDir = Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(Program)).Location); string pyHome = @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; PythonEngine.PythonHome = @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; PythonEngine.ProgramName = "PythonRuntime"; Environment.SetEnvironmentVariable("PYTHONPATH", Path.GetFullPath(Path.Combine(pyHome, "DLLs")) + ";" + Path.GetFullPath(Path.Combine(pyHome, "Lib")) + ";" + Path.GetFullPath(Path.Combine(pyHome, "Lib", "site-packages")) + ";" + binDir ); Environment.SetEnvironmentVariable("PYTHONVERBOSE", "1"); PythonEngine.Initialize(); PythonEngine.ImportModule("clr"); using (Py.GIL()) { PythonEngine.RunSimpleString( "import clr; " + "a = clr.AddReference('System'); " + "print(a.Location);" + "from System import Environment;" + "print(Environment.MachineName);"); } } } } //======================================================================= ?D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime? is a folder where I?ve copied the DLLs and Lib folder from a python 3.2 x86 distribution. When I run it from Visual Studio it works fine (I guess it may be related to the fact that I?m using python tools for Visual Studio). But when I run it from the console, it fails with the output: //======================================================================= Traceback (most recent call last): File "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", line 481, in execsitecustomize import sitecustomize UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character Traceback (most recent call last): File "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", line 497, in execusercustomize import usercustomize UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll Traceback (most recent call last): File "", line 1, in UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character //======================================================================= The ?print(a.Location);" works but not the ?from System import Environment?. There are also all these errors about mbcs. Any idea on what I?m doing wrong? Thanks, Serge Weinstock ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. _________________________________________________ Python.NET mailing list - PythonDotNet at python.org https://mail.python.org/mailman/listinfo/pythondotnet ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. _________________________________________________ Python.NET mailing list - PythonDotNet at python.org https://mail.python.org/mailman/listinfo/pythondotnet ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. _________________________________________________ Python.NET mailing list - PythonDotNet at python.org https://mail.python.org/mailman/listinfo/pythondotnet ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tony at pyxll.com Wed Aug 27 18:33:49 2014 From: tony at pyxll.com (Tony Roberts) Date: Wed, 27 Aug 2014 17:33:49 +0100 Subject: [Python.NET] Running an embedded interpreter In-Reply-To: References: Message-ID: Hi Serge, I don't see your pull request here https://github.com/renshawbay/pythonnet/pulls, and I can't see your fork. Could you send me the url of your fork and I'll take a look if I can pull the changes from there? If you want to have another go at creating the pull request this might help: On Wed, Aug 27, 2014 at 2:02 PM, Serge WEINSTOCK < serge.weinstock at uk.bnpparibas.com> wrote: > Hi Tony, > > > > I?ve submitted the fix. As I?m new to git and github, I?m not sure I?ve > done the right steps. Let me know if you can?t find my patch > > > > Thanks, > > Serge > > > > *From:* pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org > [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] > > *Sent:* 26 August 2014 16:54 > > *To:* pythondotnet at python.org > *Subject:* Re: [Python.NET] Running an embedded interpreter > > > > Hi Serge, > > > > ah great, good spot. > > > > Certainly, please send a pull request to the github repo and I'll merge it. > > > > thanks, > > Tony > > > > On Tue, Aug 26, 2014 at 1:07 AM, Serge WEINSTOCK < > serge.weinstock at uk.bnpparibas.com> wrote: > > Hi Tony, > > I think I've found the issue: > The signatures of Py_SetPythonHome, Py_GetPythonHome, Py_SetProgramName > and Py_GetProgramName have changed from python 2.x to python 3.x: the > strings are now Unicode strings. > > I've done the following patches (I've also added support for Py_SetPath > and Py_GetPath as I needed to set up myself sys.path): > * runtime.cs > //================================================================== > #if PYTHON32 || PYTHON33 || PYTHON34 > [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, > ExactSpelling=true, CharSet=CharSet.Ansi)] > [return: MarshalAs(UnmanagedType.LPWStr)] > internal unsafe static extern string > Py_GetProgramName(); > > [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, > ExactSpelling=true, CharSet=CharSet.Ansi)] > internal unsafe static extern void > Py_SetProgramName([MarshalAsAttribute(UnmanagedType.LPWStr)]string > name); > > [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, > ExactSpelling=true, CharSet=CharSet.Ansi)] > [return: MarshalAs(UnmanagedType.LPWStr)] > internal unsafe static extern string > Py_GetPythonHome(); > > [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, > ExactSpelling=true, CharSet=CharSet.Ansi)] > internal unsafe static extern void > Py_SetPythonHome([MarshalAsAttribute(UnmanagedType.LPWStr)]string > home); > > [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, > ExactSpelling=true, CharSet=CharSet.Ansi)] > [return: MarshalAs(UnmanagedType.LPWStr)] > internal unsafe static extern string > Py_GetPath(); > > [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, > ExactSpelling=true, CharSet=CharSet.Ansi)] > internal unsafe static extern void > Py_SetPath([MarshalAsAttribute(UnmanagedType.LPWStr)]string home); > #else > [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, > ExactSpelling = true, CharSet = CharSet.Ansi)] > internal unsafe static extern string > Py_GetProgramName(); > > [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, > ExactSpelling = true, CharSet = CharSet.Ansi)] > internal unsafe static extern void > Py_SetProgramName(string name); > > [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, > ExactSpelling = true, CharSet = CharSet.Ansi)] > internal unsafe static extern string > Py_GetPythonHome(); > > [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, > ExactSpelling = true, CharSet = CharSet.Ansi)] > internal unsafe static extern void > Py_SetPythonHome(string home); > > [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, > ExactSpelling = true, CharSet = CharSet.Ansi)] > internal unsafe static extern string > Py_GetPath(); > > [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, > ExactSpelling = true, CharSet = CharSet.Ansi)] > internal unsafe static extern void > Py_SetPath(string home); > #endif > //================================================================== > PythonEngine.cs > //================================================================== > public static string PythonPath { > get > { > string result = Runtime.Py_GetPath(); > if (result == null) > { > return ""; > } > return result; > } > set > { > Runtime.Py_SetPath(value); > } > } > //================================================================== > > Could you add these patches to the source repository? > > Thanks, > > Serge > > > > From: pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org > [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] > > Sent: 21 August 2014 14:43 > > To: pythondotnet at python.org > Subject: Re: [Python.NET] Running an embedded interpreter > > Hi Serge, > > sorry, not sure why one would work and not the other. For what it's worth, > I've been using the 3.2 x64 version for some time now, both for calling > .NET from python and for embedding Python into a .NET application without > any problem like the ones you describe. > > What I suggest you try is grabbing the latest code from the renshawbay > repo and build that using setupwin.py - you might want to edit that file to > build the debug project. Then you will be able to step through and see > exactly where it's going wrong. You can build it from visual studio if you > prefer, but you will have to be careful to set some of the defines > correctly; look at setupwin.py to see what needs setting. > > You can also download the pdb files and python source from python.org, > which should allow you step into the python source code without having to > build python yourself. > > Best regards, > Tony > > > On Thu, Aug 21, 2014 at 10:36 AM, Serge WEINSTOCK < > serge.weinstock at uk.bnpparibas.com> wrote: > Hi Tony, > > I?ve noticed that you are the main contributor for this branch of Python > for .Net. Thanks a lot for that contribution. > > Maybe you can help me a little more with my issue. I think the main issue > is due to the fact that I?m using Python 3.2. > > I?ve done the following tests: > * Python 3.2 x86: > * calling .Net libraries from standard python interpreter: works fine. > * running embedded interpreter from .Net application: > * from Visual Studio: works fine. Setting PYTHONHOME is enough. > No need to set PYTHONPATH > * from command line: doesn't work. Setting PYTHONPATH improves a > little things. > * Python 3.2 x64: > * calling .Net libraries from standard python interpreter: works fine. > * running embedded interpreter from .Net application: > * from Visual Studio: doesn't work. Setting PYTHONPATH improves > a little things. > * from command line: doesn't work. Setting PYTHONPATH improves a > little things. > * Python 3.3 x86: > * calling .Net libraries from standard python interpreter: works fine. > * running embedded interpreter from .Net application: > * from Visual Studio: works fine. > * from command line: works fine. > > I've also compared for the VS run or for the command line run: > * the paths given by 'sys.modules'. They are the same. > * the paths of the loaded dlls as given by 'listdlls'. They are the same. > > Maybe you have a clue on why running an embedded interpreter works with > 3.3 but not 3.3 > > Thanks, > Serge > > > From: Serge WEINSTOCK > Sent: 19 August 2014 17:45 > To: 'pythondotnet at python.org' > Subject: RE: [Python.NET] Running an embedded interpreter > > Hi Tony, > > I?ve tried your suggestion but it doesn?t work. > > The issue seems to be more ?fundamental? as the import of the .Net System > assembly doesn?t work. > > Serge > > From: pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org > [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] > Sent: 19 August 2014 17:16 > To: pythondotnet at python.org > Subject: Re: [Python.NET] Running an embedded interpreter > Hi Serge, > > 'mbcs' is what python uses to mean the current configured encoding. I > would guess that the encoding of sys.stdout is different when using visual > studio output console than the console. > > You could try a different encoding method by setting the PYTHONIOENCODING > environment variable before starting your exe, eg: > SET PYTHONIOENCODING=utf-8:ignore > > Look for PYTHONIOENCODING here > https://docs.python.org/3/using/cmdline.html for more details. > > Tony > > > > On Tue, Aug 19, 2014 at 2:22 PM, Serge WEINSTOCK < > serge.weinstock at uk.bnpparibas.com> wrote: > Hi, > > I?m trying to use Python3.2 using the Python.Net version found at: > https://github.com/renshawbay/pythonnet > > I?m using the following simple test program: > > //======================================================================= > using System; > using System.IO; > using Python.Runtime; > > namespace TestPythonNet > { > class Program > { > static void Main(string[] args) > { > string binDir = > Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(Program)).Location); > string pyHome = > @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; > PythonEngine.PythonHome = > @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; > PythonEngine.ProgramName = "PythonRuntime"; > Environment.SetEnvironmentVariable("PYTHONPATH", > Path.GetFullPath(Path.Combine(pyHome, "DLLs")) + ";" + > Path.GetFullPath(Path.Combine(pyHome, "Lib")) + ";" + > Path.GetFullPath(Path.Combine(pyHome, "Lib", > "site-packages")) + ";" + > binDir > ); > Environment.SetEnvironmentVariable("PYTHONVERBOSE", "1"); > PythonEngine.Initialize(); > PythonEngine.ImportModule("clr"); > using (Py.GIL()) > { > PythonEngine.RunSimpleString( > "import clr; " + > "a = clr.AddReference('System'); " + > "print(a.Location);" + > "from System import Environment;" + > "print(Environment.MachineName);"); > } > } > } > } > //======================================================================= > > ?D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime? is a folder > where I?ve copied the DLLs and Lib folder from a python 3.2 x86 > distribution. > > > When I run it from Visual Studio it works fine (I guess it may be related > to the fact that I?m using python tools for Visual Studio). > > But when I run it from the console, it fails with the output: > > //======================================================================= > Traceback (most recent call last): > File > "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", > line 481, in execsitecustomize > import sitecustomize > UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: > invalid character > Traceback (most recent call last): > File > "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", > line 497, in execusercustomize > import usercustomize > UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: > invalid character > > C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll > Traceback (most recent call last): > File "", line 1, in > UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: > invalid character > //======================================================================= > > The ?print(a.Location);" works but not the ?from System import > Environment?. There are also all these errors about mbcs. > > Any idea on what I?m doing wrong? > > Thanks, > Serge Weinstock > > > ___________________________________________________________ > This e-mail may contain confidential and/or privileged information. If you > are not the intended recipient (or have received this e-mail in error) > please notify the sender immediately and delete this e-mail. Any > unauthorised copying, disclosure or distribution of the material in this > e-mail is prohibited. > Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for > additional disclosures. > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > > > ___________________________________________________________ > This e-mail may contain confidential and/or privileged information. If you > are not the intended recipient (or have received this e-mail in error) > please notify the sender immediately and delete this e-mail. Any > unauthorised copying, disclosure or distribution of the material in this > e-mail is prohibited. > > Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for > additional disclosures. > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > > > ___________________________________________________________ > This e-mail may contain confidential and/or privileged information. If you > are not the intended recipient (or have received this e-mail in error) > please notify the sender immediately and delete this e-mail. Any > unauthorised copying, disclosure or distribution of the material in this > e-mail is prohibited. > > Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for > additional disclosures. > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > > > > > ___________________________________________________________ > This e-mail may contain confidential and/or privileged information. If you > are not the intended recipient (or have received this e-mail in error) > please notify the sender immediately and delete this e-mail. Any > unauthorised copying, disclosure or distribution of the material in this > e-mail is prohibited. > > Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for > additional disclosures. > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > -------------- next part -------------- An HTML attachment was scrubbed... URL: From tony at pyxll.com Wed Aug 27 18:35:57 2014 From: tony at pyxll.com (Tony Roberts) Date: Wed, 27 Aug 2014 17:35:57 +0100 Subject: [Python.NET] Running an embedded interpreter In-Reply-To: References: Message-ID: Hi Serge, I don't see your pull request here https://github.com/renshawbay/pythonnet/pulls, and I can't see your fork. Could you send me the url of your fork and I'll take a look if I can pull the changes from there? If you want to have another go at creating the pull request this might help: https://help.github.com/articles/creating-a-pull-request Best regards, Tony p.s. Apologies for the previous incomplete mail. My fat fingers sent it early! On Wed, Aug 27, 2014 at 5:33 PM, Tony Roberts wrote: > Hi Serge, > > I don't see your pull request here > https://github.com/renshawbay/pythonnet/pulls, and I can't see your fork. > Could you send me the url of your fork and I'll take a look if I can pull > the changes from there? > > If you want to have another go at creating the pull request this might > help: > > > > > On Wed, Aug 27, 2014 at 2:02 PM, Serge WEINSTOCK < > serge.weinstock at uk.bnpparibas.com> wrote: > >> Hi Tony, >> >> >> >> I?ve submitted the fix. As I?m new to git and github, I?m not sure I?ve >> done the right steps. Let me know if you can?t find my patch >> >> >> >> Thanks, >> >> Serge >> >> >> >> *From:* pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org >> [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] >> >> *Sent:* 26 August 2014 16:54 >> >> *To:* pythondotnet at python.org >> *Subject:* Re: [Python.NET] Running an embedded interpreter >> >> >> >> Hi Serge, >> >> >> >> ah great, good spot. >> >> >> >> Certainly, please send a pull request to the github repo and I'll merge >> it. >> >> >> >> thanks, >> >> Tony >> >> >> >> On Tue, Aug 26, 2014 at 1:07 AM, Serge WEINSTOCK < >> serge.weinstock at uk.bnpparibas.com> wrote: >> >> Hi Tony, >> >> I think I've found the issue: >> The signatures of Py_SetPythonHome, Py_GetPythonHome, Py_SetProgramName >> and Py_GetProgramName have changed from python 2.x to python 3.x: the >> strings are now Unicode strings. >> >> I've done the following patches (I've also added support for Py_SetPath >> and Py_GetPath as I needed to set up myself sys.path): >> * runtime.cs >> //================================================================== >> #if PYTHON32 || PYTHON33 || PYTHON34 >> [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, >> ExactSpelling=true, CharSet=CharSet.Ansi)] >> [return: MarshalAs(UnmanagedType.LPWStr)] >> internal unsafe static extern string >> Py_GetProgramName(); >> >> [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, >> ExactSpelling=true, CharSet=CharSet.Ansi)] >> internal unsafe static extern void >> >> Py_SetProgramName([MarshalAsAttribute(UnmanagedType.LPWStr)]string name); >> >> [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, >> ExactSpelling=true, CharSet=CharSet.Ansi)] >> [return: MarshalAs(UnmanagedType.LPWStr)] >> internal unsafe static extern string >> Py_GetPythonHome(); >> >> [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, >> ExactSpelling=true, CharSet=CharSet.Ansi)] >> internal unsafe static extern void >> Py_SetPythonHome([MarshalAsAttribute(UnmanagedType.LPWStr)]string >> home); >> >> [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, >> ExactSpelling=true, CharSet=CharSet.Ansi)] >> [return: MarshalAs(UnmanagedType.LPWStr)] >> internal unsafe static extern string >> Py_GetPath(); >> >> [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, >> ExactSpelling=true, CharSet=CharSet.Ansi)] >> internal unsafe static extern void >> Py_SetPath([MarshalAsAttribute(UnmanagedType.LPWStr)]string home); >> #else >> [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, >> ExactSpelling = true, CharSet = CharSet.Ansi)] >> internal unsafe static extern string >> Py_GetProgramName(); >> >> [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, >> ExactSpelling = true, CharSet = CharSet.Ansi)] >> internal unsafe static extern void >> Py_SetProgramName(string name); >> >> [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, >> ExactSpelling = true, CharSet = CharSet.Ansi)] >> internal unsafe static extern string >> Py_GetPythonHome(); >> >> [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, >> ExactSpelling = true, CharSet = CharSet.Ansi)] >> internal unsafe static extern void >> Py_SetPythonHome(string home); >> >> [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, >> ExactSpelling = true, CharSet = CharSet.Ansi)] >> internal unsafe static extern string >> Py_GetPath(); >> >> [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, >> ExactSpelling = true, CharSet = CharSet.Ansi)] >> internal unsafe static extern void >> Py_SetPath(string home); >> #endif >> //================================================================== >> PythonEngine.cs >> //================================================================== >> public static string PythonPath { >> get >> { >> string result = Runtime.Py_GetPath(); >> if (result == null) >> { >> return ""; >> } >> return result; >> } >> set >> { >> Runtime.Py_SetPath(value); >> } >> } >> //================================================================== >> >> Could you add these patches to the source repository? >> >> Thanks, >> >> Serge >> >> >> >> From: pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org >> [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org >> ] >> >> Sent: 21 August 2014 14:43 >> >> To: pythondotnet at python.org >> Subject: Re: [Python.NET] Running an embedded interpreter >> >> Hi Serge, >> >> sorry, not sure why one would work and not the other. For what it's >> worth, I've been using the 3.2 x64 version for some time now, both for >> calling .NET from python and for embedding Python into a .NET application >> without any problem like the ones you describe. >> >> What I suggest you try is grabbing the latest code from the renshawbay >> repo and build that using setupwin.py - you might want to edit that file to >> build the debug project. Then you will be able to step through and see >> exactly where it's going wrong. You can build it from visual studio if you >> prefer, but you will have to be careful to set some of the defines >> correctly; look at setupwin.py to see what needs setting. >> >> You can also download the pdb files and python source from python.org, >> which should allow you step into the python source code without having to >> build python yourself. >> >> Best regards, >> Tony >> >> >> On Thu, Aug 21, 2014 at 10:36 AM, Serge WEINSTOCK < >> serge.weinstock at uk.bnpparibas.com> wrote: >> Hi Tony, >> >> I?ve noticed that you are the main contributor for this branch of Python >> for .Net. Thanks a lot for that contribution. >> >> Maybe you can help me a little more with my issue. I think the main issue >> is due to the fact that I?m using Python 3.2. >> >> I?ve done the following tests: >> * Python 3.2 x86: >> * calling .Net libraries from standard python interpreter: works fine. >> * running embedded interpreter from .Net application: >> * from Visual Studio: works fine. Setting PYTHONHOME is enough. >> No need to set PYTHONPATH >> * from command line: doesn't work. Setting PYTHONPATH improves >> a little things. >> * Python 3.2 x64: >> * calling .Net libraries from standard python interpreter: works fine. >> * running embedded interpreter from .Net application: >> * from Visual Studio: doesn't work. Setting PYTHONPATH improves >> a little things. >> * from command line: doesn't work. Setting PYTHONPATH improves >> a little things. >> * Python 3.3 x86: >> * calling .Net libraries from standard python interpreter: works fine. >> * running embedded interpreter from .Net application: >> * from Visual Studio: works fine. >> * from command line: works fine. >> >> I've also compared for the VS run or for the command line run: >> * the paths given by 'sys.modules'. They are the same. >> * the paths of the loaded dlls as given by 'listdlls'. They are the same. >> >> Maybe you have a clue on why running an embedded interpreter works with >> 3.3 but not 3.3 >> >> Thanks, >> Serge >> >> >> From: Serge WEINSTOCK >> Sent: 19 August 2014 17:45 >> To: 'pythondotnet at python.org' >> Subject: RE: [Python.NET] Running an embedded interpreter >> >> Hi Tony, >> >> I?ve tried your suggestion but it doesn?t work. >> >> The issue seems to be more ?fundamental? as the import of the .Net System >> assembly doesn?t work. >> >> Serge >> >> From: pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org >> [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org >> ] >> Sent: 19 August 2014 17:16 >> To: pythondotnet at python.org >> Subject: Re: [Python.NET] Running an embedded interpreter >> Hi Serge, >> >> 'mbcs' is what python uses to mean the current configured encoding. I >> would guess that the encoding of sys.stdout is different when using visual >> studio output console than the console. >> >> You could try a different encoding method by setting the PYTHONIOENCODING >> environment variable before starting your exe, eg: >> SET PYTHONIOENCODING=utf-8:ignore >> >> Look for PYTHONIOENCODING here >> https://docs.python.org/3/using/cmdline.html for more details. >> >> Tony >> >> >> >> On Tue, Aug 19, 2014 at 2:22 PM, Serge WEINSTOCK < >> serge.weinstock at uk.bnpparibas.com> wrote: >> Hi, >> >> I?m trying to use Python3.2 using the Python.Net version found at: >> https://github.com/renshawbay/pythonnet >> >> I?m using the following simple test program: >> >> //======================================================================= >> using System; >> using System.IO; >> using Python.Runtime; >> >> namespace TestPythonNet >> { >> class Program >> { >> static void Main(string[] args) >> { >> string binDir = >> Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(Program)).Location); >> string pyHome = >> @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; >> PythonEngine.PythonHome = >> @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; >> PythonEngine.ProgramName = "PythonRuntime"; >> Environment.SetEnvironmentVariable("PYTHONPATH", >> Path.GetFullPath(Path.Combine(pyHome, "DLLs")) + ";" + >> Path.GetFullPath(Path.Combine(pyHome, "Lib")) + ";" + >> Path.GetFullPath(Path.Combine(pyHome, "Lib", >> "site-packages")) + ";" + >> binDir >> ); >> Environment.SetEnvironmentVariable("PYTHONVERBOSE", "1"); >> PythonEngine.Initialize(); >> PythonEngine.ImportModule("clr"); >> using (Py.GIL()) >> { >> PythonEngine.RunSimpleString( >> "import clr; " + >> "a = clr.AddReference('System'); " + >> "print(a.Location);" + >> "from System import Environment;" + >> "print(Environment.MachineName);"); >> } >> } >> } >> } >> //======================================================================= >> >> ?D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime? is a folder >> where I?ve copied the DLLs and Lib folder from a python 3.2 x86 >> distribution. >> >> >> When I run it from Visual Studio it works fine (I guess it may be related >> to the fact that I?m using python tools for Visual Studio). >> >> But when I run it from the console, it fails with the output: >> >> //======================================================================= >> Traceback (most recent call last): >> File >> "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", >> line 481, in execsitecustomize >> import sitecustomize >> UnicodeEncodeError: 'mbcs' codec can't encode characters in position >> 0--1: invalid character >> Traceback (most recent call last): >> File >> "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", >> line 497, in execusercustomize >> import usercustomize >> UnicodeEncodeError: 'mbcs' codec can't encode characters in position >> 0--1: invalid character >> >> C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll >> Traceback (most recent call last): >> File "", line 1, in >> UnicodeEncodeError: 'mbcs' codec can't encode characters in position >> 0--1: invalid character >> //======================================================================= >> >> The ?print(a.Location);" works but not the ?from System import >> Environment?. There are also all these errors about mbcs. >> >> Any idea on what I?m doing wrong? >> >> Thanks, >> Serge Weinstock >> >> >> ___________________________________________________________ >> This e-mail may contain confidential and/or privileged information. If >> you are not the intended recipient (or have received this e-mail in error) >> please notify the sender immediately and delete this e-mail. Any >> unauthorised copying, disclosure or distribution of the material in this >> e-mail is prohibited. >> Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for >> additional disclosures. >> >> _________________________________________________ >> Python.NET mailing list - PythonDotNet at python.org >> https://mail.python.org/mailman/listinfo/pythondotnet >> >> >> ___________________________________________________________ >> This e-mail may contain confidential and/or privileged information. If >> you are not the intended recipient (or have received this e-mail in error) >> please notify the sender immediately and delete this e-mail. Any >> unauthorised copying, disclosure or distribution of the material in this >> e-mail is prohibited. >> >> Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for >> additional disclosures. >> _________________________________________________ >> Python.NET mailing list - PythonDotNet at python.org >> https://mail.python.org/mailman/listinfo/pythondotnet >> >> >> ___________________________________________________________ >> This e-mail may contain confidential and/or privileged information. If >> you are not the intended recipient (or have received this e-mail in error) >> please notify the sender immediately and delete this e-mail. Any >> unauthorised copying, disclosure or distribution of the material in this >> e-mail is prohibited. >> >> Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for >> additional disclosures. >> _________________________________________________ >> Python.NET mailing list - PythonDotNet at python.org >> https://mail.python.org/mailman/listinfo/pythondotnet >> >> >> >> >> ___________________________________________________________ >> This e-mail may contain confidential and/or privileged information. If >> you are not the intended recipient (or have received this e-mail in error) >> please notify the sender immediately and delete this e-mail. Any >> unauthorised copying, disclosure or distribution of the material in this >> e-mail is prohibited. >> >> Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for >> additional disclosures. >> >> _________________________________________________ >> Python.NET mailing list - PythonDotNet at python.org >> https://mail.python.org/mailman/listinfo/pythondotnet >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: