我有一个使用Pycharm / Spyder可以正常运行的脚本,但是当我尝试在c#上使用Iron Python运行时,出现以下错误:'没有名为keras.callbacks的模块'
这是我用来运行脚本的代码:
public string PatchParameter(string parameter, int serviceid)
{
var engine = Python.CreateEngine(); // Extract Python language engine from their grasp
var scope = engine.CreateScope(); // Introduce Python namespace (scope)
var d = new Dictionary
{
{ "serviceid", serviceid},
{ "parameter", parameter}
}; // Add some sample parameters. Notice that there is no need in specifically setting the object type, interpreter will do that part for us in the script properly with high probability
scope.SetVariable("params", d); // This will be the name of the dictionary in python script, initialized with previously created .NET Dictionary
ICollection searchPaths = engine.GetSearchPaths();
searchPaths.Add(@"D:\Projects\xxx");
searchPaths.Add(@"C:\Users\xxx\Anaconda3");
searchPaths.Add(@"C:\Users\xxx\Anaconda3\Library\bin");
searchPaths.Add(@"C:\Users\xxx\Anaconda3\Scripts");
searchPaths.Add(@"D:\Projects\SmartTrader\venv");
searchPaths.Add(@"D:\Projects\SmartTrader\venv\Scripts");
searchPaths.Add("..\\..");
engine.SetSearchPaths(searchPaths);
ScriptSource source = engine.CreateScriptSourceFromFile(@"D:\Projects\xxx\Main.py"); // Load the script
object result = source.Execute(scope);
parameter = scope.GetVariable("parameter"); // To get the finally set variable 'parameter' from the python script
return parameter;
}
在注释中,建议将虚拟环境添加到搜索路径,但是仍然无法正常工作。
我还尝试通过常规CMD运行它,但效果不佳:
C:\Users\xxx\Anaconda3>python.exe D:\Projects\xxx\Main.py
Traceback (most recent call last):
File "D:\Projects\xxx\Main.py", line 7, in
import Config as cfg
File "D:\Projects\xxx\Config.py", line 2, in
from keras.callbacks import EarlyStopping, ModelCheckpoint
ModuleNotFoundError: No module named 'keras'
我尝试将Anaconda路径添加到其搜索路径,但是没有用。 我想念什么?
谢谢阿米特