3

I think I'm trying to do something really simple but keep running into problems.

I am trying to deploy a Python function that uses two Python files, __init__.py and another one called handler.py. Here is all of __init__.py:

import json
import logging
import azure.functions as func
import handler

def main(req: func.HttpRequest) -> func.HttpResponse:
    result = handler.yourFunction(req.params, None)
    return func.HttpResponse(result)

For whatever reason, whenever I run this function I get an error saying that the handler module cannot be found. I can view the folder structure on Azure and see that it is in the same folder as __init__.py.

I am really confused why this is not working. I can deploy Node.js functions just fine but Python ones just keep running into problems.

2
  • What exactly is the traceback on the import? Commented Sep 15, 2019 at 9:20
  • Here is another import statement: Result: Failure Exception: ModuleNotFoundError: No module named 'Inspector' Stack: File "/usr/local/lib/python3.6/site-packages/azure_functions_worker/dispatcher.py", line 239, in _handle__function_load_request func_request.metadata.entry_point) File "/usr/local/lib/python3.6/site-packages/azure_functions_worker/loader.py", line 66, in load_function ... File "/home/site/wwwroot/pythontestt51234y/handler.py", line 3, in <module> from Inspector import * Commented Sep 16, 2019 at 0:15

3 Answers 3

2

I found this worked fine:

from . import mylibrary as mlib

This also helped in places:

from . import mylibrary # pylint: disable=relative-beyond-top-level
from ..myfolder import mylibrary # pylint: disable=relative-beyond-top-level

Disabling the pylint warning here as it's a false positive for azure.

Sign up to request clarification or add additional context in comments.

Comments

1

It's not supporting python very well using Azue function service.

For the python module finding mechanism, it will find the module listed in sys.path. So if you want to import your own module. You need to add your current folder in sys.path. Add something like this before import your own module:

import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__))))

import handler

This works on my local.

2 Comments

Awesome! Solved my issue. I spent DAYS trying to figure out why this was not working.
So I have init.py and blob-quickstart-v12.py within the same Azure Function "Test-v3". While init.py is a blob trigger, "blob-quickstart-v12.py " has the python code that I want to run. Can you please exemplify how I should be running this? I am new to Azure function
1

This document goes into some patterns you can use to import shared code:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python#folder-structure

I personally have found luck with import ..SharedCode/myHander

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.