在 IronPython 中导入外部库失败

问题描述 投票:0回答:1

我想使用 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);
        }
    }
}
python c# ironpython
1个回答
0
投票

您似乎引用的标准 python 库(3.4.1,当前 IronPython 版本附带的库)不包含 IntFlag,因为它是在 3.6 中引入的。当前状态可以在这里看到。

您尝试运行的代码很可能(尚)与 IronPython 目前提供的功能不兼容。

© www.soinside.com 2019 - 2024. All rights reserved.