datetime - 不能在 IronPython 中使用 DateTime

标签 datetime ironpython

我在 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 

我在谷歌上花了一些时间,我发现的大部分代码似乎不再起作用了。

我的 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/1472553/

相关文章:

java - Joda 中的“负”持续时间,是错误还是功能?

c# 解析 UTC 日期时间

基于 C# 正则表达式的日期解析和提取

.net - 如何在 .NET 应用程序中嵌入 IronPython

wpf - 向提示 "Window is not supported in a WPF project"添加新的IronPython WPF窗口

python - 通过 Python 导出带有标题和日期格式的 CSV

MySQL - 使用 current_date 和特定时间创建日期时间

python - 使用 IronPython 中的 ManagementClass.Getinstances()

python - 删除 spotfire 数据表 Ironpython 中的所有行

c# - 使用 IronPython 托管时如何将 __name__ 设置为 '__main__'?