IronPython MissingMemberException尝试导入xml.etree.ElementT

2024-06-16 11:07:51 发布

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

我对Python一点都不熟悉,希望有人能帮助和指导我找出这里出了什么问题。在

以下是错误消息:

MissingMemberException: 'LightException' object has no attribute 'etree'

这是抛出它的python代码: import xml.etree.ElementTree as ET

我们在一个c项目中使用IronPython2.7.3,python代码使用Execute()方法执行: private void Execute(string code, ScriptScope scope) { try { PythonByteCode compiled = (PythonByteCode)Compile(code, SourceCodeType.AutoDetect); compiled.Execute(scope); } catch (Exception e) { throw new PythonParseException(e); } }


Tags: no代码消息executeobject错误codeetree
1条回答
网友
1楼 · 发布于 2024-06-16 11:07:51

这很容易。运行引擎时,它不知道默认的程序集位置(在我的机器上是“C:\ProgramFiles(x86)\IronPython2.7”)。因此,它尝试从当前工作目录中获取模块,然后从工作目录的-Lib子目录中获取模块。当然,它在那里找不到模块。 enter image description here

你应该做什么:

获取IronPython发行版的路径。实际上,您需要Lib子目录内容。也许您应该考虑如何在目标计算机上部署它,这样您的发布版本也可以找到它。在

使用下面的代码将其添加到python搜索路径

string dir = Path.GetDirectoryName(scriptPath);                       
ICollection<string> paths = engine.GetSearchPaths();

if (!string.IsNullOrEmptydir))
{
    paths.Add(dir);
}
else
{
    paths.Add(Environment.CurrentDirectory);
}
engine.SetSearchPaths(paths);

相关问题 更多 >