AWS Unable to import module 'app' : no module named Pymysql
Asked Answered
A

3

6

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):
Akela answered 3/8, 2016 at 22:19 Comment(0)
S
3

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

Solus answered 3/8, 2016 at 22:44 Comment(1)
Thanks for this. It worked! Now I have to figure out why its failing on pymysql.connect. But that will be a separate question ;)Akela
R
0
  1. mkdir pymysql-layer/
  2. cd pymysql-layer/
  3. put in the following files:

    • get_layer_packages.sh

    export PKG_DIR="python"

    rm -rf ${PKG_DIR} && mkdir -p ${PKG_DIR}

    docker run --rm -v $(pwd):/foo -w /foo lambci/lambda:build-python3.6 \ pip install -r requirements.txt --no-deps -t ${PKG_DIR}

    • requirements.txt

    pymysql

    sqlalchemy

  4. then run : chmod +x get_layer_packages.sh

  5. then run : ./get_layer_packages.sh
  6. finally, run 'zip -r mysql.zip .

Prefer uploading to S3 and then import s3:// path into the lambda layer while creating it

Redwine answered 23/3, 2020 at 16:41 Comment(0)
T
0

You need to create a lambda layer with a zip file containing all the dependencies for the required packages.

If you are on windows you should probably create the zip file using docker, otherwise do it on you your pc.

For me this did the trick:

apt install python3.9  
apt install python3-pip
mkdir -p build/python/lib/python3.9/site-packages
pip3 install PyMySQL -t build/python/lib/python3.9/site-packages
cd build
zip -r layer.zip *

Open your lambda in the aws console and 'add a layer' at the bottom of the screen. choose 'create a new layer' and upload your zip file.

Go back to the 'add a layer' page and choose 'add a custom layer' with your layer.

Tedium answered 11/8, 2022 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.