pallets / flask

The Python micro framework for building web applications.
https://flask.palletsprojects.com
BSD 3-Clause "New" or "Revised" License
68.02k stars 16.21k forks source link

pickle and cPickle can't find class from import #2489

Closed dav1nci closed 7 years ago

dav1nci commented 7 years ago

Expected Behavior

inside blueprints.flaskr.py from flask examples I add some code

from foo import Foo

@bp.route('/')
def index():
    a = Foo()
    print a
    # a is valid Foo object, no error here
    with open('some_pkl_file', 'rb') as f:
        # Foo object in some_pkl_file
        my_foo = cPickle.load(f)
    print my_foo
    # other things

Actual Behavior

cPickle and pickle can't find Foo. Even after I installed foo as package. Looks like import inside flask application imports packages to some special namespace or context, where pickle or cPicke can't find them. I open this issue because I spent a day and didn't find any information about this. If someone know how to fix this, help me =)

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/path/flaskr/flaskr/blueprints/flaskr.py", line 58, in index
    my_foo = cPickle.load(f)
AttributeError: 'module' object has no attribute 'Foo'

Environment

davidism commented 7 years ago

Looks like import inside flask application imports packages to some special namespace

It does not.

davidism commented 7 years ago

Please use Stack Overflow for questions about your own code. This tracker is for issues with Flask itself. Be sure to include a Minimal, Complete, and Verifiable Example.