我想使用 IronPython 运行 Python 应用程序。
这是错误:
Unhandled exception. IronPython.Runtime.Exceptions.ImportException: Cannot import name IntFlag
at Microsoft.Scripting.Runtime.LightExceptions.ThrowException(LightException lightEx)
at Microsoft.Scripting.Runtime.LightExceptions.CheckAndThrow(Object value)
at Microsoft.Scripting.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.HandleException(InterpretedFrame frame, Exception exception)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
at IronPython.Compiler.PythonScriptCode.Run(Scope scope)
at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)
at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink)
at Microsoft.Scripting.SourceUnit.Execute(Scope scope)
at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)
我尝试手动将库添加到 IronPython。
我已经遇到过site-packages中缺少库的情况。当我添加它时,问题就解决了。
我添加到路径中的其余文件夹包含 .py 文件。
using System;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
namespace IronPythonExample
{
class Program
{
static void Main(string[] args)
{
// Set the Python script file path
string pythonScriptPath = @"/usr/PhyProgram.py";
// Execute the script
// Create a new Python runtime
ScriptEngine engine = Python.CreateEngine();
var searchPaths = engine.GetSearchPaths();
searchPaths.Add(@"/usr/local/lib/python3.6/site-packages");
searchPaths.Add(@"/usr/phy_core_lib");
searchPaths.Add(@"/usr/phy_core_lib/src");
searchPaths.Add(@"/usr/phy_core_lib/src/network");
searchPaths.Add(@"/usr/phy_core_lib/src/network/prm");
engine.SetSearchPaths(searchPaths);
ScriptScope scope = engine.CreateScope();
engine.CreateScriptSourceFromFile(pythonScriptPath).Execute(scope);
}
}
}