我对Python并不熟悉,希望有人可以帮助并引导我找到这里出了什么问题。
这是错误消息:
MissingMemberException:'LightException'对象没有属性'etree'
这是抛出它的python代码:
import xml.etree.ElementTree as ET
我们在c#项目中使用IronPython 2.7.3,使用我们的Execute()方法执行python代码:
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);
}
}
答案 0 :(得分:1)
这很容易。当您运行引擎时,它不知道默认程序集位置(在我的机器上它是“C:\ Program Files(x86)\ IronPython 2.7”)。因此,它尝试从当前工作目录获取模块,然后 - 工作目录的 - Lib
子目录。当然它找不到那里的模块。
你应该做什么:
获取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);