Darshat
Darshat

Reputation: 73

Deployment of azure function in python doesnt install dependencies from requirements.txt

I have a simple python script to deploy as azure function following instructions here.

When the script is triggered (set for event grid event of blob upload), it hits a runtime error:

Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Functions.imageeventgridtrigger --->
  Microsoft.Azure.WebJobs.Script.Rpc.RpcException: Result: Failure
  Exception: ModuleNotFoundError: No module named 'numpy' Stack:   File
  "/azure-functions-host/workers/python/3.6/LINUX/X64/azure_functions_worker/dispatcher.py",
  line 242, in _handle__function_load_request
      func_request.metadata.entry_point)   File "/azure-functions-host/workers/python/3.6/LINUX/X64/azure_functions_worker/loader.py",
  line 66, in load_function
      mod = importlib.import_module(fullmodname)   File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in
  import_module
      return _bootstrap._gcd_import(name[level:], package, level)   File "/home/site/wwwroot/imageeventgridtrigger/__init__.py", line 3, in
  <module>
      import numpy as np    at Microsoft.Azure.WebJobs.Script.Description.OutOfProcInvoker.InvokeCore(Object[]
  parameters, FunctionInvocationContext context) in
  /src/azure-functions-host/src/WebJobs.Script/Description/OutOfProc/OutOfProcInvoker.cs:line
  82*

[ snipped rest of trace] The deployment was via vscode integration. The requirements file seems to be partially executed based on log output:

10:09:44 AM manuscriptfunctionapp: Python Version: /opt/python/3.6.9/bin/python3 10:09:44 AM manuscriptfunctionapp:
  Running pip install... 10:09:47 AM manuscriptfunctionapp:
  [04:39:47+0000] Collecting azure-functions (from -r requirements.txt
  (line 1)) 10:09:48 AM manuscriptfunctionapp: [04:39:48+0000]  
  Downloading
  https://files.pythonhosted.org/packages/68/b3/535af50791b2fcda75d3e18820588885686a33d8a13f83af6dcf74094cf4/azure_functions-1.0.4-py3-none-any.whl
  (108kB) 10:09:48 AM manuscriptfunctionapp: [04:39:48+0000] Collecting
  numpy==1.16.4 (from -r requirements.txt (line 2)) 10:09:50 AM
  manuscriptfunctionapp: [04:39:50+0000]   Downloading
  https://files.pythonhosted.org/packages/87/2d/e4656149cbadd3a8a0369fcd1a9c7d61cc7b87b3903b85389c70c989a696/numpy-1.16.4-cp36-cp36m-manylinux1_x86_64.whl
  (17.3MB) 10:09:52 AM manuscriptfunctionapp: [04:39:52+0000] Collecting
  matplotlib==3.1.0 (from -r requirements.txt (line 3)) 10:09:53 AM
  manuscriptfunctionapp: [04:39:53+0000]   Downloading
  https://files.pythonhosted.org/packages/da/83/d989ee20c78117c737ab40e0318ea221f1aed4e3f5a40b4f93541b369b93/matplotlib-3.1.0-cp36-cp36m-manylinux1_x86_64.whl
  (13.1MB) 10:10:07 AM manuscriptfunctionapp: Waiting for long running
  command to finish... 10:10:25 AM manuscriptfunctionapp: App container
  will begin restart within 10 seconds. 10:10:44 AM
  manuscriptfunctionapp: Syncing triggers... 10:10:48 AM: Deployment to
  "manuscriptfunctionapp" completed. 10:10:48 AM manuscriptfunctionapp:
  Querying triggers...

The requirements file has other dependencies but they don't appear in log output (only till line 3!). Tried deploying multiple times but same results. Requirements.txt:

azure-functions
numpy==1.16.4
matplotlib==3.1.0
opencv_python==4.1.1.26
scikit_image==0.15.0
scipy==1.2.1
skimage==0.0

Python script:

mport json
import logging
import numpy as np
import matplotlib
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import statistics
import cv2
import os
import scipy
import scipy.signal as ss
import skimage
from skimage.filters import (threshold_otsu, threshold_niblack, threshold_sauvola)
from skimage.restoration import denoise_nl_means, estimate_sigma

import azure.functions as func


def main(event: func.EventGridEvent):
    result = json.dumps({
        'id': event.id,
        'data': event.get_json(),
        'topic': event.topic,
        'subject': event.subject,
        'event_type': event.event_type,
    })

    logging.info('Python EventGrid trigger processed an event: %s', result)
    logging.info('cv2 %s ', cv2.__version__)
    logging.info('matplotlib %s ', matplotlib.__version__)
    logging.info('scipy %s ', scipy.__version__)
    logging.info('skimage %s', skimage.__version__)

The deployment and trigger works ok if only azure-functions is mentioned in the requirements. This is for an Always On app service plan and deployment is happening to Linux. What is going wrong with deployment?

Upvotes: 4

Views: 12359

Answers (2)

Sudharshann D
Sudharshann D

Reputation: 2103

In my case, I was just dumping the files from my Azure Dev Ops (ADO) repo to the Azure Function.

The correct procedure is to first build the code and only then deploy it. This is one method of making the ADO pipeline that build the python code and then published the drop to Azure Functions.

Upvotes: 0

Darshat
Darshat

Reputation: 73

Figured out the issue. The requirements.txt file had a spurious entry for skimage. The imports in python refer to skimage but the requirements only must be scikit-image. Since I generated the file via pipereqs I missed that.

Oddly enough spotting the deployment logs is not easy. The default output in vscode terminal for azure func does not have the full stack. But under Azure extension tab, there is detailed deployment logs available for the function app that had necessary info to debug.

Upvotes: 2

Related Questions