Error importing Shared Code into Python function
See original GitHub issueHack required to import Shared Code modules into Python functions
Investigative information
Repro steps
- Create a folder
SharedCode
adjacent to your function folders - Added a python module into the
SharedCode
folder - Attempt to import your
SharedCode
modules 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(""))
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:41 (10 by maintainers)
Top Results From Across the Web
Azure Functions - Unable to import other python modules in ...
It seems you haven't import "azure.datalake.store", your code just import some parts(core, lib, multithread) of "azure.datalake.store". Could ...
Read more >Importing Python Functions from Other Scripts | by Katy Hagerty
A script cannot handle two functions with the same name. Use as to avoid import error. Import All Functions and Modules from Script....
Read more >Resolve "Unable to import module" errors from Python ...
I receive an "Unable to import module" error when I try to run my AWS Lambda code in Python. How do I resolve...
Read more >Troubleshoot Python errors in Azure Functions - Microsoft Learn
Here are the troubleshooting sections for common issues in Python functions: ModuleNotFoundError and ImportError; Cannot import 'cygrpc'; Python ...
Read more >5. The import system — Python 3.11.1 documentation
Python code in one module gains access to the code in another module by the process of importing it. The import statement is...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
To import submodules directly:
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?