pythonnet 从 3.0 版本开始,必须设置Runtime.PythonDLL
属性或环境变量
例如:
cs
string pathToVirtualEnv = ".\\envs\\pythonnetTest";
Runtime.PythonDLL = Path.Combine(pathToVirtualEnv, "python39.dll");
PythonEngine.PythonHome = Path.Combine(pathToVirtualEnv, "python.exe");
PythonEngine.PythonPath = $"{pathToVirtualEnv}\\Lib\\site-packages;{pathToVirtualEnv}\\Lib";
PythonEngine.Initialize();
如果报错:
内部异常 1:
DllNotFoundException: Could not load C:\AppData\Python311\python311.dll.
内部异常 2:
Win32Exception: %1 不是有效的 Win32 应用程序。
解决办法:
修改项目属性,目标平台 any cpu 改成目标平台(x64)