Passing pickle between Python and IronPython
Asked Answered
M

2

9

I'm having difficulty loading a pickle in Python that was dumped in IronPython.

When I pickle something basic like "[1,2,3]" in IronPython, the pickle loads fine in Python. But, when I pickle results from the DB query below in IronPython, I get the error "No module named clr" when trying to load the pickle in Python.

What might be going wrong? (And is there a better way to share data between Python and IronPython?)

def GetData(id):
    TheConnection = SqlClient.SqlConnection("server=MyServer;database=MyDB;Trusted_Connection=yes")
    TheConnection.Open()
    sqlcommand = "MyStoredProcedure#GetMyData '%s'" %id

    MyAction = SqlClient.SqlCommand(sqlcommand, TheConnection)
    MyReader = MyAction.ExecuteReader()

    results = list()

    while MyReader.Read():
        row = {
               'Type':'LolCat',
               'Date':datetime.datetime(MyReader[1]),
               'Location':str(MyReader[3]),
               'Weight':float(MyReader[6])/float(MyReader[7]),
               }
        results.append(row)

    TheConnection.Close()
    MyReader.Close()

    return results



results1 = GetData(1234)
results2 = GetData(2345)

...
picklefile= open("testpickle","w")
cPickle.dump((results1,results2),picklefile)

...
picklefile = open("testpickle","r")
p = cPickle.load(file)  # this is the line that gives the error "ImportError: No module named clr"
Mcquade answered 12/7, 2012 at 18:14 Comment(3)
It looks like your IronPython install might have a module clr installed and your Cpython install doesn't have it.Maxfield
That is correct. CLR is .NET specific and not available as part of regular Python.Mcquade
It looks like those are all primitive types, so it shouldn't be a problem. Can you open an issue on ironpython.codeplex.com/WorkItem/Create and attach the pickle file, if possible? I'd like to know what types are involved.Palinode
C
4

Pickling is indeed a good general way to share data between pythons (when you can trust the data and know it hasn't been tampered with). But it's only really good for simple, builtin types. When you have objects of special types (such as whatever the results of GetData() are), then pickling will embed the fully-qualified name of the class, and just hope that the python on the other end knows how to find that class and reinstantiate it. Needless to say, this can get very messy if you're not careful.

Try converting your results1, results2 values to simple types, like (possibly nested) tuples, dicts, lists, etc.

Commodus answered 12/7, 2012 at 18:21 Comment(0)
R
2

The data you're pickling isn't made up of generic Python objects, but has some objects that are implementation-dependent. clr is an IronPython-specific module, as its name implies a connection to .NET. Looking at your data, I would guess this to be something with your datetime objects.

Try using a format other than datetime for your serialized data, like UNIX epoch time:

import time
results['Date'] = time.mktime(datetime.datetime(MyReader[1]).timetuple())
Rosmunda answered 12/7, 2012 at 18:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.