问题描述
我正在尝试从 https://github.com/运行 Embedding Python in .NET 示例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: No module命名为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 添加到将文件夹添加到 sys.path 的 PYTHONPATH 变量(而不是 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 无法加载模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!