IronPython在托管时如何加载模块? [英] How does IronPython loads modules while being hosted?

查看:100
本文介绍了IronPython在托管时如何加载模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对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.dllC:\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

  1. "."
  2. "D:\\IronPythonTest\\bin\\Debug\\Lib"
  3. "D:\\IronPythonTest\\bin\\Debug\\DLLs"
  1. "."
  2. "D:\\IronPythonTest\\bin\\Debug\\Lib"
  3. "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 之一是

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在托管时如何加载模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆