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