skip to Main Content

I am making a foray into writing a webhook listener in Visual Studio Code using Python. I will admit I am a tyro, so do bear with me. The initial example in VSC for a webhook listener is straightforward. I run it locally in my terminal window using func host start, I then test it using Postman in VSC and get the expected response and a 200 status.

But I want to do more than simply give a response/200, I want to grab the incoming JSON data, store it in Azure Data Lake, and then give a response. To do this, I have to add some libraries. The imports in my function_app.py file have gone from

import logging

import azure.functions as func

to this

import logging
import datetime
import os
import json

import azure.functions as func
from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient
from azure.storage.filedatalake import DataLakeServiceClient

I updated my requirements.txt file so that it went from

# DO NOT include azure-functions-worker in this file
# The Python Worker is managed by Azure Functions platform
# Manually managing azure-functions-worker may cause unexpected issues

azure-functions

to this

# DO NOT include azure-functions-worker in this file
# The Python Worker is managed by Azure Functions platform
# Manually managing azure-functions-worker may cause unexpected issues

azure-functions
azure-storage-file-datalake
azure-identity
azure-keyvault
requests

My local.settings.json now includes entries for KEY_VAULT_URL, STORAGE_ACCOUNT_NAME, etc.

Thinking all is set, I kick off the code in the terminal window using func host start and now get an error that boils down to ModuleNotFoundError: No module named 'azure.identity'.

Somewhere it seems I’ve not told VSC where azure.identity exists. It is in .venvLibsite-packagesazure_identity-1.16.0.dist-info.

I know I’m probably missing something very obvious to others, but I would be grateful for any suggestions.

I have tried researching, using ChatGPT, GitHub Copilot, etc. but nothing seems to make a difference.

I have performed pip installs of individual packages, I’ve done a pip install -r requirements.txt to ensure everything was available. I’m honestly at a loss.

2

Answers


  1. Chosen as BEST ANSWER

    Mea culpa.

    It turns out I had an old, lingering installation of Python 3.7 on my laptop from some time back. Once I uninstalled it, everything loaded as it should.

    Thanks everyone who took a gander at my question.


  2. When you are using virtual environment. you need to install requirements.txt file inside virtual environment.

    To activate virtual environment follow these commands

    .venvScriptsactivate  #for Windows ()
    .venvbinactivate      #for Linux
    
    #then run your command to install packages
    pip install -r requirements.txt
    
    #then run your func host start command
    func host start
    

    you will see virtual environment tag(.venv) in front as shown below.

    This worked for me.

    import azure.functions as func
    import logging
    import os
    from azure.identity import DefaultAzureCredential
    from azure.storage.filedatalake import DataLakeServiceClient
    
    
    app = func.FunctionApp()
    
    def get_service_client():
        account_name = os.getenv("STORAGE_ACCOUNT_NAME")
        credential = DefaultAzureCredential()
        service_client = DataLakeServiceClient(
            account_url=f"https://{account_name}.blob.core.windows.net",
            credential=credential
        )
        return service_client
    
    def read_file_from_datalake(service_client, filesystem_name, file_path):
        file_system_client = service_client.get_file_system_client(file_system=filesystem_name)
        file_client = file_system_client.get_file_client(file_path)
        download = file_client.download_file()
        downloaded_bytes = download.readall()
        return downloaded_bytes.decode('utf-8')
    
    @app.route(route="http_trigger", auth_level=func.AuthLevel.ANONYMOUS)
    def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
        logging.info('Python HTTP trigger function processed a request.')
        source_filesystem = "func"
        source_filepath = "input.txt"
    
        try:
            service_client = get_service_client()
    
            # Read file content from the source
            file_content = read_file_from_datalake(service_client, source_filesystem, source_filepath)
            logging.info(f"Read content from {source_filepath}: {file_content}")
            processed_content = file_content.upper()
    
            return func.HttpResponse(f"File content in Uppercase{processed_content}")
    
        except Exception as e:
            logging.error(f"Error processing file: {e}")
            return func.HttpResponse(f"Error processing file: {e}", status_code=500)
    

    requirements.txt:

    azure-functions
    azure-identity
    azure-storage-file-datalake
    

    OUTPUT:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search