本地调试Azure Functions时出现pandas模块缺失的ModuleNotFoundError
本地调试Azure Functions时出现ModuleNotFoundError(找不到pandas模块)的问题
问题说明
编写了导入pandas库的Python Azure Functions代码,部署到Azure Function App后可正常运行,但本地调试时触发ModuleNotFoundError,提示找不到pandas模块。已确认pandas已安装,移除pandas导入语句后可正常本地调试并通过Postman触发请求。
函数代码
import logging import azure.functions as func import pandas as pd def main(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') name = req.params.get('name') if not name: try: req_body = req.get_json() except ValueError: pass else: name = req_body.get('name') if name: return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.") else: return func.HttpResponse( "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.", status_code=200 )
依赖文件(requirements.txt)
pandas==2.0.1 numpy azure-functions azure-storage-blob openai[embeddings] requests
本地调试错误日志
[2023-04-27T08:21:37.499Z] Executing 'Functions.func-test' (Reason='This function was programmatically called via the host APIs.', Id=44347c87-36a7-4415-bbe8-63a922af57bc) [2023-04-27T08:21:37.590Z] Executed 'Functions.func-test' (Failed, Id=44347c87-36a7-4415-bbe8-63a922af57bc, Duration=125ms) [2023-04-27T08:21:37.591Z] System.Private.CoreLib: Exception while executing function: Functions.func-test. System.Private.CoreLib: Result: Failure Exception: ModuleNotFoundError: No module named 'pandas'. Please check the requirements.txt file for the missing module. For more info, please refer the troubleshooting guide: https://aka.ms/functions-modulenotfound Stack: File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 357, in _handle__function_load_request func = loader.load_function( File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10/WINDOWS/X64\azure_functions_worker\utils\wrappers.py", line 48, in call raise extend_exception_message(e, message) File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10/WINDOWS/X64\azure_functions_worker\utils\wrappers.py", line 44, in call return func(*args, **kwargs) File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10/WINDOWS/X64\azure_functions_worker\loader.py", line 132, in load_function mod = importlib.import_module(fullmodname) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\importlib\__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "C:\Users\anandu.balachandran\AzureFunctionTest\func-test\__init__.py", line 3, in <module> import pandas as pd
排查与解决步骤
确认虚拟环境激活状态
本地调试必须使用安装了依赖的虚拟环境:- Windows系统执行:
venv\Scripts\activate - macOS/Linux系统执行:
source venv/bin/activate
激活后重新安装依赖:pip install -r requirements.txt
- Windows系统执行:
检查Core Tools使用的Python解释器
从错误日志可知Core Tools使用的是系统路径下的Python解释器,需确认该解释器是否安装了pandas:- 运行该路径下的Python,执行
import pandas测试 - 若未安装,用对应路径的pip安装:
"C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\Scripts\pip.exe" install pandas==2.0.1
- 运行该路径下的Python,执行
添加.funcignore文件
在项目根目录创建.funcignore文件,添加以下内容,避免Core Tools读取错误的依赖目录:__pycache__/ .venv/ env/重启Core Tools
关闭当前运行的func start进程,激活正确虚拟环境后重新启动调试。
内容的提问来源于stack exchange,提问作者Anandu KB
相关产品推荐
相关产品推荐

