测试环境:
windows10 x64
VS2019
首先使用的是官方代码:
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);
1、然后添加引用Python.Runtime,这个是pythonnet的库,注意.net framework版本,我这设置4.5不行,设置4.7.1可以
2、添加环境变量,下面请对应自己的路径修改
变量名称:PYTHONNET_PYDLL
值:D:\anaconda3\python38.dll
变量名称:PYTHONHOME
值:D:\anaconda3
变量名称:PYTHONPATH
值:D:\anaconda3\python.exe
此时运行即可