Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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):
like image 454
Darth Vadr Avatar asked Aug 03 '16 22:08

Darth Vadr


People also ask

How do I fix No module named PyMySQL?

The Python "ModuleNotFoundError: No module named 'pymysql'" occurs when we forget to install the PyMySQL module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install PyMySQL command.

How do I resolve the unable to import module error that I receive when I run Lambda code in Python?

To resolve this error, create a deployment package or Lambda layer that includes the libraries that you want to use in your Python code for Lambda. Important: Make sure that you put the library that you import for Python inside the /python folder.

How do I import a module in Lambda?

Step 1: Go to the AWS management console. Step 2: Click on create function. Step 5: Now try importing the requests module in your lambda function. So, create an event named “myevent” by clicking the down arrow on the test button.


1 Answers

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

like image 150
Yasel Avatar answered Sep 24 '22 04:09

Yasel