python - 在 azure 函数中找不到本地模块

标签 python python-3.x azure azure-functions python-import

我尝试重现this setup将本地模块导入到我的代码中。

这是我的文件夹结构,类似于之前的链接:

folders structure

和我的代码:

嘿/init.py

import logging

from shared.helloworld import sayhello # the way I would like to do it -> doesn't work

# from shared import helloworld # as per documentation, but doesn't work
# import shared.helloworld # as per documentation, still doesn't work


import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info(sayhello("from log"))
    return func.HttpResponse(
             sayhello("from http response"),
             status_code=200
        )

共享/helloworld.py

def sayhello(name : str) -> str:
    return "hello world " + name

我将 shared/__init__.py 保留为空,因为我读过这是一个不错的做法,并且该文件的存在只是为了将此文件夹标记为可导入。

无论我使用什么语法来导入模块,我总是遇到相同的错误:

Result: Failure Exception: ModuleNotFoundError: No module named 'shared'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound Stack: File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 315, in _handle__function_load_request func = loader.load_function( File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 42, in call raise extend_exception_message(e, message) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 40, in call return func(*args, **kwargs) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/loader.py", line 85, in load_function mod = importlib.import_module(fullmodname) File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/home/site/wwwroot/hey/__init__.py", line 3, in from shared.helloworld import sayhello

错误消息中链接的文档不涉及本地导入,我在这里不知道。 有人知道为什么我的导入不起作用吗?

最佳答案

当我使用 terraform 配置我的 az func 时,我的问题是我没有提供此 block :

resource "azurerm_linux_function_app" "auth_func" {
    ...
    application_stack {
      python_version = "3.9"
    }

如果其他人也面临这个问题,这里有一些调查途径:

  • 确保在 azure 函数中设置了 python 运行时
  • 导入包中的
  • __init__.py 为空或设计为在 azure 函数中工作(无本地引用)
  • requirements.txt 包含您在代码中使用的所有外部模块

关于python - 在 azure 函数中找不到本地模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72215655/

相关文章:

python - 在 Keras "ImageDataGenerator"中, "validation_split"参数是一种 K 折交叉验证吗?

Azure AD 无法访问 login.microsoftonline.com/{tenantId}

python - 为什么我的位置查找功能出现值错误?

python-3.x - 由于 python 版本,运行 PySpark DataProc 作业时出错

python-3.x - 标记默认月份 - python pandas

azure - 我应该将 Azure 应用程序注册证书放置在本地服务器上的哪个证书存储中

powershell - 登录后 Azure 凭据不可用

python - 在Python中,DOWNLOAD_DELAY和time.sleep有什么区别?

python - 在 Python 中根据列值减去行的最佳方法

python - 在新选项卡中打开链接并在选项卡之间切换(Selenium WebDriver + Python)