问题描述
我对IronPython在托管时加载模块的方式感到困惑.
I'm confused about the way IronPython loads modules while being hosted.
我正在使用通过MSI软件包安装的IronPython 2.7.7,并且引用了C:\Program Files (x86)\IronPython 2.7\IronPython.dll
和C:\Program Files (x86)\IronPython 2.7\Microsoft.Scripting.dll
.
I'm using IronPython 2.7.7 installed using the MSI package and I've referenced C:\Program Files (x86)\IronPython 2.7\IronPython.dll
and C:\Program Files (x86)\IronPython 2.7\Microsoft.Scripting.dll
.
有时IronPython无法加载引发IronPython.Runtime.Exceptions.ImportException: 'No module named modulename'
的模块,但有时一切正常.
Sometimes IronPython fails to load a module throwing IronPython.Runtime.Exceptions.ImportException: 'No module named modulename'
, but sometimes everything works fine.
例如,
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
中设置搜索路径的建议.所以我添加了
I've found a suggestion to set search paths in engine
. So I've added
var searchPaths = engine.GetSearchPaths();
searchPaths.Add(@"C:\Program Files (x86)\IronPython 2.7\Lib");
engine.SetSearchPaths(searchPaths);
,现在使用json
模块的示例正常工作.
and now the example which uses json
module works.
但是我从engine
获得的搜索路径仅包含
But search paths which I get from engine
contain only
-
"."
-
"D:\\IronPythonTest\\bin\\Debug\\Lib"
-
"D:\\IronPythonTest\\bin\\Debug\\DLLs"
"."
"D:\\IronPythonTest\\bin\\Debug\\Lib"
"D:\\IronPythonTest\\bin\\Debug\\DLLs"
在我的Debug
文件夹下没有Lib
文件夹,也没有DLLs
文件夹.
And I don't have neither Lib
nor DLLs
folders under my Debug
folder.
那么IronPython如何设法加载datetime
模块?
So how does IronPython manage to load datetime
module?
谢谢您的澄清.
推荐答案
您向引擎添加相关路径的基本方法是正确的.
Your basic approach in adding the relevant paths to the engine is correct.
IronPython使用大量标准库(尽可能).在CPython中本地实现的低级模块在用于IronPython的C#中实现. modules 之一是 datetime .因此,只要IronPython.Modules.dll
存在,即使没有特定的加载/路径处理,也可以使用几个标准模块.
IronPython uses large amounts of the standard library (wherever possible). Low level modules that are implemented natively in CPython are implemented in C# for IronPython. One of those modules is datetime. So as long as IronPython.Modules.dll
is around, several standard modules are available even without specific loading/path handling.
这篇关于IronPython在托管时如何加载模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!