Will
Will

Reputation: 2410

local module not found in azure function

I try to reproduce this setup to import local modules into my code.

Here is my folders structure, similar to previous link:

folders structure

and my code :

hey/init.py

import logging

from shared.helloworld import sayhello # the way I would like to do it -> doesn't work

# from shared import helloworld # as per documentation, but doesn't work
# import shared.helloworld # as per documentation, still doesn't work


import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info(sayhello("from log"))
    return func.HttpResponse(
             sayhello("from http response"),
             status_code=200
        )

shared/helloworld.py

def sayhello(name : str) -> str:
    return "hello world " + name

I have kept shared/__init__.py empty as I have read this is an ok practice and that file was only present to mark this folder as importable.

No matter what syntax I use for importing my module, I always hit the same error :

Result: Failure Exception: ModuleNotFoundError: No module named 'shared'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound Stack: File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 315, in _handle__function_load_request func = loader.load_function( File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 42, in call raise extend_exception_message(e, message) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 40, in call return func(*args, **kwargs) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/loader.py", line 85, in load_function mod = importlib.import_module(fullmodname) File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/home/site/wwwroot/hey/__init__.py", line 3, in from shared.helloworld import sayhello

The documentation linked in error message does not concern local import and I'm out of idea here. Anyone having a clue why my import doesn't work ?

Upvotes: 2

Views: 5909

Answers (2)

Will
Will

Reputation: 2410

As I was provisionning my az func with terraform, my issue was I didn't provide this block :

resource "azurerm_linux_function_app" "auth_func" {
    ...
    application_stack {
      python_version = "3.9"
    }

In case someone else is facing this issue, here a few path to investigate :

  • make sure you python runtime is set in the azure function
  • __init__.py from imported package is empty or is designed to work in azure function (no local references)
  • requirements.txt contains all external modules you're using in code

Upvotes: 1

JIJO JOSEPH
JIJO JOSEPH

Reputation: 375

The documentation explains it better . Refer hereMicrosoft Doc

Upvotes: -2

Related Questions