【问题标题】:Exception when trying to run the example from the Python.Net ReadMe on github尝试从 github 上的 Python.Net 自述文件运行示例时出现异常
【发布时间】:2017-08-01 15:42:05
【问题描述】:

在 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。运行此程序时,我收到以下错误 {"Unable to load DLL 'python27': The specified module could not be found.{"Unable to load DLL 'python27': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"} with Stack trace

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 消息:托管调试助手“PInvokeStackImbalance”在“C:\Users\username\Documents\Visual Studio 2015\Projects\PythonNetIntegrationTest\PythonNetIntegrationTest\bin\Debug\PythonNetIntegrationTest.vshost.exe”中检测到问题。 附加信息:对 PInvoke 函数“Python.Runtime!Python.Runtime.Runtime::Py_SetPythonHome”的调用使堆栈不平衡。这可能是因为托管 PInvoke 签名与非托管目标签名不匹配。检查 PInvoke 签名的调用约定和参数是否与目标非托管签名匹配。

【问题讨论】:

  • @denfromufa 您链接到的 wiki 提供以下内容:'“无法加载 DLL pythonXX”:未安装 CPython,或未在 %PATH% 和 %PYTHONHOME% 环境变量中注册。或者在初始化 Python 运行时之前设置 PythonEngine.PythonHome 属性。我正在使用 Canopy 安装我的 Python,所以我尝试了 PythonEngine.PythonHome = "C:\path\to\python.exe"。这没有用。是不是我做错了什么?
  • 请再次阅读 wiki,我更新了链接。你的 pythonhome 应该设置为目录,而不是 python 可执行文件。
  • @denfromufa 请参阅上面已编辑的问题。
  • 您可能使用的是较旧的 Windows,因此请查看此问题:github.com/pythonnet/pythonnet/issues/448

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


【解决方案1】:

我认为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();
}

【讨论】:

    猜你喜欢
    • 2019-09-16
    • 2017-09-25
    • 2021-12-25
    • 2018-10-26
    • 1970-01-01
    • 2011-09-17
    • 2012-09-05
    • 1970-01-01
    • 2016-02-01
    相关资源
    最近更新 更多