0

Using the vscocde azure functions template I created a project. And I want to import a script to run with the function. But after placing my mymodule.py file in the same directory as the __init__.py and using import mymodule I get the following error

> Executing task: . .venv/bin/activate && func host start <

Found Python version 3.8.2 (python3).

Azure Functions Core Tools
Core Tools Version:       3.0.3477 Commit hash: 5fbb9a76fc00e4168f2cc90d6ff0afe5373afc6d  (64-bit)
Function Runtime Version: 3.0.15584.0

[2021-06-11T22:24:04.600Z] Cannot create directory for shared memory usage: /dev/shm/AzureFunctions
[2021-06-11T22:24:04.600Z] System.IO.FileSystem: Access to the path '/dev/shm/AzureFunctions' is denied. Operation not permitted.

Functions:

        MyAzureTimerTrigger: timerTrigger

For detailed output, run func with --verbose flag.
[2021-06-11T22:24:08.227Z] Traceback (most recent call last):
[2021-06-11T22:24:08.227Z]   File "/usr/local/Cellar/azure-functions-core-tools@3/3.0.3477/workers/python/3.8/OSX/X64/azure_functions_worker/bindings/shared_memory_data_transfer/file_accessor_unix.py", line 127, in _get_valid_mem_map_dirs
[2021-06-11T22:24:08.227Z]     os.makedirs(dir_path)
[2021-06-11T22:24:08.227Z]   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/os.py", line 213, in makedirs
[2021-06-11T22:24:08.228Z]     makedirs(head, exist_ok=exist_ok)
[2021-06-11T22:24:08.228Z]   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/os.py", line 223, in makedirs
[2021-06-11T22:24:08.228Z]     mkdir(name, mode)
[2021-06-11T22:24:08.228Z] PermissionError: [Errno 1] Operation not permitted: '/dev/shm'
[2021-06-11T22:24:08.369Z] Worker process started and initialized.
[2021-06-11T22:24:08.396Z] Worker failed to function id d82d7162-c855-4d3e-8e8c-eb2ad67ae871.
[2021-06-11T22:24:08.396Z] Result: Failure
[2021-06-11T22:24:08.396Z] Exception: ModuleNotFoundError: No module named 'mymodule'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound
[2021-06-11T22:24:08.396Z] Stack:   File "/usr/local/Cellar/azure-functions-core-tools@3/3.0.3477/workers/python/3.8/OSX/X64/azure_functions_worker/dispatcher.py", line 302, in _handle__function_load_request
[2021-06-11T22:24:08.396Z]     func = loader.load_function(
[2021-06-11T22:24:08.396Z]   File "/usr/local/Cellar/azure-functions-core-tools@3/3.0.3477/workers/python/3.8/OSX/X64/azure_functions_worker/utils/wrappers.py", line 42, in call
[2021-06-11T22:24:08.397Z]     raise extend_exception_message(e, message)
[2021-06-11T22:24:08.397Z]   File "/usr/local/Cellar/azure-functions-core-tools@3/3.0.3477/workers/python/3.8/OSX/X64/azure_functions_worker/utils/wrappers.py", line 40, in call
[2021-06-11T22:24:08.397Z]     return func(*args, **kwargs)
[2021-06-11T22:24:08.397Z]   File "/usr/local/Cellar/azure-functions-core-tools@3/3.0.3477/workers/python/3.8/OSX/X64/azure_functions_worker/loader.py", line 83, in load_function
[2021-06-11T22:24:08.397Z]     mod = importlib.import_module(fullmodname)
[2021-06-11T22:24:08.397Z]   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
[2021-06-11T22:24:08.397Z]     return _bootstrap._gcd_import(name[level:], package, level)
[2021-06-11T22:24:08.397Z]   File "..../MyAzureTimerTrigger/__init__.py", line 3, in <module>
[2021-06-11T22:24:08.397Z]     import mymodule as f
[2021-06-11T22:24:08.397Z] .
[2021-06-11T22:24:12.833Z] Host lock lease acquired by instance ID '000000000000000000000000FC309749'.

Terminal will be reused by tasks, press any key to close it.

screenshot of file imports screenshot of file structure

gh0st
  • 1,653
  • 3
  • 27
  • 59

2 Answers2

0

Two things you need to notice:

First, if the mymodule is a public module that can installed by pip, then you should put it in the requirements.txt, then you can get it.

Second, if the mymodule is the module that you create by yourself, then it is a python relative import question. You need to make the folder can be accessible.

If still doesn't work, please show the import part of your code and the screenshot of your function app structure.

Cindy Pau
  • 13,085
  • 1
  • 15
  • 27
0

Source: Python developer reference for Azure Functions | Microsoft Docs

https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python?tabs=azurecli-linux%2Capplication-level#folder-structure

Try one of these:

from shared_code import my_first_helper_function #(absolute)  
import shared_code.my_second_helper_function #(absolute)       
from . import example #(relative)

In my case this import doesn`t work in Visual Studio Code but on Azure this works.

Also, if you have still problem try to add aliases when you use custom module. In my case, I imported three custom module and Azure Function show error massage. I added aliace on first imported module and Azure Function worked, I do not know why.

In Visual Studio Code I use this code to import custom module and test my Azure Function script init.py:

import moduleName
Drvoseca
  • 1
  • 1