如何在Dockerfile中配置PYTHONPATH环境变量?

huangapple go评论61阅读模式
英文:

How to configure a PYTHONPATH env variable in the Dockerfile?

问题

以下是您要翻译的部分:

  • 项目结构如下:

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

    # 使用Python 3.8作为Lambda运行时的基本镜像
    FROM public.ecr.aws/lambda/python:3.8
    
    # 将之前创建的requirements.txt文件复制到容器中
    COPY requirements.txt ./
    
    # 从requirements.txt安装Python依赖
    RUN python3.8 -m pip install -r requirements.txt 
    
    # 创建用于挂载EFS的模型目录
    RUN mkdir -p /mnt/ml
    
    # 复制之前创建的app.py文件到容器中
    COPY app.py ./
    COPY maskrcnn/ ./maskrcnn
    
    # 设置CMD为您的处理程序
    CMD ["app.lambda_handler"]
    
  • app.py中导入Python模块的命令如下:

    from maskrcnn.m_rcnn import *
    from maskrcnn.visualize import random_colors, get_mask_contours, draw_mask
    
  • 运行Docker映像时,您遇到了导入错误:

    {"errorMessage": "无法导入模块'app':找不到模块'maskrcnn'",
    "errorType": "Runtime.ImportModuleError", "stackTrace": []}
    
  • m_rcnn.py模块中,访问mask_rcnn_coco.h5文件执行一些操作,如下所示:

    ROOT_DIR = os.path.abspath("/maskrcnn")
    
    # 训练权重文件的本地路径
    COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
    
  • 由于这段代码,您遇到了以下错误:

    {"errorMessage": "[Errno 2] 没有这样的文件或目录:'/maskrcnn/mask_rcnn_coco.h5'",
    "errorType": "FileNotFoundError"},
    
英文:

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"},

答案1

得分: 3

如果你想在本地工作目录中使用 Python 模块 maskrcnn,你需要将它复制到 Docker 镜像中。添加一个 maskrcnn/* 的 COPY 语句可以解决这个问题:

# Dockerfile

# 将之前创建的 app.py 文件复制到容器中
COPY app.py ./
COPY maskrcnn/ ./maskrcnn

并且请移除 ENV PYTHONPATH... 这行。我没有看到需要涉及 PYTHONPATH 的任何迹象。

英文:

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.

答案2

得分: 1

PYTHONPATH 告诉 Python 在哪里查找模块和包。

/maskrcnn 目录本身是一个包,因为存在 __init__.py 文件。

因此,将 ./maskrcnn 添加到 PYTHONPATH 不会产生任何效果!您需要将 maskrcnn父目录添加到 Python 搜索路径中,这通常默认情况下是当前目录 .

此外,您添加了目录 /maskrcnnPYTHONPATH,而不是 ./maskrcnn。根据 Dockerfile,前者目录似乎甚至不存在。

根据这里的回答(https://stackoverflow.com/a/75663954/2954547),所有这些都是无关紧要的,因为您忘记将 maskrcnn 目录复制到镜像中!实际上,我不知道错误的原因是什么;这将需要进一步调试。 但一旦您解决了这个问题,如果简单删除 ENV PYTHONPATH 指令,这个 Dockerfile 应该能正常工作。

如果仍然不起作用,那么可能当前目录 . 并不在包搜索路径中,所以尝试 ENV PYTHONPATH " . " 以确保将其添加进去。

英文:

PYTHONPATH tells Python where to look for modules and packages.

The /maskrcnn directory is itself a package due to the presence of the __init__.py file.

Therefore, adding ./maskrcnn to PYTHONPATH accomplishes nothing! You need the parent directory of maskrcnn added to the Python search path, which is just the current directory ., which is usually added to the Python search path by default.

Furthermore, you added the directory /maskrcnn to PYTHONPATH, and not ./maskrcnn. As far as I can tell from the Dockerfile, the former directory doesn't even exist.

As per the answer here, all this is moot anyway because you forgot to copy the maskrcnn directory in to the image! <strike>I actually don't know the cause of the error as-written; that will require further debugging. But</strike> Once you fix that problem, this Dockerfile should work as-is if you simply remove the ENV PYTHONPATH directive.

If that still doesn't work, then possibly . is not in the package search path, so try ENV PYTHONPATH &quot;.&quot; to ensure that it is added.

答案3

得分: -1

你可以尝试在导入maskrcnn之前添加以下代码行:

import sys
from pathlib import Path

sys.path.append(str(Path(__file__).parents[0]))

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

这样,模块maskrcnn将在与app.py相同的级别进行搜索,因此导入应该能够正常工作。

英文:

You can try to add this lines before importing maskrcnn:


import sys
from pathlib import Path

sys.path.append(str(Path(__file__).parents[0]))

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

This way, the module maskrcnn will be searched at the same level as app.py so the imports should work correctly.

huangapple
  • 本文由 发表于 2023年3月7日 21:41:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定