python-3.xazure-functionsazure-blob-storageazure-function-app

Azure Function ModuleNotFoundError in Python script


I have the following script which I am trying to run in Azure Function: init.py

import logging
import azure.functions as func
import pandas as pd
import numpy as np
from datetime import datetime

def main(myblob: func.InputStream):
    logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {myblob.name}\n"
                 f"Blob Size: {myblob.length} bytes")

Here is the function.json:

{
    "scriptFile": "__init__.py",
    "bindings": [
        {
            "name": "myblob",
            "type": "blobTrigger",
            "direction": "in",
            "path": "uwci-sftp-rb92351a6c-41fa-4b90-aa79-4e9974ca83f7/{name}",
            "connection": ""
        }
    ]
}

It was working fine when I imported only azure.functions and logging. I am getting this error only while trying to run the code in Azure Function. I am getting the following error for pandas or any other library for that matter:

Result: Failure
Exception: ModuleNotFoundError: No module named 'pandas'
Stack:   File "/azure-functions-host/workers/python/3.7/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.7/LINUX/X64/azure_functions_worker/loader.py", line 66, in load_function
    mod = importlib.import_module(fullmodname)
  File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/site/wwwroot/Test-UWCI2/__init__.py", line 3, in <module>
    import pandas as pd ```

Solution

  • You need to include a requirements.txt file with your code which lists all the python dependencies of your function.

    requirements.txt: Contains the list of packages the system installs when publishing to Azure.

    Reference:

    https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python#python-version-and-package-management