gpt4 book ai didi

datetime - 不能在 IronPython 中使用 DateTime

转载 作者:太空宇宙 更新时间:2023-11-03 16:45:51 27 4
gpt4 key购买 nike

我像这样在 C# webapp 中托管我的 IronPython:

var engine = Python.CreateEngine();
var scope = engine.CreateScope();
var script = Engine.CreateScriptSourceFromString(pythonCode, SourceCodeKind.Statements);
script.Execute(scope);

我的 python 代码如下所示:

import clr
clr.AddReference('System.Core')

from System import DateTime
theDate = DateTime.Today()

产生此错误的原因:

IronPython.Runtime.Exceptions.ImportException: Cannot import name DateTime 

我在 Google 上花了一些时间,我发现的大部分代码似乎都不再有效了。

我的 IronPython 运行时版本是 v2.0.50727 - 我应该升级吗?不过,我本以为 DateTime 会早点进来?

最佳答案

尝试添加对 mscorlib 的引用而不是 System.Core。我们在某些时候更改了默认托管行为(2.0.1?2.0.2?),以便在托管时默认完成。您可以通过您的托管代码执行此操作:

engine.Runtime.LoadAssembly(typeof(string).Assembly);

关于datetime - 不能在 IronPython 中使用 DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6029435/

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