本文介绍了pythonnet在.net示例中嵌入Python无法加载模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 https://github.com/运行.NET示例中的嵌入Python. pythonnet/pythonnet .我已经按照故障排除文章中的说明在程序基本目录中为我的anaconda环境设置了正确的%PYTHONPATH%和%PYTHONHOME%.

I'm trying to run the Embedding Python in .NET example from https://github.com/pythonnet/pythonnet. I've followed troubleshooting articles to set the proper %PYTHONPATH% and %PYTHONHOME% to my anaconda environment in the program base directory.

激活我的anaconda环境后,我已经成功导入了sys和imp作为测试,并且还成功使用了PythonEngine.RunSimpleString(),但是numpy示例因 Python.Runtime.PythonException而失败:ImportError:没有模块名为"numpy"

After activating my anaconda environment, I have successfully imported sys, and imp as a test, and also sucessfully used PythonEngine.RunSimpleString(), but the numpy example fails with Python.Runtime.PythonException: ImportError : No module named 'numpy'

在此环境中从python导入numpy成功,但是此软件包和其他软件包无法在pythonnet中导入.

importing numpy from python in this environment was successful, but this and other packages fail to import in pythonnet.

Pythonnet版本:2.3 x64(使用 conda install -c pythonnet pythonnet 安装)

Pythonnet version: 2.3 x64 (installed using conda install -c pythonnet pythonnet)

Python版本:Python 3.5 x64(anaconda)

Python version: Python 3.5 x64 (anaconda)

操作系统:Windows 10

Operating System: Windows 10

以下代码会产生错误:

static void Main(string[] args)
{
    string envPythonHome = AppDomain.CurrentDomain.BaseDirectory + "cntk-py35";
    string envPythonLib = envPythonHome + @"\Lib";
    Environment.SetEnvironmentVariable("PYTHONHOME", envPythonHome, EnvironmentVariableTarget.Process);
    Environment.SetEnvironmentVariable("PATH", envPythonHome + ";" + Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine), EnvironmentVariableTarget.Process);
    Environment.SetEnvironmentVariable("PYTHONPATH", envPythonLib, EnvironmentVariableTarget.Process);

    PythonEngine.PythonHome = envPythonHome;
    PythonEngine.PythonPath = Environment.GetEnvironmentVariable("PYTHONPATH");

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

在我的环境中,site-packages下的所有软件包似乎都失败了.添加到%PATH%无效.有没有办法让pythonnet识别并加载这些模块?

It seems that any package under site-packages in my environment similarly fail. Adding to %PATH% did not work. Is there a way to get pythonnet to recognize and load these modules?

推荐答案

我能够通过将Lib/site-packages添加到PYTHONPATH变量(而不是PATH)中来导入模块,该变量将文件夹添加到sys.path中.其他任何python库和自定义python代码都必须将相应的文件夹添加到PYTHONPATH.

I was able to import the modules by adding Lib/site-packages to the PYTHONPATH variable (rather than the PATH) which adds the folder to sys.path. It was necessary for any other python libraries and custom python code to add the corresponding folder to PYTHONPATH.

这篇关于pythonnet在.net示例中嵌入Python无法加载模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 09:51