Vince M
Vince M

Reputation: 1196

Python serverless: ModuleNotFoundError

I'm trying to use serverless framework with a python project. I created a hello world example that I run in offline mode. It works well but when I try to import a python package I get ModuleNotFoundError.

Here is my serverless.yaml file:

service: my-test
frameworkVersion: "3"

provider:
  name: aws
  runtime: python3.8

functions:
  hello:
    handler: lambdas.hello.hello
    events:
      - http:
          path: /hello
          method: get
plugins:
  - serverless-python-requirements
  - serverless-offline

In lambdas.hello.py:

import json
import pandas


def hello(event, context):
    body = {
        "message": 'hello world',
    }

    response = {"statusCode": 200, "body": json.dumps(body)}

    return response

In my Pipfile:

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
pandas = "*"

[requires]
python_version = "3.8"

To run it, I use the command $ sls offline start Then When I query on postman http://localhost:3000/dev/hello I get the error ModuleNotFoundError. If I remove the line import pandasin hello.py file, it works.

I don't understand why I get this error as serverless-python-requirements is supposed to check the pipfile and pandas is in my pipfile.

How can I use pandas (or any other python package) in my lambdas with serverless framework in offline mode ?

Upvotes: 2

Views: 2628

Answers (2)

kumar harshit
kumar harshit

Reputation: 672

The serverless-python-requirements plugin is used to bundle your dependencies and package them for deployment. This only comes to effect when you run sls deploy. From the plugin page - The plugin will now bundle your python dependencies specified in your requirements.txt or Pipfile when you run sls deploy

Read more about python packaging here - https://www.serverless.com/blog/serverless-python-packaging

Since you are running your service locally, this plugin will not be used. Your dependencies need to be installed locally. perform the below steps to make it work -

  • Create a virtual environment in you serverless directory.

  • install the plugin serverless plugin install -n serverless-offline

  • install pandas using pip

  • run sls offline start

Upvotes: 1

TheSmartMonkey
TheSmartMonkey

Reputation: 1052

Your lambda function don't have the panda module installed

You need to use the serverless-python-requirements plugin : https://www.serverless.com/plugins/serverless-python-requirements. To use it you need docker on your machine and to create a requirement.txt file in your service with the packages you need in your lambda

Upvotes: 0

Related Questions