python的lambda和for一起使用,在Python中将MySQL与AWS Lambda一起使用时出现问题

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.

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 /

Error:

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

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()

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.

Then I get:

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

Grateful for any help and suggestions!

EDIT

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:

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

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.

解决方案

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值