gpt4 book ai didi

python - IronPython 在托管时如何加载模块?

转载 作者:太空宇宙 更新时间:2023-11-04 00:34:58 26 4
gpt4 key购买 nike

我对 IronPython 在托管时加载模块的方式感到困惑。

我使用的是使用 MSI 包安装的 IronPython 2.7.7,我引用了 C:\Program Files (x86)\IronPython 2.7\IronPython.dllC:\程序文件 (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 得到的搜索路径只包含

  1. "."
  2. "D:\\IronPythonTest\\bin\\Debug\\Lib"
  3. "D:\\IronPythonTest\\bin\\Debug\\DLLs"

而且我的 Debug 文件夹下既没有 Lib 也没有 DLLs 文件夹。

那么 IronPython 如何加载 datetime 模块呢?

预先感谢您的澄清。

最佳答案

您将相关路径添加到引擎的基本方法是正确的。

IronPython 使用了大量的标准库(只要有可能)。在 CPython 中 native 实现的低级模块在 IronPython 中用 C# 实现。其中之一modulesdatetime .因此,只要 IronPython.Modules.dll 存在,即使没有特定的加载/路径处理,也可以使用多个标准模块。

关于python - IronPython 在托管时如何加载模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44441574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com