在 azure 函数中导入自定义模块时出现 ModuleNotFound

时间:2021-06-11 22:53:47

标签: python python-3.x azure azure-functions python-module

我使用 vscocde azure 函数模板创建了一个项目。我想导入一个脚本来运行该函数。但是将我的 mymodule.py 文件与 __init__.py 放在同一目录并使用 import mymodule 后,我收到以下错误

> 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

1 个答案:

答案 0 :(得分:0)

您需要注意的两件事:

首先,如果mymodule是可以通过pip安装的公共模块,那么你应该把它放在requirements.txt中,然后你就可以得到它。

其次,如果mymodule是自己创建的模块,那么就是python相对导入问题。您需要使文件夹可以访问。

如果还是不行,请出示你的代码的导入部分和你的函数应用结构的截图。