question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error importing Shared Code into Python function

See original GitHub issue

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(""))

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:41 (10 by maintainers)

github_iconTop GitHub Comments

5reactions
elpranscommented, Oct 9, 2018

To import submodules directly:

from ..SharedCode import module1
4reactions
polarapfelcommented, Apr 11, 2019

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found