Python Function App导入ultralytics.yolo遇ModuleNotFoundError问题咨询
Azure Python Function App中
ultralytics.yolo模块找不到的原因分析 问题背景
我正在创建Python Function App,从Ultralytics Yolov5中挑选代码复用,当前简化后的文件夹结构如下:
Function |_ ApiCall | |_ __init__.py | function.json |_Lib/site-packages | |_ ultralytics | |_ yolo | | |_ utils | | | __init__.py | | | checks.py | | |_ __init__.py | |_ __init__.py |_ utils | __init__.py | general.py |_ detect.py
detect.py中的导入语句:
from utils.general import (Profile, check_file, check_img_size, non_max_suppression, scale_boxes)
general.py中的导入语句:
from ultralytics.yolo.utils.checks import check_requirements
运行时出现错误:
Result: Failure Exception: ModuleNotFoundError: No module named 'ultralytics.yolo'. Please check the requirements.txt file for the missing module. For more info, please refer the troubleshooting guide: https://aka.ms/functions-modulenotfound
已在虚拟环境中安装requirements.txt中的依赖:
azure-functions gitpython>=3.1.30 matplotlib>=3.3 numpy>=1.26.0 shapely>=2.0.1 torch>=2.0.1 torchvision>=0.15.1 ultralytics>=8.0.107 pandas>=1.1.4 setuptools>=65.5.1
且每个文件夹都已添加__init__.py标记为包,请问该模块找不到错误产生的原因是什么?
错误原因
Ultralytics版本结构变更
你安装的是ultralytics>=8.0.107,v8版本对项目结构做了重构,原v5时期的ultralytics.yolo路径已被调整,yolo不再作为顶层包的直接子模块对外暴露,直接按旧路径导入会找不到模块。自定义包目录未被运行时识别
你在项目内手动创建了Lib/site-packages并放入ultralytics,但Azure Functions的Python运行时只会加载系统或自动安装的site-packages路径,不会将你项目内的这个自定义目录加入Python的sys.path,即使本地能运行,部署后也无法识别该目录下的模块。路径优先级冲突(次要)
项目根目录下的自定义utils包,可能会干扰Python对ultralytics内部模块的查找,但核心问题还是版本结构和运行时路径识别的问题。
对应解决建议
- 适配v8版本调整导入语句:将
from ultralytics.yolo.utils.checks import check_requirements改为from ultralytics.utils.checks import check_requirements,v8中相关工具类已移至ultralytics.utils路径下。 - 删除项目内的
Lib/site-packages目录,依赖完全通过requirements.txt让Azure Functions自动安装,不要手动复制包文件到项目中。 - 确保本地开发环境和Azure运行时的依赖版本一致,避免版本差异导致的结构不兼容。
内容的提问来源于stack exchange,提问作者GameFire
相关产品推荐
相关产品推荐

