Azure Functions 3.x部署后出现ImportError:无法导入FunctionRegister
我有一个基于Python 3.7开发、运行一年的Azure Durable Functions 3.x应用,近期做了小改动后通过func azure functionapp publish重新部署。本地VS Code调试正常,但云端运行时抛出以下错误:
Result: Failure Exception: ImportError: cannot import name 'FunctionRegister' from 'azure.functions' (/azure-functions-host/workers/python/3.7/LINUX/X64/azure/functions/init.py).
Stack: File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py", line 318, in _handle__function_load_request func_request.metadata.entry_point)
File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 42, in call raise extend_exception_message(e, message)
File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 40, in call return func(*args, **kwargs)
File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/loader.py", line 85, in load_function mod = importlib.import_module(fullmodname)
File "/usr/local/lib/python3.7/importlib/init.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1006, in _gcd_import
File "", line 983, in _find_and_load
File "", line 967, in _find_and_load_unlocked
File "", line 677, in _load_unlocked
File "", line 728, in exec_module
File "", line 219, in _call_with_frames_removed
File "/home/site/wwwroot/ClientStarter/init.py", line 5, in import azure.durable_functions as df
File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/durable_functions/init.py", line 14, in from .decorators import DFApp
File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/durable_functions/decorators/init.py", line 4, in from .durable_app import DFApp
File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/durable_functions/decorators/durable_app.py", line 10, in from azure.functions import FunctionRegister, TriggerApi, BindingApi, AuthLevel
已知是Python版本与应用版本的模块兼容问题,但无法升级到Functions 4.x,请问能否继续使用3.x并解决该错误?
锁定azure.durable-functions的兼容版本
FunctionRegister是Azure Functions Python Worker 4.x引入的API,而Durable Functions的新版本(如v1.13+)默认依赖这个API。你需要降级到适配Functions 3.x和Python 3.7的版本,推荐在requirements.txt中添加azure-durable-functions==1.12.0,这是最后一批完全兼容Functions 3.x的稳定版。固定azure-functions SDK版本
在requirements.txt中添加azure-functions==1.14.0,这是Functions 3.x支持的最高Python SDK版本,避免部署时自动升级到不兼容的新版本。重新打包部署
- 删除本地的
__pycache__和.python_packages目录 - 运行
pip install -r requirements.txt --target .python_packages/lib/site-packages,确保本地依赖与云端环境一致 - 使用
func azure functionapp publish <你的应用名称> --build remote重新部署,强制云端使用指定的依赖版本
- 删除本地的
验证云端运行环境配置
在Azure门户的函数应用配置中确认:- 运行时版本设置为
~3 - Python版本设置为
3.7 - 禁用自动依赖更新类的选项(若存在)
- 运行时版本设置为
内容的提问来源于stack exchange,提问作者Salvatore Nedia

