Azure Function App部署后无法访问目录文件问题求助
问题
创建Azure Function时,项目包含data、model、utils等目录,存放推理脚本inference.py依赖的模块文件。通过Docker镜像部署到Azure Portal后,这些目录无法被识别或访问,运行代码时出现No such file or directory错误。相关代码片段、Dockerfile、requirements.txt如下:
推理脚本片段(inference.py)
""" Run a summarization model interactively. """ import argparse import numpy as np import nltk # from colors import yellow, red, blue import torch from torch.autograd import Variable import json,re from data import loader from data.loader import DataLoader from utils import helper, constant, torch_utils, text_utils, bleu, rouge from utils.torch_utils import set_cuda from utils.vocab import Vocab from model.trainer import Trainer import joblib def load_model(): model_file = "model.pt" return model_file # 其余代码省略...
Dockerfile
FROM mcr.microsoft.com/azure-functions/python:4.0 LABEL NAME=myfun2 Version=0.0.1 EXPOSE 80 ENV AzureWebJobsScriptRoot=/home/site/wwwroot \ AzureFunctionsJobHost__Logging__Console__IsEnabled=true COPY requirements.txt / RUN cp /etc/apt/sources.list /etc/apt/sources.list~ RUN sed -i '$a deb-src http://deb.debian.org/debian buster main' /etc/apt/sources.list RUN sed -i '$a deb-src http://security.debian.org/debian-security buster/updates main' /etc/apt/sources.list RUN sed -i '$a deb-src http://deb.debian.org/debian buster-updates main' /etc/apt/sources.list # RUN apt-get -y update RUN apt-get -y upgrade RUN apt-get install -y git # RUN apt-get -y install software-properties-common RUN apt-get -y install g++ RUN apt-get -y install unixodbc-dev RUN pip install --upgrade pip RUN pip install git+https://github.com/tagucci/pythonrouge.git RUN pip install -r /requirements.txt #RUN pip env --version RUN python -m nltk.downloader popular COPY . /home/site/wwwroot
requirements.txt
azure-functions pymsteams configparser requests asyncio pandas joblib numpy nltk torch==1.10.1+cu111 torchvision==0.11.2+cu111 torchaudio==0.10.1 -f https://download.pytorch.org/whl/cu111/torch_stable.html
解决方案
添加Python路径配置
Azure Function的执行工作目录可能不是脚本所在路径,导致无法找到同级的data、model、utils目录。在inference.py的开头添加以下代码,将脚本所在目录加入Python搜索路径:import sys import os sys.path.append(os.path.dirname(os.path.abspath(__file__)))修正模型文件路径
当前load_model函数中使用相对路径"model.pt",需确保路径与镜像中的实际位置一致。建议使用绝对路径避免路径问题:def load_model(): # 假设model.pt与inference.py在同一目录下 model_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "model.pt") # 如果model.pt在model目录下,改为: # model_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "model", "model.pt") return model_file验证镜像内目录结构
在Dockerfile的COPY . /home/site/wwwroot命令后添加目录验证命令,确保目录被正确复制:COPY . /home/site/wwwroot # 验证目录是否存在 RUN ls -la /home/site/wwwroot本地预测试镜像
部署到Azure前,先本地运行镜像并进入容器检查目录结构,测试模块导入是否正常:# 构建镜像 docker build -t az-function-image . # 运行容器并进入bash docker run -it --rm az-function-image bash # 进入工作目录检查 cd /home/site/wwwroot ls -la # 测试脚本运行 python inference.py确认Azure Function的脚本根目录
确保AzureWebJobsScriptRoot环境变量设置正确,当前Dockerfile中已设置为/home/site/wwwroot,需保证所有代码文件和目录都在该路径下。
内容的提问来源于stack exchange,提问作者Shyam
相关产品推荐
相关产品推荐

