azure-functions-python-worker: Error importing Shared Code into Python function

Hack required to import Shared Code modules into Python functions

Investigative information

Repro steps

  1. Create a folder SharedCode adjacent to your function folders
  2. Added a python module into the SharedCode folder
  3. Attempt to import your SharedCode modules into existing Python Azure Function
  4. Run your Python Azure Function

Expected behavior

Module should be available by default because it is inside the same virtual environment without requiring any PYTHONPATH mods.

Actual behavior

Error:

Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Functions.HttpTriggerPython ---> Microsoft.Azure.WebJobs.Script.Rpc.RpcException: Result: Failure
Exception: ModuleNotFoundError: No module named 'SharedCode'

Known workarounds

Add the following lines to the top of your __init__.py in your Azure Function. This will allow any modules defined in your FunctionApp to be available for import

import sys
import os
sys.path.append(os.path.abspath(""))

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 2
  • Comments: 41 (11 by maintainers)

Commits related to this issue

Most upvoted comments

To import submodules directly: from …SharedCode import module1

I still don’t get it. I followed the instructions in the documentation page (matching the quoted comment here).

I get this in pylint and at runtime:

Attempted relative import beyond top-level package

Clearly, the documentation is wrong. What is the right way to share code between multiple functions in the same app? Can someone please fix the documentation?

To import submodules directly:

from ..SharedCode import module1

I had the same issue with needing absolute imports.

sys.path.append(os.path.abspath(“”)) > did not work sys.path.append(‘/home/site/wwwroot’) > did work

facing same issue… all imports working with SharedCode.MODULE 2 weeks ago, now it’s not working with error “Module not Found”

Codes are not finding or navigating to proper files suddenly everywhere

On Mon, Aug 12, 2019 at 7:22 AM Brad Bitterman notifications@github.com wrote:

Same thing is happening to me now. I had a function that was deployed and working fine. Now the same code stop working with this error. I didn’t change or deploy anything. I had to add the path append like @jaymegordo https://github.com/jaymegordo.

sys.path.append(‘/home/site/wwwroot’)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Azure/azure-functions-python-worker/issues/219?email_source=notifications&email_token=ACQLYQHHQMHA7I7LRP2JDM3QEFIXBA5CNFSM4FZXKBMKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD4CLLMI#issuecomment-520402353, or mute the thread https://github.com/notifications/unsubscribe-auth/ACQLYQBQPUGUFIK4K3VYR2LQEFIXBANCNFSM4FZXKBMA .

– Anindya Sankar Dey

Same thing is happening to me now. I had a function that was deployed and working fine. Now the same code stop working with this error. I didn’t change or deploy anything. I had to add the path append like @jaymegordo.

sys.path.append(‘/home/site/wwwroot’)

To clarify, we currently employ this importlib hackery to make even the relative imports work properly:

https://github.com/Azure/azure-functions-python-worker/blob/5ce1d218dcfd8560b665df06103c210a7a285826/azure/functions_worker/loader.py#L23-L29

Again, this is because we can’t really rely on the layout or the setting of PYTHONPATH.

@wbreza In your case your shared module code is one level up, so the correct syntax would be

from .. import SharedCode

@elprans Not working for my case.

from . import SharedCode results in error Exception: ImportError: cannot import name 'SharedCode'

I would like to import a submodule using the following or similar syntax from SharedCode import MyTestClass where MyTestClass is a class defined in module1.py for example.

Folder structure looks like the following

FunctionApp
|_ HttpTrigger1
    |_ __init__.py
    |_ function.json
|_ HttpTrigger2
    |_ __init__.py
    |_ function.json
|_ SharedCode
    |_ __init__.py
    |_ module1.py
    |_ module2.py