skip to Main Content

I have an azure function app to which I am trying to deploy some python code to via zip upload through the CLI. I can get a hello world application to run but I cannot add external libraries. I added a requirements.txt in my root folder but this is not being picked up.

Many solutions and suggestions I found on the web revolve around pointing the python path to /home/site/wwwroot/.python_packages but when I ssh into the machine this path doesn’t even exist. I tried to put a venv in that path manually and installing the required packages but the function code still fails during import.

I used the portal to create a function app with python 3.11 on linux. Is this .python_packages folder still supposed to be there or did something change?

2

Answers


  1. Chosen as BEST ANSWER

    The reason the requirements.txt was not installing was that two flags were missing. In the environment variables of the function the following was missing: SCM_DO_BUILD_DURING_DEPLOYMENT: true. When using the zip upload command this variable will be set to false and in order to prevent that I needed to add the --build-remote flag to my az cli command.

    az functionapp deployment source config-zip 
      --resource-group <ResourceGroupName> 
      --name <FunctionAppName> 
      --src <PathToZipFile> 
      --build-remote
    

    This led to the requirements being installed upon deployment.


  2. When I was zipping my files with my local environment which is .venv where all my additional packages are installed, function was not visible on azure even if deployment was successful.

    It was only working with default code. where no additional packages were available..

    But this worked for me.

    I installed packages in .python_packages using this command in locally.

    pip install  --target="<PROJECT_DIR>/.python_packages/lib/site-packages"  -r requirements.txt  
    # use <PROJECT_DIR>='.' for current directory
    

    And then I zipped files with .python_packages, not .venv.

    requirements.txt:

    azure-functions
    requests
    

    function_app.py:

    import azure.functions as func
    import logging
    import requests
    
    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.')
    
        result = requests.get("https://dog.ceo/api/breeds/image/random")
        return func.HttpResponse(
                f"This HTTP triggered function executed successfully.{result.text}",
                status_code=200
        )
    

    OUTPUT

    az functionapp deployment source config-zip -g Vivek-RG -n pyfunc20sep --src host.zip   #src path
    

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