pythonazureazure-functionsazure-functions-core-toolsazure-http-trigger

Why is my python Azure Function unable to load "file or module System.Security.Cryptography" when debugging locally?


I've been developing a python Azure Function and debugging locally for about a month now. Suddenly, the local debugging was unable to execute "func host start" and returns the below error:

Unhandled exception. System.AggregateException: One or more errors occurred. (Unable to load one or more of the requested types.
Could not load file or assembly 'System.Security.Cryptography, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
Could not load file or assembly 'System.Security.Cryptography, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
Could not load file or assembly 'System.Security.Cryptography, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
Could not load file or assembly 'System.Security.Cryptography, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.)
 ---> System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
Could not load file or assembly 'System.Security.Cryptography, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
Could not load file or assembly 'System.Security.Cryptography, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
Could not load file or assembly 'System.Security.Cryptography, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
Could not load file or assembly 'System.Security.Cryptography, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.RuntimeModule.GetTypes()
   at Azure.Functions.Cli.ConsoleApp..ctor(String[] args, Assembly assembly, IContainer container)
   at Azure.Functions.Cli.ConsoleApp.RunAsync[T](String[] args, IContainer container)
System.IO.FileNotFoundException: Could not load file or assembly 'System.Security.Cryptography, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
File name: 'System.Security.Cryptography, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
System.IO.FileNotFoundException: Could not load file or assembly 'System.Security.Cryptography, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
File name: 'System.Security.Cryptography, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
System.IO.FileNotFoundException: Could not load file or assembly 'System.Security.Cryptography, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
File name: 'System.Security.Cryptography, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
System.IO.FileNotFoundException: Could not load file or assembly 'System.Security.Cryptography, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
File name: 'System.Security.Cryptography, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at Azure.Functions.Cli.ConsoleApp.Run[T](String[] args, IContainer container)
   at Azure.Functions.Cli.Program.Main(String[] args)

My Details:

azure-functions
pymupdf

I'm not sure why this started happening. Reboots don't fix it, and I'd like to avoid wiping my work laptop until I have to. I also tried repairing my VS Code installation and reinstalling core tools. This even happens with the HTTP Trigger example, with no edits made. Any help would be greatly appreciated, thank you!!

I've tried:

Rebooting -> didnt work

Repair VS Code Installation -> didnt work Uninstall and Reinstall Azure Core Tools -> didnt work

removed excess python version (3.12) -> no effect

tried running an unchanged HTTP Trigger Example -> issue still occurs

Putting System.Security.Cryptography and other variations within requirements.txt -> no effect

I've located System.Security.Cryptography .dlls and don't see one identical to the module found - but I never changed anything in the directory and this worked a week or two ago...


Solution

  • Delete %LocalAppData%\azure-functions-core-tools folder and install latest version of Azure functions core tools version with below command:

    npm install -g azure-functions-core-tools@4 --unsafe-perm true
    

    Use below code in Python Azure functions:

    function_app.py:

    import azure.functions as func
    import logging
    import pymupdf
    
    app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
    
    @app.route(route="http_trigger")
    def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
        logging.info('Python HTTP trigger function processed a request.')
        try:
            doc = pymupdf.open(".\\file.pdf") # open a document
            for page in doc:
                text = page.get_text() 
            return func.HttpResponse(f"This HTTP triggered function executed successfully.{text}")
        except:
            return func.HttpResponse(
                 "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
                 status_code=200
            )
    

    requirements.txt:

    azure-functions
    pymupdf
    

    Install the modules in Virtual Environment.

    py -m venv env
    .\env\Scripts\activate
    

    local.settings.json:

    {
      "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "python",
        "AzureWebJobsFeatureFlags": "EnableWorkerIndexing"
      }
    }
    

    Run the function:

    (.venv) C:\Users\uname\pyfunction>func start
    Found Python version 3.11.9 (py).
    
    Azure Functions Core Tools
    Core Tools Version:       4.0.6821 Commit hash: N/A +c09a2033faa7ecf51b3773308283af0ca9a99f83 (64-bit)
    Function Runtime Version: 4.1036.1.23224
    
    [2025-03-19T09:35:03.240Z] Worker process started and initialized.
    
    Functions:
    
            http_trigger:  http://localhost:7071/api/http_trigger
    
    For detailed output, run func with --verbose flag.
    [2025-03-19T09:35:13.419Z] Executing 'Functions.http_trigger' (Reason='This function was programmatically called via the host APIs.', Id=0cc267f8-a4a3-4b59-bc33-5407333ab49d)
    [2025-03-19T09:35:13.508Z] Python HTTP trigger function processed a request.
    [2025-03-19T09:35:13.714Z] Executed 'Functions.http_trigger' (Succeeded, Id=0cc267f8-a4a3-4b59-bc33-5407333ab49d, Duration=324ms)
    

    enter image description here