Search code examples
pythondockerdockerfilepython-import

How to configure a PYTHONPATH env variable in the Dockerfile?


I am modifying the Machine Learning Inference to perform inference using my trained model and to this it is need to import a set of modules. So the project tree is now:

.
├── Dockerfile
├── __init__.py
├── app.py
├── requirements.txt
**
└── maskrcnn
    ├── config.py
    ├── __init__.py
    ├── m_rcnn.py
    ├── visualize.py
    ├── mask_rcnn_coco.h5
    └── model.py
**

Dockerfile:

# Pull the base image with python 3.8 as a runtime for your Lambda
FROM public.ecr.aws/lambda/python:3.8

# Copy the earlier created requirements.txt file to the container
COPY requirements.txt ./

# Install the python requirements from requirements.txt
RUN python3.8 -m pip install -r requirements.txt 

# Create the model directory for mounting the EFS 
RUN mkdir -p /mnt/ml

# Copy the earlier created app.py file to the container
COPY app.py ./
COPY maskrcnn/ ./maskrcnn


# Set the CMD to your handler
CMD ["app.lambda_handler"]

To import the python modules in app.py I used the following command line:

from maskrcnn.m_rcnn import *
from maskrcnn.visualize import random_colors, get_mask_contours, draw_mask

When running my docker image, I get an import error:

{"errorMessage": "Unable to import module 'app': No module named 'maskrcnn'", 

"errorType": "Runtime.ImportModuleError", "stackTrace": []}

My other problem is that in the module m_rcnn.py the file mask_rcnn_coco.h5 is accessed to do some procedures.

m_rcnn.py:

ROOT_DIR = os.path.abspath("/maskrcnn")


# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")

Because of this code I am getting the following error:

{"errorMessage": "[Errno 2] No such file or directory: 

'/maskrcnn/mask_rcnn_coco.h5'", "errorType": "FileNotFoundError"},

Solution

  • If you want to use the Python module maskrcnn from your lokal working directory, you have to copy it into the Docker image. Adding a COPY statement for maskrcnn/* could solve the issue:

    # Dockerfile
    
    # Copy the earlier created app.py file to the container
    COPY app.py ./
    COPY maskrcnn/ ./maskrcnn
    

    And please remove the line ENV PYTHONPATH.... I don't see any indication to mess around with PYTHONPATH.