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.

我正在尝试使用AWS Lambda Python (Python的初学者,顺便说一句)启动和运行,但是在包括MySQL依赖方面存在一些问题。我试着按照Mac上的说明操作。

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

对于步骤3,我在项目的根上执行命令时会遇到一些问题

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

例外:Traceback(最近一次调用):File“/Library/Python/2.7/site-packages -packages/pip-1.5.6-py2.7.egg/pip/basecommand。py",第122行,主状态= self.run(options, args)文件"/Library/Python/2.7/site-packages/pip- 1.6 -py2.7.egg/pip/commands/install。在run os.path.join(选项)中,第311行。目标路径'%s'已经存在" % real_dst错误:目标路径'/MySQL_python-1.2.5-py2.7 -py2.7. egginfo / mysql2.7 - 2.2.5 -py2.7。egg-info”已经存在

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

最后我编写了下面的lambda函数(在我的Mac上运行良好),即:

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.

我接下来要做的是访问/Library/Python/2.7/site-packages并复制MySQLdb文件夹/文件,这些文件是我在sudo pip安装MySQL-python(没有-t /)时下载的(我肯定我在这里做错了),然后将内容与lambda_function一起压缩。然后上传到AWS Lambda。

Then I get:

然后我得到:

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

无法导入模块“lambda_function”:没有名为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:

能够做sudo pip安装sql -python -t /pathToProject工作(感谢在评论中的帮助),但是现在我在运行lambda函数时得到了这个:

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

无法导入模块“lambda_function”:/var/task/_mysql。所以:无效的ELF头

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.

我知道,如果我使用Linux box,那么它应该可以正常工作(正如一些人建议的那样),但是我想知道我是否可以使用OS X box。

5 个解决方案

#1


6

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

对于像Lambda这样的用例,使用纯粹的python实现(如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.

这是对MySQLdb的替换,它遵循Python数据库API规范。对于大多数事情,如触发Lambda事件,它将同样快速。

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

我在生产中经常用到它,而且效果很好。

更多相关文章

  1. mysql python pymysql模块 增删改查 查询 fetchone
  2. linux下mysql配置文件my.cnf详解
  3. 可视化工具Navicat的使用/pymysql模块的使用
  4. SQLSTATE [HY000] [2002]没有这样的文件或目录
  5. mysql启动脚本——指定数据目录并指定配置文件
  6. 'mysql'不是内部或外部命令,也不是可运行的程序或批处理文件.
  7. 什么是最好的Node.js mysql模块通过ssl连接到mysql?
  8. 多个mysql数据库怎么指定到多个不同文件目录
  9. ubuntu_mysql怎么判断自己的库和头文件的位置

随机推荐

  1. PHP中 __set_state()方法详解
  2. php curl发送请求详细教程
  3. PHP中__set()方法详解
  4. PHP中__invoke()方法详解
  5. 编译安装 ProtoBuf 扩展
  6. PHP 数据加密的方法
  7. TP5.0 PHPExcel 数据表格导出导入
  8. PHP识别相片是否是颠倒的,并且重新摆正相
  9. 避坑!用 Docker 搞定 PHP 开发环境搭建
  10. php开发常见问题总结