tags:

views:

368

answers:

2

Howdy gents,

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 tells me to f**k right off:

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?

Anthony

+4  A: 

Just checked, and the problem is that you're trying to call Today as a method instead of a property. Try this instead (no need to add a reference to System.Core):

import clr
from System import DateTime
theDate = DateTime.Today
print theDate
Jon Skeet
Looking at the question, the error seems to be stemming from an import failure, rather than the fact that "Today" is being called as a function.
Rohit
@Rohit: We can't really tell, as we haven't been told what the error message is. With the brackets on you do *get* an error message...
Jon Skeet
The exception was firing on the import command, so it hadn't even reached the Today call.Thanks for your help anyway though.
littlecharva
+5  A: 

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);
Dino Viehland
Thanks, that was spot on.
littlecharva