Conor
Conor

Reputation: 1527

Flask-SQLAlchemy with Google App Engine only works in interactive console

I am trying to get my Google App Engine app to work with Flask-SQLAlchemy. I am getting the error:

AttributeError: 'module' object has no attribute 'paramstyle'

I have followed the instructions in this answer (Local MySQLdb connection fails with AttributeError for paramstyle when running GAE development server) and so I no longer get the error in the interactive console (when I make an app context).

However, the error still appears when I run the app in my local GAE. Why is this still happening? I am running OSX Mavericks. The full trace:

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 266, in Handle
    result = handler(dict(self._environ), self._StartResponse)
  File "/Users/conor/Documents/Projj/lib/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/conor/Documents/Projj/lib/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Users/conor/Documents/Projj/lib/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/conor/Documents/Projj/lib/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/conor/Documents/Projj/lib/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/conor/Documents/Projj/lib/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/conor/Documents/Projj/lib/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/conor/Documents/Projj/lib/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/conor/Documents/Projj/financey.py", line 117, in login_view
    userObj = get_user(username)
  File "/Users/conor/Documents/Projj/financey.py", line 88, in get_user
    return db.session.query(User).filter_by(username=username).first()
  File "/Users/conor/Documents/Projj/lib/sqlalchemy/orm/scoping.py", line 149, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "/Users/conor/Documents/Projj/lib/sqlalchemy/util/_collections.py", line 864, in __call__
    return self.registry.setdefault(key, self.createfunc())
  File "/Users/conor/Documents/Projj/lib/flask_sqlalchemy/__init__.py", line 139, in __init__
    bind=db.engine,
  File "/Users/conor/Documents/Projj/lib/flask_sqlalchemy/__init__.py", line 780, in engine
    return self.get_engine(self.get_app())
  File "/Users/conor/Documents/Projj/lib/flask_sqlalchemy/__init__.py", line 797, in get_engine
    return connector.get_engine()
  File "/Users/conor/Documents/Projj/lib/flask_sqlalchemy/__init__.py", line 473, in get_engine
    self._engine = rv = sqlalchemy.create_engine(info, **options)
  File "/Users/conor/Documents/Projj/lib/sqlalchemy/engine/__init__.py", line 332, in create_engine
    return strategy.create(*args, **kwargs)
  File "/Users/conor/Documents/Projj/lib/sqlalchemy/engine/strategies.py", line 69, in create
    dialect = dialect_cls(**dialect_args)
  File "/Users/conor/Documents/Projj/lib/sqlalchemy/dialects/mysql/base.py", line 1985, in __init__
    default.DefaultDialect.__init__(self, **kwargs)
  File "/Users/conor/Documents/Projj/lib/sqlalchemy/engine/default.py", line 124, in __init__
    self.paramstyle = self.dbapi.paramstyle
AttributeError: 'module' object has no attribute 'paramstyle'

EDIT: In attempting to further diagnose the issue, when I import MySQLdb in the GAE Interactive Console (in the browser) I get the error ImportError: No module named _mysql

Upvotes: 0

Views: 1514

Answers (1)

Conor
Conor

Reputation: 1527

After looking around and debugging for far too long, I finally discovered the issue. When you run the GAE development server, the dev_appserver must be told exactly which MySQL instance you want to connect to. The connection string you use (e.g. in Python) doesn't matter. The connection string only determines database name and instance. However, SQLAlchemy can figure it out fine if you run form an ipython console. This may be due to a different driver being used under the hood.

Hopefully this saves someone else a lot of hair-pulling. Run your dev_appserver.py like so:

dev_appserver.py --mysql_host=<Cloud SQL IP> --mysql_user=root --mysql_password=<Cloud SQL root password> <application directory>

Upvotes: 2

Related Questions