Python脚本在Pycharm上运行,但在c上不使用铁皮Python#

2024-04-16 22:06:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个脚本,使用Pycharm/Spyder运行时没有任何问题,但是当我尝试在c上使用Iron Python运行时,我得到了以下错误:“没有名为keras.回调'

下面是我用来运行脚本的代码:

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<string, object>
            {
                { "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<string> 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<string>("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 <module>
    import Config as cfg
  File "D:\Projects\xxx\Config.py", line 2, in <module>
    from keras.callbacks import EarlyStopping, ModelCheckpoint
ModuleNotFoundError: No module named 'keras'

我试着将水蟒路径添加到它的搜索路径中,但没有成功。 我错过了什么?你知道吗

谢谢 阿米特


Tags: theinpyaddstringparameterscriptusers