在IronPython中无法使用DateTime [英] Can't use DateTime in IronPython

查看:216
本文介绍了在IronPython中无法使用DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的python代码如下所示:



< pre class =lang-py prettyprint-override> import clr
clr.AddReference('System.Core')

从系统导入DateTime
theDate = DateTime.Today()

哪个生成此错误:

  IronPython.Runtime.Exceptions.ImportException:无法导入名称DateTime 

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



我的IronPython运行时版本是v2 .0.50727 - 我应该升级吗?我会认为 DateTime 会从早期的门开始吗?

解决方案

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

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


I'm hosting my IronPython in a C# webapp like so:

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

And my python code looks like this:

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

from System import DateTime
theDate = DateTime.Today()

Which generates this error:

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

I've spent some time on Google and most of the code I found doesn't seem to work anymore.

My IronPython Runtime Version is v2.0.50727 - should I be upgrading? I'd have thought DateTime would've been in from early doors though?

解决方案

Try adding a reference to mscorlib instead of System.Core. We changed the default hosting behavior at some point (2.0.1? 2.0.2?) so that this is done by default when hosting. You can do this from your hosting code with:

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

这篇关于在IronPython中无法使用DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆