无法在IronPython中使用DateTime

lit*_*rva 7 datetime ironpython

我在C#webapp中托管我的IronPython,如下所示:

var engine = Python.CreateEngine();
var scope = engine.CreateScope();
var script = Engine.CreateScriptSourceFromString(pythonCode, SourceCodeKind.Statements);
script.Execute(scope);
Run Code Online (Sandbox Code Playgroud)

我的python代码如下所示:

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

from System import DateTime
theDate = DateTime.Today()
Run Code Online (Sandbox Code Playgroud)

哪个会生成此错误:

IronPython.Runtime.Exceptions.ImportException: Cannot import name DateTime 
Run Code Online (Sandbox Code Playgroud)

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

我的IronPython运行时版本是v2.0.50727 - 我应该升级吗?我曾经想过DateTime会从早期的门进去吗?

Din*_*and 11

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

engine.Runtime.LoadAssembly(typeof(string).Assembly);
Run Code Online (Sandbox Code Playgroud)


Jon*_*eet 9

刚刚检查过,问题是你试图将今天称为方法而不是属性.试试这个(不需要添加对System.Core的引用):

import clr
from System import DateTime
theDate = DateTime.Today
print theDate
Run Code Online (Sandbox Code Playgroud)