Azure web app AttributeError: module 'typing' has no attribute '_ClassVar'

Santiago 0 Reputation points
2024-05-21T23:40:57.3533333+00:00

Hi, Im doing some tests with the Azure services. In this case Im trying to deploy a ML model, trained with the Azure ML service AutoML in a web app for inference. I have the following files in the project directory:

main.py

requirements.txt

model.pkl

startup.sh

The main.py script is really simple:

import pickle
import logging
import os
from fastapi import FastAPI, Request



app = FastAPI()
logging.basicConfig(level=logging.INFO,
                    format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
                    datefmt="%Y-%m-%d %H:%M:%S")
LOGGER = logging.getLogger(__name__)

def load_model():
    path = os.path.join(os.path.dirname(__file__), "model.pkl")    
    with open(path, 'rb') as f:
        model = pickle.load(f)
    return model

model = load_model()
TARGET_NAMES = ['No Failure', 'Failure']

@app.post("/predict_class")
async def predict_class(request: Request):
    req = await request.json()
    data = req["data"]
    y_pred = model.predict(data)[0]
    pred_class = TARGET_NAMES[y_pred]

    LOGGER.info(f"Request: {data} y_pred: {pred_class}")
    response = {"class": pred_class}
    return response

Im getting the following traceback error:

2024-05-21T23:31:35.516743913Z File "/tmp/8dc79afc5b4d09d/antenv/lib/python3.9/site-packages/dataclasses.py", line 800, in _process_class
2024-05-21T23:31:35.516951440Z cls_fields = [_get_field(cls, name, type)
2024-05-21T23:31:35.516961970Z File "/tmp/8dc79afc5b4d09d/antenv/lib/python3.9/site-packages/dataclasses.py", line 800, in <listcomp>
2024-05-21T23:31:35.517165168Z cls_fields = [_get_field(cls, name, type)
2024-05-21T23:31:35.517175407Z File "/tmp/8dc79afc5b4d09d/antenv/lib/python3.9/site-packages/dataclasses.py", line 659, in _get_field
2024-05-21T23:31:35.517361835Z if (_is_classvar(a_type, typing)
2024-05-21T23:31:35.517372214Z File "/tmp/8dc79afc5b4d09d/antenv/lib/python3.9/site-packages/dataclasses.py", line 550, in _is_classvar
2024-05-21T23:31:35.517546770Z return type(a_type) is typing._ClassVar
2024-05-21T23:31:35.517556809Z AttributeError: module 'typing' has no attribute '_ClassVar'/home/LogFiles/2024_05_21_10-30-0-213_docker.log (https://test-poc-azure.scm.azurewebsites.net/api/vfs/LogFiles/2024_05_21_10-30-0-213_docker.log)

In the requirements.txt I have the following packages:

fastapi
scikit-learn
uvicorn
azureml
azureml-train
azureml-core
azureml-sdk
azureml-train-core
azureml-train-automl-runtime

I tried using python 3.8, 3.9, 3.10, 3.11 and 3.12. All the python versions available in web apps.

I have tried every workaround found on google also, none of those worked.

Any idea how can this be solved?

Azure Machine Learning
Azure Machine Learning
An Azure machine learning service for building and deploying models.
2,630 questions
Azure Static Web Apps
Azure Static Web Apps
An Azure service that provides streamlined full-stack web app development.
792 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Vahid Ghafarpour 19,395 Reputation points
    2024-05-22T03:09:50.1466667+00:00

    Thanks for posting your question in the Microsoft Q&A forum.

    It seems like Azure has a problematic import path, thus wrong typing package is imported. Python 3.8 has the typing package built-in, but it's trying to use the obsolete one installed with pip, which is causing the problem.

    Add this at the very beginning of your file, before any imports:

    import
    

    It essentially prioritizes built-in packages instead of ones installed with pip.

    https://stackoverflow.com/questions/75473492/attributeerror-module-typing-has-no-attribute-classvar-azure-function

    ** Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful **