简体   繁体   中英

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

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

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

  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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM