How to run SQLAlchemy on AWS Lambda in Python

你离开我真会死。 提交于 2019-12-11 03:19:13

问题


I preapre very simple file for connecting to external MySQL database server, like below:

from sqlalchemy import *

def run(event, context):
    sql = create_engine('mysql://root:root@127.0.0.1/scraper?charset=utf8');
    metadata = MetaData(sql)

    print(sql.execute('SHOW TABLES').fetchall())

Doesn't work on AWS, but localy on Windows works perfectly.

Next, I install by pip install sqlalchemy --target my/dir and prepare ZIP file to upload packages to AWS Lambda.

Run, but with failed message No module named 'MySQLdb': ModuleNotFoundError.

Then, I use pip install mysqlclient --target my/dir, create ZIP and again upload to AWS Lambda.

Run, but with new failed message cannot import name '_mysql': ImportError.

So, what I should doing now?


回答1:


SQLAlchemy includes many Dialect implementations for various backends. Dialects for the most common databases are included with SQLAlchemy; a handful of others require an additional install of a separate dialect.

The MySQL dialect uses mysql-python as the default DBAPI. There are many MySQL DBAPIs available, including MySQL-connector-python and OurSQL

Instead of mysql you may use mysql+mysqlconnector

sql = create_engine('mysql+mysqlconnector://root:root@127.0.0.1/scraper?charset=utf8')

Then use:

pip install mysql-connector --target my/dir

Create Zip and again upload to AWS Lambda.



来源:https://stackoverflow.com/questions/54584232/how-to-run-sqlalchemy-on-aws-lambda-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!