从c#程序调用python脚本错误:没有名为xml.etree.cElementTree的模块

从c#程序调用python脚本错误:没有名为xml.etree.cElementTree的模块,c#,xml,python-2.7,parsing,ironpython,C#,Xml,Python 2.7,Parsing,Ironpython,我已经编写了一个python脚本来解析xml文件。我从C#project调用这个文件。但是当运行一个程序时,我得到了一个错误:没有名为xml.etree.cElementTree的模块 Program.cs ----------- using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using IronPython.

我已经编写了一个python脚本来解析xml文件。我从C#project调用这个文件。但是当运行一个程序时,我得到了一个错误:没有名为xml.etree.cElementTree的模块

Program.cs
-----------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using IronPython.Hosting;
using IronPython.Modules;

namespace RunExternalScript
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Press enter to execute the python script!");
            Console.ReadLine();

            var py = Python.CreateEngine();
            try
            {
                py.ExecuteFile("wg.py");
            }
            catch (Exception ex)
            {
                Console.WriteLine(
                   "Oops! We couldn't execute the script because of an exception: " + ex.Message);
            }

            Console.WriteLine("Press enter to exit...");
            Console.ReadLine();
        }
    }
}



wg.py
-----


import xml.etree.cElementTree as ET 

tree = ET.parse('Wg.xml')
root = tree.getroot()
childrens = root.getchildren()


for p in root.findall('WGTemplate'):
        name = p.find('TemplateName').text
        # print(name)
        loc = p.find('Filename').text
        # print(loc)
        for p1 in p.findall('Product'):
            print("{:<50}{:<50}{:>50}".format(name, loc, p1.text))
Program.cs
-----------
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用IronPython.Hosting;
使用IronPython.Modules;
命名空间运行外部脚本
{
班级计划
{
静态void Main(字符串[]参数)
{
WriteLine(“按enter键执行python脚本!”);
Console.ReadLine();
var py=Python.CreateEngine();
尝试
{
可执行文件(“wg.py”);
}
捕获(例外情况除外)
{
控制台写入线(
“哎呀!我们无法执行脚本,因为出现异常:”+ex.Message);
}
Console.WriteLine(“按enter键退出…”);
Console.ReadLine();
}
}
}
wg.py
-----
将xml.etree.cElementTree作为ET导入
tree=ET.parse('Wg.xml')
root=tree.getroot()
childrens=root.getchildrens()
对于root.findall('WGTemplate')中的p:
name=p.find('TemplateName')。text
#印刷品(名称)
loc=p.find('Filename')。文本
#打印(loc)
对于p.findall中的p1(“产品”):

print(“{:设置python路径,以了解系统路径 在.py文件中,只需键入

import sys
print(sys.path)
有了这个,你将得到你的sys路径,并设置所有路径

ScriptEngine engine = Python.CreateEngine(); //For Engine to initiate the script
            List<string> pathes = engine.GetSearchPaths().ToList();
            pathes.AddRange(new[]
            {
             @"<add your all path here>"
             });
            engine.SetSearchPaths(pathes);
ScriptEngine engine=Python.CreateEngine();//让引擎启动脚本
列表路径=engine.getSearchPath().ToList();
paths.AddRange(新[]
{
@""
});
引擎。设置搜索路径(路径);

我希望我的回答能帮助您解决问题。

脚本在独立运行时是否工作?是的。它工作得很好。唯一的挑战是与c#集成时。是否有任何标准库模块工作,或者它与etree隔离?看看您需要如何向托管的python引擎提供有关标准runtim位置的信息e、 Microsoft.Dynamic.dll中发生类型为“IronPython.Runtime.Exceptions.ImportException”的未处理异常。其他信息:无法从xml.eTree导入cElementTree。遵循上面发布的链接后,将引发上述异常。