在Python中将MySQL与AWS Lambda一起使用时出现问题 [英] Problems using MySQL with AWS Lambda in Python

查看:206
本文介绍了在Python中将MySQL与AWS Lambda一起使用时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AWS Lambda Python(Python初学者)启动并运行,但是在包括MySQL依赖项方面存在一些问题.我正在尝试按照说明此处在我的Mac上.

I am trying to get up and running with AWS Lambda Python (beginner in Python btw) but having some problems with including MySQL dependency. I am trying to follow the instructions here on my Mac.

对于第3步,在我的项目根目录执行命令遇到一些问题

For step number 3, I am getting some problems with doing the command at the root of my project

sudo pip install MySQL-python -t /

错误:

例外: 追溯(最近一次通话): 主文件122行中的文件"/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/basecommand.py" 状态= self.run(选项,参数) 运行中的文件"/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/commands/install.py",行311 os.path.join(options.target_dir,项目) 移动中的文件"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py",第292行 引发错误,目标路径'%s'已经存在"%real_dst 错误:目标路径'/MySQL_python-1.2.5-py2.7.egg-info/MySQL_python-1.2.5-py2.7.egg-info'已经存在

Exception: Traceback (most recent call last): File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/basecommand.py", line 122, in main status = self.run(options, args) File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/commands/install.py", line 311, in run os.path.join(options.target_dir, item) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 292, in move raise Error, "Destination path '%s' already exists" % real_dst Error: Destination path '/MySQL_python-1.2.5-py2.7.egg-info/MySQL_python-1.2.5-py2.7.egg-info' already exists

我最终编写了以下lambda函数(在Mac上可以正常工作),即

I end up writing my following lambda function (works fine on my Mac), which is:

import MySQLdb

def lambda_handler(event, context):
   # Open database connection
   db = MySQLdb.connect(...)

   # prepare a cursor object using cursor() method
   cursor = db.cursor()

   sql = "SELECT * FROM Users"

   try:
      # Execute the SQL command
      cursor.execute(sql)
      # Fetch all the rows in a list of lists.
      results = cursor.fetchall()
      for row in results:
         fname = row[0]
         lname = row[1]
         age = row[2]
         sex = row[3]
         income = row[4]
         # Now print fetched result
         print ("lname=%s" %(lname))
   except:
      print "Error: unable to fecth data"

   # disconnect from server
   db.close()

我要做的是转到/Library/Python/2.7/site-packages,并复制当我sudo pip安装MySQL-python时下载的MySQLdb文件夹/文件(不带-t/)(我确定我在这里对lambda项目做错了事,然后将内容与lambda_function.py一起压缩并上传到AWS Lambda.

What I went on to do is go to /Library/Python/2.7/site-packages and copying over the the MySQLdb folders/files that were downloaded when I did sudo pip install MySQL-python (without -t /) (I'm sure I'm doing something wrong here), to my lambda project, and then zipped the content along with the lambda_function.py and uploaded to AWS Lambda.

然后我得到:

无法导入模块"lambda_function":没有名为MySQLdb的模块

Unable to import module 'lambda_function': No module named MySQLdb

感谢您的任何帮助和建议!

Grateful for any help and suggestions!

编辑

能够使sudo pip安装MySQL-python -t/pathToProject工作(感谢注释中的帮助),但是现在我在运行lambda函数时得到了此提示:

Was able to do make sudo pip install MySQL-python -t /pathToProject work (thanks for the help in the comments) but now I get this when runing the lambda function:

无法导入模块"lambda_function":/var/task/_mysql.so:无效的ELF标头

Unable to import module 'lambda_function': /var/task/_mysql.so: invalid ELF header

我知道,如果我在Linux机器上工作,那么它应该可以正常工作(有人建议),但是我想知道是否可以在OS X机器上使它工作.

I know that if I work on a Linux box, then it should work fine (as suggested by some people), but I am wondering if I can make it work from an OS X box.

推荐答案

对于像Lambda这样的用例,使用像

For a use case like Lambda you'll be a lot happier using a pure python implementation like PyMySQL.

Python数据库API 之后,这是对MySQLdb的替代.规格.对于大多数事件,如触发的Lambda事件,它的速度将一样快.

It's a drop in replacement for MySQLdb that follows the Python Database API specification. For most things like triggered Lambda events it will be just as fast.

我在生产中使用了很多,效果很好.

I've used it in production a lot and it works great.

这篇关于在Python中将MySQL与AWS Lambda一起使用时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆