如何获得Python.Net使用python3.6 Anaconda发行版

2024-06-25 22:32:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我怎么得到Python.NET使用Python3.6?我复制了下面的示例代码,当我运行它时,我得到了错误:

Unhandled Exception: System.DllNotFoundException: Unable to load DLL 'python35': 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(IEnumerable`1 args, Boolean setSysArgv)
   at Python.Runtime.PythonEngine.Initialize(Boolean setSysArgv)
   at Python.Runtime.PythonEngine.Initialize()
   at Python.Runtime.Py.GIL()

我没有python3.5,因此没有python35.dll。我有python3.6。这是水蟒分布的一部分。我怎么得到Python.Net用这个?在

示例代码:

^{pr2}$

更新以回答评论中的问题: 我安装了Python.net通过获得NuGet包“pythonnet_py35_dotnet”,它的版本是v2.3.0。在

python-c“导入系统;打印(系统版本)“给予

3.6.0 |Anaconda custom (64-bit)| (default, Dec 23 2016, 11:57:41) [MSC v.1900 64 bit (AMD64)]

“python”给出了

C:\Users\<username>\AppData\Local\Continuum\Anaconda3\python.exe

Tags: 代码py版本示例net系统exceptionbit
1条回答
网友
1楼 · 发布于 2024-06-25 22:32:13

我的解决方案是:

*假设我使用的是Visual Studio 2012学习版

  1. 在解决方案资源管理器的引用中添加对Python.Runtime.dll的引用(如果很难轻松找到DLL文件,只需尝试在windows上使用Everything应用程序)
  2. 在C代码的顶部添加以下行。在

    using Python.Runtime;
    
  3. 在调用using (Py.GIL()){...}之前添加以下代码

    Environment.SetEnvironmentVariable("PATH", @"path-to-the-directory-containing-python-interpreter.exe", EnvironmentVariableTarget.Process);
    Environment.SetEnvironmentVariable("PYTHONHOME", @"path-to-the-directory-containing-python-interpreter.exe", EnvironmentVariableTarget.Process);
    
  4. 最后,最重要的是在Properties is Solution Explorer的Build选项卡中设置系统架构类型。

在我的例子中(不使用虚拟环境),如下图:

Steps 1 to 3.

Step 4.

希望它能起作用!在

相关问题 更多 >