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

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?

Source: Docker Questions

LEAVE A COMMENT