gpt4 book ai didi

python - 如何让 Python.Net 使用 Python 3.6 Anaconda Distribution

转载 作者:行者123 更新时间:2023-12-04 03:04:26 25 4
gpt4 key购买 nike

如何让 Python.NET 使用 Python 3.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()

我没有 Python 3.5,因此没有 python35.dll。我有 Python 3.6。它是 Anaconda 发行版的一部分。我如何让 Python.Net 使用它?

示例代码:

    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 }, dtype: np.int32);
Console.WriteLine(b.dtype);

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

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

python -c "import sys; print (sys.version)"给出

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

最佳答案

对我来说可行的解决方案是:

*假设我使用的是 Visual Studio 2012 Express,

  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. 最后,最重要的事情是在解决方案资源管理器的“属性”选项卡中设置您的系统架构类型。

在我的例子中(没有使用虚拟环境),就像下面的图片:

Steps 1 to 3.

Step 4.

希望它有用!

关于python - 如何让 Python.Net 使用 Python 3.6 Anaconda Distribution,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45869073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com