Forums

Flask app error AttributeError: '_RequestGlobals' object has no attribute 'db'

I get the following traceback message. I followed the advice at https://www.pythonanywhere.com/wiki/#Common_problems_for_specific_web_frameworks and commented out the app.run() and also corrected the pointer to the database. The problem does seem to be related to the database but I was able to run my database using sqlite3 in the console. Searched the forums and didn't find anything exactly like what I have...

Upon further investigation, I think maybe I don't have the sqlite3 package? (Maybe it doesn't exist? my app calls it.) When I try to pip install sqlite3, it doesn't work.Even pip search gives me the error File "/usr/lib/python2.7/socket.py", line 571, in create_connection raise err error: [Errno 111] Connection refused

2014-10-29 02:08:52,457 :Traceback (most recent call last):
2014-10-29 02:08:52,457 :  File "/bin/user_wsgi_wrapper.py", line 130, in __call__
2014-10-29 02:08:52,457 :    self.error_log_file.logger.exception("Error running WSGI application")
2014-10-29 02:08:52,458 :  File "/usr/lib/python2.7/logging/__init__.py", line 1183, in exception
2014-10-29 02:08:52,458 :    self.error(msg, *args, **kwargs)
2014-10-29 02:08:52,458 :  File "/usr/lib/python2.7/logging/__init__.py", line 1176, in error
2014-10-29 02:08:52,458 :    self._log(ERROR, msg, args, **kwargs)
2014-10-29 02:08:52,458 :  File "/usr/lib/python2.7/logging/__init__.py", line 1268, in _log
2014-10-29 02:08:52,458 :    record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
2014-10-29 02:08:52,458 :  File "/usr/lib/python2.7/logging/__init__.py", line 1242, in makeRecord
2014-10-29 02:08:52,459 :    rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
2014-10-29 02:08:52,459 :  File "/usr/lib/python2.7/logging/__init__.py", line 284, in __init__
2014-10-29 02:08:52,459 :    self.threadName = threading.current_thread().name
2014-10-29 02:08:52,459 :  File "/usr/lib/python2.7/threading.py", line 1158, in currentThread
2014-10-29 02:08:52,459 :    return _active[_get_ident()]
2014-10-29 02:08:52,459 :  File "/bin/user_wsgi_wrapper.py", line 122, in __call__
2014-10-29 02:08:52,459 :    app_iterator = self.app(environ, start_response)
2014-10-29 02:08:52,460 :  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1701, in __call__
2014-10-29 02:08:52,460 :    return self.wsgi_app(environ, start_response)
2014-10-29 02:08:52,460 :  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1685, in wsgi_app
2014-10-29 02:08:52,460 :    with self.request_context(environ):
2014-10-29 02:08:52,460 :  File "/usr/local/lib/python2.7/dist-packages/flask/ctx.py", line 274, in __enter__
2014-10-29 02:08:52,460 :    self.push()
2014-10-29 02:08:52,461 :  File "/usr/local/lib/python2.7/dist-packages/flask/ctx.py", line 221, in push
2014-10-29 02:08:52,461 :    top.pop()
2014-10-29 02:08:52,461 :  File "/usr/local/lib/python2.7/dist-packages/flask/ctx.py", line 257, in pop
2014-10-29 02:08:52,461 :    self.app.do_teardown_request(exc)
2014-10-29 02:08:52,461 :  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1587, in do_teardown_request
2014-10-29 02:08:52,461 :    rv = func(exc)
2014-10-29 02:08:52,461 :  File "/home/cluelesscoder/workprototype/workprototype2.py", line 30, in close_db_connection
2014-10-29 02:08:52,463 :    g.db.close()
2014-10-29 02:08:52,463 :  File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 336, in __getattr__
2014-10-29 02:08:52,463 :    return getattr(self._get_current_object(), name)
2014-10-29 02:08:52,463 :AttributeError: '_RequestGlobals' object has no attribute 'db'
2014-10-29 02:08:59,276 :Traceback (most recent call last):
2014-10-29 02:08:59,276 :  File "/bin/user_wsgi_wrapper.py", line 130, in __call__
2014-10-29 02:08:59,276 :    self.error_log_file.logger.exception("Error running WSGI application")
2014-10-29 02:08:59,276 :  File "/usr/lib/python2.7/logging/__init__.py", line 1183, in exception
2014-10-29 02:08:59,276 :    self.error(msg, *args, **kwargs)
2014-10-29 02:08:59,276 :  File "/usr/lib/python2.7/logging/__init__.py", line 1176, in error
2014-10-29 02:08:59,277 :    self._log(ERROR, msg, args, **kwargs)
2014-10-29 02:08:59,277 :  File "/usr/lib/python2.7/logging/__init__.py", line 1268, in _log
2014-10-29 02:08:59,277 :    record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
2014-10-29 02:08:59,277 :  File "/usr/lib/python2.7/logging/__init__.py", line 1242, in makeRecord
2014-10-29 02:08:59,277 :    rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
2014-10-29 02:08:59,277 :  File "/usr/lib/python2.7/logging/__init__.py", line 284, in __init__
2014-10-29 02:08:59,278 :    self.threadName = threading.current_thread().name
2014-10-29 02:08:59,278 :  File "/usr/lib/python2.7/threading.py", line 1158, in currentThread
2014-10-29 02:08:59,278 :    return _active[_get_ident()]
2014-10-29 02:08:59,278 :  File "/bin/user_wsgi_wrapper.py", line 122, in __call__
2014-10-29 02:08:59,278 :    app_iterator = self.app(environ, start_response)
2014-10-29 02:08:59,278 :  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1701, in __call__
2014-10-29 02:08:59,278 :    return self.wsgi_app(environ, start_response)
2014-10-29 02:08:59,279 :  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1689, in wsgi_app
2014-10-29 02:08:59,279 :    response = self.make_response(self.handle_exception(e))
2014-10-29 02:08:59,279 :  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1687, in wsgi_app
2014-10-29 02:08:59,279 :    response = self.full_dispatch_request()
2014-10-29 02:08:59,279 :  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1360, in full_dispatch_request
2014-10-29 02:08:59,280 :    rv = self.handle_user_exception(e)
2014-10-29 02:08:59,280 :  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1356, in full_dispatch_request
2014-10-29 02:08:59,280 :    rv = self.preprocess_request()
2014-10-29 02:08:59,280 :  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1539, in preprocess_request
2014-10-29 02:08:59,280 :    rv = func()
2014-10-29 02:08:59,281 :  File "/home/cluelesscoder/workprototype/workprototype2.py", line 25, in before_request
2014-10-29 02:08:59,281 :    g.db = connect_db()
2014-10-29 02:08:59,281 :  File "/home/cluelesscoder/workprototype/workprototype2.py", line 21, in connect_db
2014-10-29 02:08:59,282 :    return sqlite3.connect(app.config['DATABASE'])
2014-10-29 02:08:59,282 :sqlite3.OperationalError: unable to open database file

Are you using the full, absolute path to your database?

Hey harry - thanks, you were right on. I was missing the first forward slash on the path (so I had home/... rather than /home/). Glenn also mentioned a possible attribute issue and I tweaked something related to that before finding the path issue, so that may have been involved. Thanks!

clueless - I'm having this same problem, but fixing the path didn't solve it... what else did you do that you think may have fixed it?

Thanks for your help.

Replied on the other thread- see if you agree/disagree/if it was helpful?