AWS Unable to import module 'app' : no module named Pymysql

Darth Vadr picture Darth Vadr · Aug 4, 2016 · Viewed 11.6k times · Source

I am using the AWS Console and trying to add data to a MySQL table using a Lambda function. Whenever I try to test the function, I get the following error:

Unable to import module 'app' : no module named pymysql

Its acting like pymysql is not in the path. I went into the microEC2 instance and pip'ed pymysql. But it still doesn't work. I have tried zipping my code into a file and uploading and also copying and pasting the code into the console and running it. Neither works. Any help would be greatly appreciated.

Here is a code snippet:

import sys

import logging

import pymysql

def main(event, context):

Answer

Yasel picture Yasel · Aug 4, 2016

You must package all your project dependency in your deployment.

  1. Copy from your virtual environment the package PyMysql, to the root of directory where your code lives.

  2. Create a zip, and upload it to lambda function

    See the docs