[Python.NET] Running an embedded interpreter
Tony Roberts
tony at pyxll.com
Tue Aug 19 18:15:38 CEST 2014
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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20140819/8631b473/attachment-0001.html>
More information about the PythonDotNet
mailing list