[Python.NET] Running an embedded interpreter

Tony Roberts tony at pyxll.com
Wed Aug 27 18:35:57 CEST 2014


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 <tony at pyxll.com> 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 "<string>", line 1, in <module>
>> 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: <http://mail.python.org/pipermail/pythondotnet/attachments/20140827/abeb5ba7/attachment-0001.html>


More information about the PythonDotNet mailing list