0

I'm very new to flask & Google App Engine. I'm able to run this locally fine and it's building/deploying fine on Google, but when I hit the URL it returns a 500. Nothing shows up in the error logs, when I view the logs for the path im testing ('/'), I can see some errors.

main.py:

from flask import Flask
from flask import jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS

# instantiated flask application
app = Flask(__name__) # __name__ is the name of the module
CORS(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mydb'
db = SQLAlchemy(app)

@app.route('/')
def hello_world():
    results = db.session.execute('SELECT * FROM users')
    print('got results', results)
    users = [{"name": "sam"}]
    # for r in results:
    #     new = {
    #         "user_id": r['user_id']
    #     }
    #     users.append(new)

    return jsonify(users)


if __name__ == '__main__':
    app.run()

app.yaml:

runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /static
  static_dir: static
- url: /.*
  script: main.app

libraries:
- name: ssl
  version: latest

requirements.txt


Flask==0.12.2
Flask-SQLAlchemy==2.4.0
Flask-Cors==3.0.3
psycopg2==2.5.2

I didn't originally include psycopg2 in my requirements, but i saw Google complaining it couldn't find the module. I thought maybe flask-sqlalchemy is using it?

Anyways, now it is saying:

ImportError: dynamic module does not define init function (init_psycopg)

2019-08-02 09:25:52.287 CDT /base/data/home/apps/s~flaskappevents/20190802t092443.420035961696766896/lib/flask_sqlalchemy/init.py:835: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning. 2019-08-02 09:25:52.287 CDT
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and ' 2019-08-02 09:25:52.462 CDT [2019-08-02 14:25:52,448] ERROR in app: Exception on / [GET] 2019-08-02 09:25:52.462 CDT Traceback (most recent call last): 2019-08-02 09:25:52.462 CDT File "/base/data/home/apps/s~flaskappevents/20190802t092443.420035961696766896/lib/flask/app.py", line 1982, in wsgi_app 2019-08-02 09:25:52.462 CDT response = self.full_dispatch_request() 2019-08-02 09:25:52.462 CDT File "/base/data/home/apps/s~flaskappevents/20190802t092443.420035961696766896/lib/flask/app.py", line 1614, in full_dispatch_request 2019-08-02 09:25:52.462 CDT rv = self.handle_user_exception(e) 2019-08-02 09:25:52.462 CDT File "/base/data/home/apps/s~flaskappevents/20190802t092443.420035961696766896/lib/flask/app.py", line 1517, in handle_user_exception 2019-08-02 09:25:52.462 CDT reraise(exc_type, exc_value, tb) 2019-08-02 09:25:52.462 CDT File "/base/data/home/apps/s~flaskappevents/20190802t092443.420035961696766896/lib/flask/app.py", line 1612, in full_dispatch_request 2019-08-02 09:25:52.462 CDT rv = self.dispatch_request() 2019-08-02 09:25:52.462 CDT File "/base/data/home/apps/s~flaskappevents/20190802t092443.420035961696766896/lib/flask/app.py", line 1598, in dispatch_request 2019-08-02 09:25:52.462 CDT return self.view_functionsrule.endpoint 2019-08-02 09:25:52.462 CDT File "/base/data/home/apps/s~flaskappevents/20190802t092443.420035961696766896/main.py", line 21, in hello_world 2019-08-02 09:25:52.462 CDT results = db.session.execute('SELECT * FROM users') 2019-08-02 09:25:52.462 CDT File "/base/data/home/apps/s~flaskappevents/20190802t092443.420035961696766896/lib/sqlalchemy/orm/scoping.py", line 162, in do 2019-08-02 09:25:52.462 CDT return getattr(self.registry(), name)(*args, **kwargs) 2019-08-02 09:25:52.462 CDT File "/base/data/home/apps/s~flaskappevents/20190802t092443.420035961696766896/lib/sqlalchemy/util/collections.py", line 1012, in call 2019-08-02 09:25:52.462 CDT return self.registry.setdefault(key, self.createfunc()) 2019-08-02 09:25:52.462 CDT File "/base/data/home/apps/s~flaskappevents/20190802t092443.420035961696766896/lib/sqlalchemy/orm/session.py", line 3214, in call 2019-08-02 09:25:52.462 CDT return self.class(**local_kw) 2019-08-02 09:25:52.462 CDT File "/base/data/home/apps/s~flaskappevents/20190802t092443.420035961696766896/lib/flask_sqlalchemy/init.py", line 138, in init 2019-08-02 09:25:52.462 CDT bind = options.pop('bind', None) or db.engine 2019-08-02 09:25:52.462 CDT File "/base/data/home/apps/s~flaskappevents/20190802t092443.420035961696766896/lib/flask_sqlalchemy/init.py", line 937, in engine 2019-08-02 09:25:52.462 CDT return self.get_engine() 2019-08-02 09:25:52.462 CDT File "/base/data/home/apps/s~flaskappevents/20190802t092443.420035961696766896/lib/flask_sqlalchemy/init.py", line 956, in get_engine 2019-08-02 09:25:52.462 CDT return connector.get_engine() 2019-08-02 09:25:52.462 CDT File "/base/data/home/apps/s~flaskappevents/20190802t092443.420035961696766896/lib/flask_sqlalchemy/init.py", line 561, in get_engine 2019-08-02 09:25:52.462 CDT self._engine = rv = self._sa.create_engine(sa_url, options) 2019-08-02 09:25:52.462 CDT File "/base/data/home/apps/s~flaskappevents/20190802t092443.420035961696766896/lib/flask_sqlalchemy/init.py", line 966, in create_engine 2019-08-02 09:25:52.462 CDT return sqlalchemy.create_engine(sa_url, **engine_opts) 2019-08-02 09:25:52.462 CDT File "/base/data/home/apps/s~flaskappevents/20190802t092443.420035961696766896/lib/sqlalchemy/engine/init.py", line 435, in create_engine 2019-08-02 09:25:52.462 CDT return strategy.create(*args, **kwargs) 2019-08-02 09:25:52.462 CDT File "/base/data/home/apps/s~flaskappevents/20190802t092443.420035961696766896/lib/sqlalchemy/engine/strategies.py", line 87, in create 2019-08-02 09:25:52.462 CDT dbapi = dialect_cls.dbapi(**dbapi_args) 2019-08-02 09:25:52.462 CDT File "/base/data/home/apps/s~flaskappevents/20190802t092443.420035961696766896/lib/sqlalchemy/dialects/postgresql/psycopg2.py", line 653, in dbapi 2019-08-02 09:25:52.462 CDT import psycopg2 2019-08-02 09:25:52.463 CDT File "/base/data/home/apps/s~flaskappevents/20190802t092443.420035961696766896/lib/psycopg2/init.py", line 50, in 2019-08-02 09:25:52.463 CDT from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID 2019-08-02 09:25:52.463 CDT ImportError: dynamic module does not define init function (init_psycopg) 2019-08-02 09:25:52.473 CDT This request caused a new process to be started for your application, and thus caused your application code to be loaded for the first time. This request may thus take longer and use more CPU than a typical request for your application.

Asdfg
  • 11,362
  • 24
  • 98
  • 175
sb9
  • 266
  • 7
  • 22
  • What's the plan with SQLAlchemy? what are you using as your DB? – Alex Aug 02 '19 at 16:51
  • I had issues getting psycopg2 to install on my machine, so I’m using SQLAlchemy. It’s a Postgres dB hosted through heroku. It’s connecting fine in the code on my Mac. – sb9 Aug 02 '19 at 17:04
  • you'll have an easier time if you have your postgres DB within Google Cloud SQL https://cloud.google.com/sql/docs/postgres/quickstart. In the comments on this question https://stackoverflow.com/questions/43342835/psycopg2-error-on-google-app-engine they say `App Engine only supports Postgres connections through its native CloudSQL platform` However that question is a bit old, so its possible that things are different now. Another option is using App Engine Flex instead of App Engine Standard. – Alex Aug 02 '19 at 17:19
  • My understanding is that there is a cost associated with using CloudSQL solutions. I also found this page (one exists for flexible as well) that is saying third party connections are possible https://cloud.google.com/appengine/docs/standard/go/using-third-party-databases – sb9 Aug 02 '19 at 17:25

1 Answers1

0

I found a similar case here Google App Engine Import Error and the problem was that in the app.yaml file it was missing the type of DB. The user there, it was using MySQLdb but I think you can try the solution given in the link with SQLAlchemy.

Samuel Romero
  • 1,233
  • 7
  • 12