部署至Azure函数的FastAPI导入DefaultAzureCredential时触发ImportError问题求助
大家好,我碰到了个头疼的问题:我把一个FastAPI部署到Azure函数上,本地运行一切正常,但部署后只要导入DefaultAzureCredential就会报错,把那行导入代码删掉就能正常运行。
问题触发点
只要执行这行导入代码:
# 去掉这行程序就能正常运行 from azure.identity import DefaultAzureCredential
立刻会抛出以下错误:
2025-02-05T18:13:34.960344159Z return util.import_app(self.app_uri) 2025-02-05T18:13:34.960347459Z
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2025-02-05T18:13:34.960350559Z File "/opt/python/3.12.2/lib/python3.12/site-packages/gunicorn/util.py", line 371, in import_app 2025-02-05T18:13:34.960353959Z mod = importlib.import_module(module) 2025-02-05T18:13:34.960357159Z
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2025-02-05T18:13:34.960360459Z File "/opt/python/3.12.2/lib/python3.12/importlib/init.py", line 90, in import_module 2025-02-05T18:13:34.960363959Z return _bootstrap._gcd_import(name[level:], package, level) 2025-02-05T18:13:34.960367359Z
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2025-02-05T18:13:34.960370759Z File "", line 1387, in _gcd_import 2025-02-05T18:13:34.960374859Z File "", line 1360, in _find_and_load 2025-02-05T18:13:34.960378160Z File "", line 1331, in _find_and_load_unlocked 2025-02-05T18:13:34.960381660Z
File "", line 935, in _load_unlocked 2025-02-05T18:13:34.960384960Z File "", line 995, in exec_module 2025-02-05T18:13:34.960388360Z File "", line 488, in _call_with_frames_removed 2025-02-05T18:13:34.960392960Z File "/tmp/8dd460fb826ed55/main.py", line 12, in 2025-02-05T18:13:34.960400760Z from azure.identity import DefaultAzureCredential 2025-02-05T18:13:34.960404260Z File "/tmp/8dd460fb826ed55/antenv/lib/python3.12/site-packages/azure/identity/init.py", line 10, in 2025-02-05T18:13:34.960407860Z from ._credentials import ( 2025-02-05T18:13:34.960411060Z File "/tmp/8dd460fb826ed55/antenv/lib/python3.12/site-packages/azure/identity/_credentials/init.py", line 5, in 2025-02-05T18:13:34.960414560Z from .authorization_code import AuthorizationCodeCredential 2025-02-05T18:13:34.960417860Z File "/tmp/8dd460fb826ed55/antenv/lib/python3.12/site-packages/azure/identity/_credentials/authorization_code.py", line 7, in 2025-02-05T18:13:34.960421260Z from azure.core.credentials import AccessToken, AccessTokenInfo, TokenRequestOptions 2025-02-05T18:13:34.960424860Z ImportError: cannot import name 'AccessTokenInfo' from 'azure.core.credentials' (/agents/python/azure/core/credentials.py)
我的相关代码
main.py
import os from typing import Union from fastapi import FastAPI, Security import uvicorn from utils.azure.fast_api_auth import FastAPIAuth,FastAPIAuthOptions from routers.router1 import router1_router from enums import fast_api_metadata from settings import settings import logging from azure.identity import DefaultAzureCredential print("TEST1") fast_api_options = FastAPIAuthOptions( title="API", tenant_id=settings.xx, openapi_tags=xx, client_id=settings.fastapi_settings.xx, ) fast_api_auth = FastAPIAuth(fast_api_options) app = fast_api_auth.get_fast_api_app() app.include_router( router1_router, prefix="/api/router1", dependencies=[Security(fast_api_auth.azure_scheme)], tags=["vessels"] ) @app.get("/log", dependencies=[Security(fast_api_auth.azure_scheme)]) async def root(): return {"whoIsTheBest": "!!"} @app.get("/", dependencies=[Security(fast_api_auth.azure_scheme)]) def read_root(): return {"Hello": "World"} @app.get("/settings") def read_settings(): return settings @app.get("/items/{item_id}") def read_item(item_id: int, q: Union[str, None] = None): return {"item_id": item_id, "q": q} if __name__ == '__main__': uvicorn.run( 'main:app', host="0.0.0.0", port=8000, reload=True, log_level="info" )
requirements.txt
annotated-types==0.7.0 anyio==4.7.0 certifi==2024.8.30 cffi==1.17.1 click==8.1.7 colorama==0.4.6 cryptography==44.0.0 fastapi==0.115.6 fastapi-azure-auth==5.0.1 h11==0.14.0 httpcore==1.0.7 httpx==0.28.1 idna==3.10 pycparser==2.22 pydantic==2.10.3 pydantic-settings==2.6.1 pydantic_core==2.27.1 PyJWT==2.10.1 python-dotenv==1.0.1 sniffio==1.3.1 starlette==0.41.3 typing_extensions==4.12.2 uvicorn==0.32.1 azure-monitor-opentelemetry databricks databricks.sdk databricks-sql-connector pandas sqlalchemy numpy azure-identity opencensus-ext-azure opentelemetry-api opentelemetry-sdk opentelemetry-instrumentation-fastapi
有没有大佬能帮忙分析下这个问题?本地跑完全没问题,一部署到Azure函数就出状况,实在摸不着头脑😭
备注:内容来源于stack exchange,提问作者Thomas Segato

