【发布时间】:2014-10-12 17:07:20
【问题描述】:
我正在尝试使用 Python3.2,使用的 Python.Net 版本位于:https://github.com/renshawbay/pythonnet
我正在使用以下简单的测试程序:
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”是我从 python 3.2 x86 发行版复制 DLL 和 Lib 文件夹的文件夹。
当我从 Visual Studio 运行它时,它运行良好(我猜这可能与我在 Visual Studio 中使用 python 工具有关)。
但是当我从控制台运行它时,它会输出失败:
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
“print(a.Location);” 有效,但“from System import Environment”无效。还有关于mbcs 的所有这些错误。
知道我做错了什么吗?
【问题讨论】:
标签: c# python .net python-3.x python.net