Azure Function Python Import python file

Po Chia Choon 0 Reputation points
2023-06-26T03:37:05.35+00:00

Hi

Would like to know how should I import py file in Azure function? I keep getting error code 500 when running the function when I am having my import code in my project

Untitled

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
Azure
Azure
A cloud computing platform and infrastructure for building, deploying and managing applications and services through a worldwide network of Microsoft-managed datacenters.
1,408 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MayankBargali-MSFT 70,936 Reputation points Moderator
    2023-06-26T03:51:15.8466667+00:00

    @Po Chia Choon Thanks for reaching out. As per the screenshot you have specified

    from ExeGet impart getDN..

    but I don't see any python file starting with getDN in your screenshot.

    Let' say if your file is CreateDNV.py and the folder is ExeGet then you need to use the below line

    from ExeGet import x
    

    Now let's say if you have the method define a test function inside your CreateDNV file you need to use below to call

    x.test()

    Sample example for your reference: https://stackoverflow.com/a/71999399/13796136

    Created a Folder calcfunction and added the add_number function:

    def  add_number(n1,n2):
    sum = n1 + n2;
    return  sum;
    print("The sum of two number is",sum)
    

    Calling this method from Azure Function Python Class:

    import  logging
    import  azure.functions  as  func
    
    from calcfunction import  calc
    
    def  main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    
    id = req.params.get('id')
    
    if  not  id:
    try:
    req_body = req.get_json()
    except  ValueError:
    pass
    else:
    id = req_body.get('id')
    
    if  id:
    return  func.HttpResponse(f"This is the user entered userId {id} and calc function value {calc.add_number(12,24)}")
    
    else:
    return  func.HttpResponse(
    "This HTTP triggered function executed successfully. Pass a ID in the query string or in the request body for a personalized response.",
    status_code=200
    )
    

    Folder Structure:

    enter image description here


  2. Khang Nguyen Ba 0 Reputation points
    2023-11-27T02:40:42.3033333+00:00

    Hi everyone, I'm using V2. How can i import a common.py file?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.