skip to Main Content

I have Azure Function App in Python hosted in App Service Plan in Basic tier. I uploaded function from local VSCode to Azure Portal. Function is visible and enabled in Azure Portal. Then I installed Python venv in function root folder (using SSH in portal) and next I installed all dependencies related to this function. When function is running in Azure (trigger by TimeTrigger) it returns that particular module "is not found". What do I need to do to get this function to work properly?

The steps I took on the SSH side in the function container in Azure Portal:

  1. Set up a Virtual Environment (venv):
    python -m venv myenv

  2. Activate the Virtual Environment:
    source myenv/bin/activate

  3. Install Dependencies in this venv:
    pip install --target=<path-to-site-packages> -r requirements.txt

  4. Verify Dependencies:
    pip list

All steps above performed correctly, Python version is identical between local environment and venv, I restarted the function and unfortunately python modules imported in __init__.py are not visible / downloaded.

I described steps which I tried.
I expected to run function app correctly with all dependencies.

2

Answers


  1. Chosen as BEST ANSWER

    I resolved it and it was 2 issues:

    1. Problem of Networking with Storage - I added VNet and special configuration to firewall and it works.

    2. So problem was that in main python file I had "import asyncio" - it was a problem for Azure, because 'asyncio' is already built-in package in Python, so since this it returned error related to 'Module Not Found'


    • To achieve your requirement, create requirements.txt file with all the required python packages in root folder of your project.
    • The requirements.txt is a simple text file which is used to specify the Python packages that are required by the Azure Function. When you deploy your Azure Function, the packages listed in the requirements.txt file are installed automatically in the function’s environment.

    I have created a simple python Azure function locally:

    Project Structure:

    enter image description here

    requirements.txt:

    I have used below packages in my project:

    azure-functions
    numpy
    pandas
    

    Code Snippet:

    import datetime
    import json
    import logging
    
    import azure.functions as func
    import numpy as np
    import pandas as pd
    
    app = func.FunctionApp()
    
    @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.')
    
        arr = np.array([1, 2, 3])
        df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
    
        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 with {arr} and {df}.")
        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
            )
    
    • Create and activate virtual environment, install the required packages:

    enter image description here

    Running locally:

    enter image description here

    enter image description here

    • Created Python Lunix Azure Function App with App Service Plan Basic tier.

    Deployed my function to Azure Function App:

    enter image description here

    • When you run the function, it activates virtual environment and install the dependencies which are mentioned in requirements.txt.

    Portal response:

    enter image description here

    enter image description here

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