question

ErfanNariman-5893 avatar image
0 Votes"
ErfanNariman-5893 asked Shalom-1708 commented

Azure functions ModuleNotFoundError for python script in same folder.

I have the following project structure:
36600-image.png

My main.py looks as followed:

python
import datetime
import logging
import azure.functions as func
from .process_data import export_to_azure, create_dataframe


def main(timer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    # on timer run export data
    df = create_dataframe()
    export_to_azure(table_name="sdp_taken", schema="monday", df=df)

    logging.info('Python timer trigger function ran at %s', utc_timestamp)


When I have main.py as my main script to run and run func start, I get:

[2020-11-02T02:19:58.327] Exception: ModuleNotFoundError: No module named 'monday'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound
[2020-11-02T02:19:58.327] Stack:   File "/usr/local/Cellar/azure-functions-core-tools@3/3.0.2931/workers/python/3.8/OSX/X64/azure_functions_worker/dispatcher.py", line 262, in _handle__function_load_request
[2020-11-02T02:19:58.327]     func = loader.load_function(
[2020-11-02T02:19:58.327]   File "/usr/local/Cellar/azure-functions-core-tools@3/3.0.2931/workers/python/3.8/OSX/X64/azure_functions_worker/utils/wrappers.py", line 34, in call
[2020-11-02T02:19:58.327]     raise extend_exception_message(e, message)
[2020-11-02T02:19:58.327]   File "/usr/local/Cellar/azure-functions-core-tools@3/3.0.2931/workers/python/3.8/OSX/X64/azure_functions_worker/utils/wrappers.py", line 32, in call
[2020-11-02T02:19:58.327]     return func(*args, **kwargs)
[2020-11-02T02:19:58.327]   File "/usr/local/Cellar/azure-functions-core-tools@3/3.0.2931/workers/python/3.8/OSX/X64/azure_functions_worker/loader.py", line 76, in load_function
[2020-11-02T02:19:58.327]     mod = importlib.import_module(fullmodname)
[2020-11-02T02:19:58.327]   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
[2020-11-02T02:19:58.327]     return _bootstrap._gcd_import(name[level:], package, level)
[2020-11-02T02:19:58.328]   File "/Users/erfannariman/Workspace/zypp/monday/monday/main.py", line 4, in <module>
[2020-11-02T02:19:58.328]     from .process_data import export_to_azure, create_dataframe
[2020-11-02T02:19:58.328]   File "/Users/erfannariman/Workspace/zypp/monday/monday/process_data.py", line 3, in <module>
[2020-11-02T02:19:58.328]     from monday.api import request_data


But, when I put all the code in one file and then run func start, everything works fine.

I am not sure how to solve this and what causes this, I read this thread, but that did not help.

function.json looks as followed:

{
  "scriptFile": "main.py",
  "bindings": [
    {
      "name": "timer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */5 * * * *"
    }
  ]
}




azure-functions
image.png (79.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ErfanNariman-5893 avatar image
0 Votes"
ErfanNariman-5893 answered JayaC-MSFT commented

Apparently although the the script runs with


from monday.process_data import process_func


That will not work with azure functions. I had to change this to:

from .process_data import process_func
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ErfanNariman-5893 avatar image
0 Votes"
ErfanNariman-5893 answered ErfanNariman-5893 edited

@JayaC-MSFT it's not solvable with pip install since it's just my own written script in the same folder, see screenshot. For example I am loading process_data.py in main.py. The import error is not about an external package which I pip installed. Here's an simplified example:

monday/process_data.py:

def process_func():
    # do some data processing


Then in monday/main.py:

from monday.process_data.py import process_func

# do something with the process_func


I also tried from .proccess_data import process_func, but that did not help either.

Import note: when I run the python script without func start, everything works fine and it finds the other scripts/modules. So it is a problem that occurs when running func start

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Shalom-1708 avatar image
0 Votes"
Shalom-1708 answered Shalom-1708 commented

Hi @ErfanNariman-5893 ,
Did you solve this problem?
I had the same problem as you.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Even i am getting the same error . Can anyone please point me how to resolve this.

0 Votes 0 ·
Shalom-1708 avatar image Shalom-1708 SharmaKartikeya-7489 ·

I add those 3 lines at the top of each file. it resolved the problem.

 import sys
 import os
 sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))

I don't like this solution because sometimes there is a project with many files. but it's works for now

0 Votes 0 ·