[Python.NET] Running an embedded interpreter

Tony Roberts tony at pyxll.com
Thu Aug 21 15:43:27 CEST 2014


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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20140821/586ae45b/attachment.html>


More information about the PythonDotNet mailing list