如何查看我的 IronPython 脚本生成了哪些异常?

How can I see what exceptions my IronPython script is generating?

我正在尝试将 IronPython 嵌入到 Unity 3D 项目中,我 运行 遇到了一些我似乎无法弄清楚的错误。

我将以下脚本附加到 Unity 中的游戏对象。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using IronPython;
using IronPython.Modules;
using System.Text;

public class IronPythonWrapper : MonoBehaviour {

    void Start() {
        ScriptTest();
    }

    public static void ScriptTest() {
        // create the engine
        var engine = IronPython.Hosting.Python.CreateEngine();

        // and the scope (i.e. the Python namespace)
        var scope = engine.CreateScope();

        // execute the Python script
        engine.ExecuteFile("Assets/Scripts/ipscript.py");

        // grab the variable from the Python scope
        string commands = scope.GetVariable<string>("commands");
        Debug.Log(commands);
    }
}

目标是让上面实例化的 IronPython 引擎执行以下简单的 Python 脚本,称为 ipscript.py.

commands = "Script executed."

当我进入播放模式(在 Unity 中)时出现以下错误。

MissingMemberException: 'ScopeStorage' object has no attribute 'commands'

有人知道我应该调查什么来解决这个问题吗?也许我需要实施一些错误捕获,以便我可以看到 IronPython 在执行 Python 脚本时生成的任何异常?

您的 python 执行未正确通过范围。因此,您的 commands 变量是在临时范围内创建的,该范围在执行 python 文件后被丢弃,并且显式创建的范围不知道 commands 是什么。

代码段应该读起来像

engine.ExecuteFile("Assets/Scripts/ipscript.py", scope);