[Python] ImportError: cannot > import name 'AccessTokenInfo' from 'azure.core.credentials'

Discussão em 'Python' iniciado por Stack, Fevereiro 6, 2025.

  1. Stack

    Stack Membro Participativo

    I have a FAST API deployed to an azure function. Locally everything works as expected. However when deployed I get following error. The error allready occurs in when doing the import:

    from azure.identity import DefaultAzureCredential #if i remove this line it works


    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="DNA PORT API",
    tenant_id=settings.TENANT_ID,
    openapi_tags=fast_api_metadata,
    client_id=settings.fastapi_settings.APP_CLIENT_ID,

    )

    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

    Continue reading...

Compartilhe esta Página