问题
I'm hosting a website/app using Flask/SQLAlchemy on Google's App Engine, part of which entails communicating with a PostgreSQL server and querying it to populate the website.
The SQL server is hosted on Google's Compute Engine as a VM instance per these instructions, but with the server open to all connections.
When I send a GET request to the website/app when it is hosted on localhost, I get the data I expect. However, when I launch the app and attempt to send the same request to the new URL, I get an HTTP 500 error. In GCP's logging section, I see the request and a stack trace containing the following:
(/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py:279)
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 267, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "/base/data/home/apps/s~cs373-idb/20170411t063743.400476890985925133/lib/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/base/data/home/apps/s~cs373-idb/20170411t063743.400476890985925133/lib/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/base/data/home/apps/s~cs373-idb/20170411t063743.400476890985925133/lib/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/base/data/home/apps/s~cs373-idb/20170411t063743.400476890985925133/lib/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/base/data/home/apps/s~cs373-idb/20170411t063743.400476890985925133/lib/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/base/data/home/apps/s~cs373-idb/20170411t063743.400476890985925133/lib/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/base/data/home/apps/s~cs373-idb/20170411t063743.400476890985925133/lib/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/base/data/home/apps/s~cs373-idb/20170411t063743.400476890985925133/lib/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/base/data/home/apps/s~cs373-idb/20170411t063743.400476890985925133/main.py", line 108, in get_book1
for item in models.Book.query.all():
File "/base/data/home/apps/s~cs373-idb/20170411t063743.400476890985925133/lib/flask_sqlalchemy/__init__.py", line 498, in __get__
return type.query_class(mapper, session=self.sa.session())
File "/base/data/home/apps/s~cs373-idb/20170411t063743.400476890985925133/lib/sqlalchemy/orm/scoping.py", line 78, in __call__
return self.registry()
File "/base/data/home/apps/s~cs373-idb/20170411t063743.400476890985925133/lib/sqlalchemy/util/_collections.py", line 990, in __call__
return self.registry.setdefault(key, self.createfunc())
File "/base/data/home/apps/s~cs373-idb/20170411t063743.400476890985925133/lib/sqlalchemy/orm/session.py", line 2829, in __call__
return self.class_(**local_kw)
File "/base/data/home/apps/s~cs373-idb/20170411t063743.400476890985925133/lib/flask_sqlalchemy/__init__.py", line 145, in __init__
bind = options.pop('bind', None) or db.engine
File "/base/data/home/apps/s~cs373-idb/20170411t063743.400476890985925133/lib/flask_sqlalchemy/__init__.py", line 922, in engine
return self.get_engine()
File "/base/data/home/apps/s~cs373-idb/20170411t063743.400476890985925133/lib/flask_sqlalchemy/__init__.py", line 941, in get_engine
return connector.get_engine()
File "/base/data/home/apps/s~cs373-idb/20170411t063743.400476890985925133/lib/flask_sqlalchemy/__init__.py", line 543, in get_engine
self._engine = rv = sqlalchemy.create_engine(info, **options)
File "/base/data/home/apps/s~cs373-idb/20170411t063743.400476890985925133/lib/sqlalchemy/engine/__init__.py", line 387, in create_engine
return strategy.create(*args, **kwargs)
File "/base/data/home/apps/s~cs373-idb/20170411t063743.400476890985925133/lib/sqlalchemy/engine/strategies.py", line 80, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "/base/data/home/apps/s~cs373-idb/20170411t063743.400476890985925133/lib/sqlalchemy/dialects/postgresql/psycopg2.py", line 554, in dbapi
import psycopg2
File "/base/data/home/apps/s~cs373-idb/20170411t063743.400476890985925133/lib/psycopg2/__init__.py", line 50, in <module>
from psycopg2._psycopg import ( # noqa
ImportError: dynamic module does not define init function (init_psycopg)
Given dummy data (i.e., having the app return some set data as opposed to querying the database,) the GET requests work fine, and TravisCI has no problem running tests which also involve querying the database using the same source, so I'm confident that the problem is with the database and its interaction with GAE.
I've read seemingly conflicting articles on Google's own website on whether or not PostgreSQL is supported by the App Engine. Is there a way to solve this?
If it helps, this is the lib folder that I've set for the app:
aniso8601 flask itsdangerous.py MarkupSafe-1.0.dist-info requests-2.13.0.dist-info SQLAlchemy-1.1.9.dist-info
aniso8601-1.2.0.dist-info Flask-0.12.1.dist-info itsdangerous.pyc psycopg2 requests_toolbelt werkzeug
click flask_restful jinja2 psycopg2-2.7.1.dist-info requests_toolbelt-0.7.1.dist-info Werkzeug-0.12.1.dist-info
click-6.7.dist-info Flask_RESTful-0.3.5.dist-info Jinja2-2.9.6.dist-info python_dateutil-2.6.0.dist-info six-1.10.0.dist-info
coverage flask_sqlalchemy markup pytz six.py
coverage-4.3.4.dist-info Flask_SQLAlchemy-2.2.dist-info markup-0.2.dist-info pytz-2017.2.dist-info six.pyc
dateutil itsdangerous-0.24.dist-info markupsafe requests sqlalchemy
These libraries were added from GCP's console with the command:
pip install -r requirements.txt -t lib
where requirements.txt contains this list:
Flask
Flask-SQLAlchemy
psycopg2
Markup
requests
flask_restful
requests-toolbelt
coverage
Finally, my python version is 2.7.9 and pip 9.0.1
Thank you in advance for taking the time to read this. Any advice would be immensely appreciated.
来源:https://stackoverflow.com/questions/43342835/psycopg2-error-on-google-app-engine