Forums

No module named copy_reg, cPickle with a Flask app on Python 2.7

I am building a simple api with Flask 0.9 and Python 2.7. I have a serialized object that my program uses that I packed on my computer with cPickle. Now, when trying to open it in my app on PA, I get this import error.

2015-03-12 03:35:18,931 :Exception on / [POST]
Traceback (most recent call last):
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1687, in wsgi_app
        response = self.full_dispatch_request()
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1360, in full_dispatch_request
        rv = self.handle_user_exception(e)
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1358, in full_dispatch_request
        rv = self.dispatch_request()
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1344, in dispatch_request
        return self.view_functions[rule.endpoint](**req.view_args)
    File "/home/set2/forest/flask_app.py", line 28, in index
        return handle_post()
    File "/home/set2/forest/flask_app.py", line 50, in handle_post
        dur, dist, steps)
    File "/home/set2/forest/flask_app.py", line 79, in calculate_result
        forest = cPickle.load(f)
ImportError: No module named copy_reg

I have all of the correct import statements, and I'm not getting any import error. Is there a weird quirk with pickle that makes it not work on PA? I'd really like to get this running.

EDIT: everything works fine on my development machine, and I'm 99% sure that I'm using the same versions of every package.

Just to clarify- are you pickling objects on your local windows machine and trying to load it on PythonAnywhere (a linux machine)?

There are a couple tweaks you will have to do according to this stackoverflow post.

The pickling is being done on a linux machine. I have been writing it in binary mode.

with open('model.cpickle', 'wb') as f:
    cPickle.dump(forest, f)

I figured out the issue. There was a problem when copying the pickled file to the server. Now I'm just getting package errors. Now when trying to update to the right package versions, its taking forever, and my process is in the tarpit :(

time to upgrade! :p

don't worry the tarpit gets reset daily.

And I solved the package errors too :D I slogged through the tarpit and got everything installed in a virtualenv. (numpy takes FOREVER to install)