AWS lambda: "Unable to import module 'app': No module named 'models'"

Viewed 3377

I want to deploy a pytorch model with aws sam. my folder structure looks as follows:

├── Dockerfile
├── __init__.py
├── app.py
├── models
│   ├── common.py
│   ├── experimental.py
│   └── yolo.py
├── requirements.txt
└── utils
    ├── autoanchor.py
    ├── datasets.py
    ├── general.py
    ├── google_utils.py
    ├── metrics.py
    ├── plots.py
    └── torch_utils.py

As you can see, I have several local dependencies. My Dockerfile looks as follows:

FROM public.ecr.aws/lambda/python:3.8

COPY app.py requirements.txt ./
ADD models utils ./

RUN python3.8 -m pip install -r requirements.txt -t .

# Command can be overwritten by providing a different command in the template directly.
CMD ["app.lambda_handler"]

The code in app.py imports modules from models. Unfortunately this doesn't work and produces the following error:

"errorMessage": "Unable to import module 'app': No module named 'models'"

I have also tried to use COPY instead of ADD for the directories models and utils, but it results in the same error. How can I fix this?

1 Answers

Thing you are missing is if you trying load folder as package you have to create proper packaging in Python. Else you can get all files one by one imported in your app.py

Steps to create package :

  1. Create folder name models
  2. Create __init__.py file inside models folder
  3. create your other files like yolo.py, experimental.py,common.py etc
  4. Then load all four into __init__.py file like below
from common import *
from experimental import *
from yolo import *

  1. Then you can use this in app.py like import models or from models import *

Note : Putting all files together into folder not work like package in Python.

Reference code : https://github.com/aviboy2006/flask-rest-api enter image description here

Related