部署Azure Function时出现'module not found azure-data'错误,本地运行正常
问题:Python Azure Function部署后启动失败,报ModuleNotFoundError: No module named 'azure.data'
我开发了一个Python版Azure Function,本地运行完全正常,但通过GitHub Actions部署后,虽然部署成功,但函数无法启动。构建过程无异常,解压部署包后也能正常运行function_app.py,但Azure Function日志显示启动时映射0条路由,报错信息为:
ModuleNotFoundError: No module named 'azure.data'
对应代码行是from azure.data.tables import TableClient, TableServiceClient,已确认requirements.txt中包含该依赖。
附上GitHub Actions工作流文件:
# Docs for the Azure Web Apps Deploy action: https://github.com/azure/functions-action # More GitHub Actions for Azure: https://github.com/Azure/actions # More info on Python, GitHub Actions, and Azure Functions: https://aka.ms/python-webapps-actions name: Deploy on: push: branches: - test workflow_dispatch: env: AZURE_FUNCTIONAPP_PACKAGE_PATH: './api' # set this to the path to your web app project, defaults to the repository root PYTHON_VERSION: '3.11' # set this to the python version to use (supports 3.6, 3.7, 3.8) AZURE_STORAGE_CONNECTION_STRING: ${{ secrets.AZURE_STORAGE_CONNECTION_STRING }} AZURE_COSMOS_CONNECTION_STRING: ${{ secrets.AZURE_COSMOS_CONNECTION_STRING }} jobs: build: runs-on: ubuntu-latest permissions: contents: read #This is required for actions/checkout steps: - name: Checkout repository uses: actions/checkout@v4 - name: Setup Python version uses: actions/setup-python@v5 with: python-version: ${{ env.PYTHON_VERSION }} - name: Copy /shared to /api run: cp -r ./shared ./api/ - name: Create and start virtual environment run: | python3 -m venv venv source venv/bin/activate - name: Install dependencies (api) run: pip3 install -r ./api/requirements.txt # Optional: Add step to run tests here - name: Test function app run: python3 ./api/function_app.py # end of tests - name: Zip artifact for deployment run: zip release.zip ./api/* -r - name: Upload artifact for deployment job uses: actions/upload-artifact@v4 with: name: python-app path: | release.zip deploy: runs-on: ubuntu-latest needs: build steps: - name: Download artifact from build job uses: actions/download-artifact@v4 with: name: python-app - name: Unzip artifact for deployment run: unzip release.zip - name: 'Deploy to Azure Functions' uses: Azure/functions-action@v1 id: deploy-to-function with: app-name: 'my_app' slot-name: 'Production' package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }} publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_XXXXXXXX }} sku: 'flexconsumption'
排查思路
- 检查依赖安装路径:当前工作流将依赖安装到虚拟环境
venv中,但打包的是./api目录,未包含虚拟环境内的依赖。Azure Functions运行时需要依赖包与代码在同一目录(或指定的site-packages路径),需调整依赖安装路径。 - 修改依赖安装命令:将安装命令改为
pip install -r ./api/requirements.txt --target ./api/.python_packages/lib/site-packages,让依赖直接安装到Azure Functions识别的目录下,部署后即可被加载。 - 修正打包逻辑:当前
zip release.zip ./api/* -r会忽略隐藏目录(如.python_packages),需改为zip release.zip ./api -r,确保包含api目录下所有内容,包括隐藏的依赖目录。 - 验证部署包结构:下载Azure上的部署包,解压后查看是否存在
.python_packages/lib/site-packages/azure/data目录,若不存在则说明依赖未被正确打包。 - 确认Python版本一致性:检查Azure Functions运行时的Python版本是否与GitHub Actions、本地使用的3.11版本一致,版本不匹配可能导致依赖兼容性问题。
- 使用官方打包工具:改用Azure Functions Core Tools的
func pack命令生成部署包,该工具会自动处理依赖和目录结构,避免手动打包的疏漏。 - 查看完整启动日志:在Azure门户中查看函数应用的完整启动日志,排查是否有依赖安装失败、权限不足等其他潜在问题。
内容的提问来源于stack exchange,提问作者romtecmax
相关产品推荐
相关产品推荐

