我对IronPython在托管时加载模块的方式感到困惑。
我使用的是使用MSI软件包安装的IronPython 2.7.7,我引用了C:\Program Files (x86)\IronPython 2.7\IronPython.dll
和C:\Program Files (x86)\IronPython 2.7\Microsoft.Scripting.dll
。
有时IronPython无法加载抛出IronPython.Runtime.Exceptions.ImportException: 'No module named modulename'
的模块,但有时候一切正常。
例如,
var engine = Python.CreateEngine();
engine.CreateScriptSourceFromString("import datetime; print datetime.datetime.now()").Execute();
有效,但
var engine = Python.CreateEngine();
engine.CreateScriptSourceFromString("import json; print json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])").Execute();
不
我发现了在engine
中设置搜索路径的建议。所以我添加了
var searchPaths = engine.GetSearchPaths();
searchPaths.Add(@"C:\Program Files (x86)\IronPython 2.7\Lib");
engine.SetSearchPaths(searchPaths);
现在使用json
模块的示例正常工作。
但我从engine
获得的搜索路径仅包含
"."
"D:\\IronPythonTest\\bin\\Debug\\Lib"
"D:\\IronPythonTest\\bin\\Debug\\DLLs"
我的Lib
文件夹下没有DLLs
和Debug
个文件夹。
那么IronPython如何设法加载datetime
模块?
提前感谢您的澄清。