0

We are building an http trigger using Azure functions.

import logging

import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
    else:
        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
        )

From the online platform, we were able to trigger it by adding a parameter and print it on the screen.

Once we try to import files from src folder which at the same level of HttpTrigger1,

import src.main

enter image description here

We have the following error:

Error in load_function. Sys Path: ['C:\Users\AliMehdy\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.10\WINDOWS\X64',

And it points out to main.py where we have already loaded a library called load_toml. Also over each import in main.py from src folder.

enter image description here

So it's throwing the same error over each module.

We did check the documentations and specifically that part:

When you're using absolute import syntax, the shared_code/ folder needs to contain an init.py file to mark it as a Python package.

Still, we didn't know what to do. Any idea how to import other modules into Azure http trigger function?

alim1990
  • 4,656
  • 12
  • 67
  • 130

1 Answers1

0

In One of my previous workarounds, I have created a Python File in the src like folder which is at the same level of HttpTrigger Function Folder level in the Azure Functions Python Project.

from calcfunction import  calc

This is the absolute path I have used in that Http Trigger Function:

enter image description here

Absolute paths are good to use for the above scenarios such as importing modules as a python package in other files.

Refer to this similar scenario on SO #58318794 provided by the user @HassanAbbas.