我正在尝试从 C# 应用程序中执行 Python 脚本,但当它尝试启动该脚本时,我收到错误 ImportException 未处理
No module name csv
。我检查了 Ironpython 文件夹,有一个 csv.py 文件...?
我用来调用脚本的代码:
IDictionary<string, object> options = new Dictionary<string, object>();
options["Argument"] = new[] { filePath, profile };
var pyEngine = Python.CreateEngine(options);
var pyScope = pyEngine.CreateScope();
string script = "xccdf-xml2tsv.py";
pyScope.SetVariable(profile, options);
pyEngine.ExecuteFile(script, pyScope);
Python 文件:
#!/usr/bin/env python
###
# (C) 2010 Adam Crosby
# Licensed under:
# http://creativecommons.org/licenses/by-nc-sa/3.0/
##
import csv
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import xml.etree.ElementTree as ET
xmlns = "http://checklists.nist.gov/xccdf/1.1"
...
最佳答案
您创建的 IronPython 引擎不知道它应该使用的标准库实现。您可以通过添加类似的内容来指定它
var paths = pyEngine.GetSearchPaths();
paths.Add(@"C:\Program Files (x86)\IronPython 2.7\Lib");
pyEngine.SetSearchPaths(paths);
您可以将其指向本地安装的 Iron Python(如我的示例中所示)或使用 appropriate NuGet package直接将其添加到您的项目中。您可能还需要将其部署到输出文件夹/安装程序/...
另请注意this answer以及this answer and comments因为他们可能会提供额外的信息。
关于c# - C# 中的 Ironpython 导入模块导入 csv 模块时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27472188/