尝试从github上的Python.Net ReadMe运行示例时出现异常

时间:2017-08-01 15:42:05

标签: c# .net visual-studio dll python.net

在Python.Net(https://github.com/pythonnet/pythonnet)的github页面上,给出了以下代码的典型用法示例:

string exeDir = AppDomain.CurrentDomain.BaseDirectory;
        string envPythonHome = exeDir + @"Python";
        Environment.SetEnvironmentVariable("PYTHONHOME", envPythonHome, EnvironmentVariableTarget.Process);
        Environment.SetEnvironmentVariable("PATH", envPythonHome, EnvironmentVariableTarget.Process);
        PythonEngine.PythonHome = envPythonHome;
using (Py.GIL())
{
   dynamic np = Py.Import("numpy");
   Console.WriteLine(np.cos(np.pi * 2));

   dynamic sin = np.sin;
   Console.WriteLine(sin(5));

   double c = np.cos(5) + sin(5);
   Console.WriteLine(c);

   dynamic a = np.array(new List<float> { 1, 2, 3 });
   Console.WriteLine(a.dtype);

   dynamic b = np.array(new List<float> { 6, 5, 4 });
   Console.WriteLine(b.dtype);

   Console.WriteLine(a * b);
   Console.ReadKey();
   }

在VS 2015(net45)中启动项目后,我创建了一个新的C#文件,该代码是主要方法。我添加了using Python.Runtime,我使用NuGet来安装pythonnet_py27_dotnet。运行此时,我收到以下错误{&#34;无法加载DLL&#39; python27&#39;:找不到指定的模块。带有堆栈跟踪的{"Unable to load DLL 'python27': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"}

at Python.Runtime.Runtime.Py_IsInitialized()
at Python.Runtime.Runtime.Initialize()
at Python.Runtime.PythonEngine.Initialize(Boolean setSysArgv)
at Python.Runtime.PythonEngine.Initialize()
at Python.Runtime.Py.GIL()
at PythonNetIntegrationTest.Program.Main(String[] args) in c:\users\<username>\documents\visual studio 2015\Projects\PythonNetIntegrationTest\PythonNetIntegrationTest\Program.cs:line 15
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

在解决方案资源管理器中查看Python.Runtime后,我看到此dll的完整路径为c:\users\username\documents\visual studio 2015\Projects\PythonNetIntegrationTest\packages\pythonnet_py27_dotnet.2.3.0\lib\net40\x86\Python.Runtime.dll。好像VS确切地知道这个dll在哪里。我怎样才能指示VS找到这个dll? 注意,我不能使用IronPython,请不要提示它。由于与我想与之交互的其他库的约束,我也只能使用Python 2.7 x86。

更新:上面的代码已根据问题#463(https://github.com/pythonnet/pythonnet/issues/463)的建议进行了编辑。现在出现了另一个错误: 发生了PInvokeStackImbalance 消息:托管调试助手&#39; PInvokeStackImbalance&#39;已在C:\ Users \ username \ Documents \ Visual Studio 2015 \ Projects \ PythonNetIntegrationTest \ PythonNetIntegrationTest \ bin \ Debug \ PythonNetIntegrationTest.vshost.exe&#39;中检测到问题。 附加信息:调用PInvoke函数&#39; Python.Runtime!Python.Runtime.Runtime :: Py_SetPythonHome&#39;堆栈不平衡。这很可能是因为托管PInvoke签名与非托管目标签名不匹配。检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配。

1 个答案:

答案 0 :(得分:0)

我认为 Python.Runtime 无法识别您的 python27.dll 文件,您可以在系统上安装的 python 中找到该文件。试试下面的代码。我认为它会解决您的问题。

string envPythonHome=@"C:\Users\YourLoggedInUserName\AppData\Local\Programs\Python\Python27\python27.dll";

Environment.SetEnvironmentVariable("PYTHONNET_PYDLL", envPythonHome, EnvironmentVariableTarget.Process);        

using (Py.GIL())
{
   dynamic np = Py.Import("numpy");
   Console.WriteLine(np.cos(np.pi * 2));

   dynamic sin = np.sin;
   Console.WriteLine(sin(5));

   double c = np.cos(5) + sin(5);
   Console.WriteLine(c);

   dynamic a = np.array(new List<float> { 1, 2, 3 });
   Console.WriteLine(a.dtype);

   dynamic b = np.array(new List<float> { 6, 5, 4 });
   Console.WriteLine(b.dtype);

   Console.WriteLine(a * b);
   Console.ReadKey();
}