Linux消费计划下Azure Function App部署成功但无法导入numpy包
解决Azure DevOps部署Azure Function App时的Numpy导入错误
VS Code本地部署Azure Function App可正常运行,但通过Azure DevOps构建部署后,虽显示部署成功,却触发Numpy导入错误。
错误信息
Result: Failure Exception: ImportError: Unable to import required dependencies: numpy: IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE! Importing the numpy C-extensions failed. This error can happen for many reasons, often due to issues with your setup or how NumPy was installed. We have compiled some common reasons and troubleshooting tips at: https://numpy.org/devdocs/user/troubleshooting-importerror.html Please note and check the following: * The Python version is: Python3.10 from "/usr/local/bin/python" * The NumPy version is: "1.25.0" and make sure that they are the versions you expect. Please carefully study the documentation linked above for further help. Original error was: No module named 'numpy.core._multiarray_umath' . Please check the requirements.txt file for the missing module. For more info, please refer the troubleshooting guide: https://aka.ms/functions-modulenotfound
用户当前Azure Pipelines配置
构建阶段配置
variables: # Azure Resource Manager connection created during pipeline creation azureSubscription: 'azureSubscription' # Agent VM image name vmImageName: 'ubuntu-20.04' # Working Directory workingDirectory: '$(System.DefaultWorkingDirectory)/' stages: - stage: Build displayName: Build stage jobs: - job: Build displayName: Build pool: vmImage: $(vmImageName) steps: - bash: | if [ -f extensions.csproj ] then dotnet build extensions.csproj --runtime ubuntu.16.04-x64 --output ./bin fi workingDirectory: $(workingDirectory) displayName: 'Build extensions' - task: UsePythonVersion@0 inputs: versionSpec: '3.x' # string. Required. Version spec. Default: 3.x. architecture: 'x64' # 'x86' | 'x64'. Required. Architecture. Default: x64. - bash: | pip install --target="./.python_packages/lib/site-packages" -r ./requirements.txt workingDirectory: $(workingDirectory) displayName: 'Install application dependencies' - task: ArchiveFiles@2 displayName: 'Archive files' inputs: rootFolderOrFile: '$(workingDirectory)' includeRootFolder: false archiveType: zip archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip replaceExistingArchive: true - publish: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip artifact: drop - task: AzureFunctionApp@2 inputs: azureSubscription: 'azureSubscription' appType: 'functionAppLinux' appName: 'app-name' package: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip' deploymentMethod: 'auto'
单独部署步骤配置
steps: - task: AzureFunctionApp@2 displayName: 'Azure Function App Deploy' inputs: azureSubscription: 'azureSubscription' appType: functionAppLinux appName: 'app-name' package: '$(System.DefaultWorkingDirectory)/_Function App/drop/*zip' deploymentMethod: zipDeploy
解决方案
- 锁定Python版本一致性:将
UsePythonVersion@0任务的versionSpec改为3.10,确保本地开发和DevOps构建代理使用完全相同的Python版本,避免依赖包编译环境不匹配。 - 优化依赖安装命令:安装依赖时添加平台参数,强制获取适配Linux环境的预编译Numpy包,避免源码编译失败:
pip install --platform manylinux2014_x86_64 --target="./.python_packages/lib/site-packages" --only-binary=:all: --no-cache-dir -r ./requirements.txt - 清理重复部署步骤:移除Build阶段中的
AzureFunctionApp@2任务,仅保留单独的Deploy阶段执行部署,避免重复部署导致目录结构混乱。 - 验证Numpy版本兼容性:将requirements.txt中的Numpy版本降级至与Python3.10兼容的稳定版本(如1.23.5),避免高版本Numpy的C扩展兼容性问题。
- 检查归档目录完整性:确认
ArchiveFiles@2任务的rootFolderOrFile包含.python_packages目录,确保依赖包被正确打包进部署压缩包。
内容的提问来源于stack exchange,提问作者David Hunt
相关产品推荐
相关产品推荐

