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
- Create a folder
SharedCodeadjacent to your function folders - Added a python module into the
SharedCodefolder - Attempt to import your
SharedCodemodules into existing Python Azure Function - 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
- Decode Jar File Path (#219) — committed to Azure/azure-functions-python-worker by pragnagopa 6 years ago
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 packageClearly, 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:
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:
– 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
@elprans Not working for my case.
from . import SharedCoderesults in errorException: ImportError: cannot import name 'SharedCode'I would like to import a submodule using the following or similar syntax
from SharedCode import MyTestClasswhereMyTestClassis a class defined inmodule1.pyfor example.Folder structure looks like the following