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?
To create a date, we can use the datetime() class (constructor) of the datetime module. The datetime() class requires three parameters to create a date: year, month, day.
To remove timestamp, tzinfo has to be set None when calling replace() function.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With