You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dockerfile配置PYTHONPATH及解决模块导入与文件路径错误

Solution for Mask R-CNN Import and Model File Errors

Fix the "No module named 'maskrcnn'" Error

  • Create an empty __init__.py file inside your local maskrcnn directory. This tells Python to recognize the folder as an importable module.
  • In your Dockerfile, add an environment variable to include your project root in Python's module search path. Assuming your project files are copied to /app (a standard Docker setup), add this line:
    ENV PYTHONPATH="/app:$PYTHONPATH"
    
  • Ensure your Dockerfile sets the working directory before copying project files:
    WORKDIR /app
    COPY . .
    

Fix the "mask_rcnn_coco.h5 not found" Error

Choose one of the following options based on your setup:

  1. Copy local model file into Docker
    If you already have mask_rcnn_coco.h5 in your local maskrcnn folder, add this COPY command to your Dockerfile (after setting the working directory):

    COPY ./maskrcnn/mask_rcnn_coco.h5 /app/maskrcnn/mask_rcnn_coco.h5
    

    Update the path in m_rcnn.py to match: /app/maskrcnn/mask_rcnn_coco.h5, or use a relative path like ./mask_rcnn_coco.h5 if the script runs from the maskrcnn directory.

  2. Download model during Docker build
    If you don't have the file locally, add a RUN command to download the official pre-trained COCO model directly into the container (ensure wget is installed):

    RUN mkdir -p /app/maskrcnn && wget -O /app/maskrcnn/mask_rcnn_coco.h5 https://github.com/matterport/Mask_RCNN/releases/download/v2.0/mask_rcnn_coco.h5
    

Sample Complete Dockerfile Snippet

# Use a compatible Python base image
FROM python:3.9-slim

# Set working directory
WORKDIR /app

# Install required dependencies (adjust based on your project needs)
RUN pip install --no-cache-dir tensorflow keras pillow numpy opencv-python

# Configure PYTHONPATH
ENV PYTHONPATH="/app:$PYTHONPATH"

# Copy all project files into the container
COPY . .

# Uncomment below to download model if not present locally
# RUN mkdir -p /app/maskrcnn && wget -O /app/maskrcnn/mask_rcnn_coco.h5 https://github.com/matterport/Mask_RCNN/releases/download/v2.0/mask_rcnn_coco.h5

# Command to start your inference app
CMD ["python", "app.py"]

内容的提问来源于stack exchange,提问作者BodeTech

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 08:33:12