Azure函数部署后无法显示且导入第三方库时报错的问题排查
我最近遇到了一个棘手的Azure函数部署问题,折腾了好久都没解决,想问问大家我到底忽略了什么:
我开发了一个Azure HTTP触发函数,用VS Code的F1选项选择部署到函数应用后,在Azure门户的函数应用里完全看不到我的函数,本地运行时也无法在函数列表中找到它。
先给大家看看我的函数代码截图:
VS Code中选择部署选项的操作截图:
但部署完成后,函数应用门户里的函数子菜单是空的:
下面是我这个函数的完整代码:
import azure.functions as func import logging import os from azure.identity import DefaultAzureCredential from azure.keyvault.secrets import SecretClient from azure.communication.email import EmailClient from datetime import timezone, timedelta, datetime import jwt import bcrypt import pymssql import json from azure.identity import DefaultAzureCredential from azure.keyvault.secrets import SecretClient app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS) @app.route(route="actualizar_contrasena", auth_level=func.AuthLevel.ANONYMOUS) def actualizar_contrasena(req: func.HttpRequest) -> func.HttpResponse: import json try: req_body = req.get_json() username_to_check = req_body.get("username") password_to_check = str(req_body.get("password")) # do things return func.HttpResponse( json.dumps( {"access_token": 1, "refresh_token": 1} ), status_code=200, ) except Exception as e: return func.HttpResponse(str(e), status_code=500)
UPDATE
按照@RithwikBoj的建议操作后,问题还是没解决。更奇怪的是,我发现本地运行时也看不到函数列表:
下面是我的项目配置和结构信息,大家帮我看看有没有问题:
- host.json配置:
{ "version": "2.0", "logging": { "applicationInsights": { "samplingSettings": { "isEnabled": true, "excludedTypes": "Request" } } }, "extensionBundle": { "id": "Microsoft.Azure.Functions.ExtensionBundle", "version": "[4.*, 5.0.0)" } }
- 项目文件夹结构:
root |_.venv |_.funcignore |_host.json |_function_app.py |_ local.settings.json

- requirements.txt依赖列表:
azure-common==1.1.28 azure-communication-email==1.0.0 azure-core==1.32.0 azure-functions==1.21.3 azure-identity==1.19.0 azure-keyvault-secrets==4.9.0 azure-mgmt-core==1.5.0 bcrypt==4.2.1 certifi==2024.12.14 cffi==1.17.1 charset-normalizer==3.4.1 cryptography==44.0.0 idna==3.10 isodate==0.7.2 jwt==1.3.1 msal==1.31.1 msal-extensions==1.2.0 msrest==0.7.1 oauthlib==3.2.2 portalocker==2.10.1 pycparser==2.22 PyJWT==2.10.1 pymssql==2.3.2 requests==2.32.3 requests-oauthlib==2.0.0 six==1.17.0 typing_extensions==4.12.2 urllib3==2.3.0
后来我尝试通过GitHub Actions部署,用的YAML配置如下:
# 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: Build and deploy Azure Function App - fnc-app-d on: push: branches: - develop workflow_dispatch: env: AZURE_FUNCTIONAPP_PACKAGE_PATH: '.' # 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) 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: Create and start virtual environment run: | python -m venv venv source venv/bin/activate - name: Install dependencies run: pip install -r requirements.txt # Optional: Add step to run tests here - name: Zip artifact for deployment run: zip -r release.zip function_app.py host.json -x "*.txt venv/*" ".git/*" ".github/* *.md .gitignore local.*" - name: Upload artifact for deployment job uses: actions/upload-artifact@v4 with: name: python-app path: | . !venv/ deploy: runs-on: ubuntu-latest needs: build permissions: id-token: write #This is required for requesting the JWT contents: read #This is required for actions/checkout steps: - name: Download artifact from build job uses: actions/download-artifact@v4 with: name: python-app path: . - name: Unzip artifact for deployment run: unzip -o release.zip - name: Login to Azure uses: azure/login@v2 with: client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_06 }} tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_88510E }} subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_38D5 }} - name: 'Deploy to Azure Functions' uses: Azure/functions-action@v1 id: deploy-to-function with: app-name: 'fnc-app-d' package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}
部署后我发现函数应用里的requirements.txt似乎有异常:
而且门户里的文件夹结构也没同步更新(我早就删掉了Readme.md,但门户里还显示着):
UPDATE
我修改了YAML配置,现在requirements.txt已经能正确上传到函数应用了。
UPDATE 2
我做了个测试:把除了os、logging和azure.functions之外的所有导入语句都删掉,这时函数能正常部署,而且在门户里也能显示出来。但只要我添加任意第三方库的导入(比如import jwt),函数就又从门户和本地列表中消失了。这才是我现在最头疼的问题——我必须要在函数里使用第三方库,但导入后函数就完全无法正常识别和部署。
UPDATE 3
我尝试了一个变通方法:把第三方库的导入语句移到函数定义的内部,比如这样:
@app.route(route="log_in_user", auth_level=func.AuthLevel.ANONYMOUS) def log_in_user(req: func.HttpRequest) -> func.HttpResponse: import json import jwt # 把库的导入移到函数内部 try: req_body = req.get_json() username_to_check = req_body.get("username") password_to_check = str(req_body.get("password")) p = jwt.encode({"pass": password_to_check}, "assaasassa", algorithm="HS256") return func.HttpResponse( json.dumps( {"access_token": 1, "refresh_token": 1, "p": p} ), status_code=200, ) except Exception as e: return func.HttpResponse(str(e), status_code=500)
这次函数终于能成功部署并在门户显示了,但运行函数时又报错:
Result: Failure Exception: ModuleNotFoundError: No module named 'jwt'
我真的搞不懂问题出在哪了,到底是部署流程的问题,还是依赖安装的问题?有没有人遇到过类似的情况?
备注:内容来源于stack exchange,提问作者OK 400

