Azure Functions - 没有名为“__app__.appname.error_codes”的模块

问题描述 投票:0回答:1

我在尝试从网络运行 TimerTigger AZ 函数时收到此错误。

Result: Failure Exception: ModuleNotFoundError: No module named '**app**.appname.error_codes'. Please check the requirements.txt file for the missing module. For more info, please refer the troubleshooting guide:  [https://aka.ms/functions-modulenotfound](https://aka.ms/functions-modulenotfound)  Stack: File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/dispatcher.py", line 380, in _handle__function_load_request func = loader.load_function( File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 48, in call raise extend_exception_message(e, message) File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 44, in call return func(*args, **kwargs) File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/loader.py", line 132, in load_function mod = importlib.import_module(fullmodname) File "/usr/local/lib/python3.10/importlib/**init**.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/home/site/wwwroot/hubs_watchtower_data/**init**.py", line 8, in from .utils import Client File "/home/site/wwwroot/hubs_watchtower_data/utils.py", line 12, in from .models import Site File "/home/site/wwwroot/hubs_watchtower_data/models.py", line 11, in from .error_codes import Error

我尝试重新创建它但没有成功?

python azure function azure-functions error-code
1个回答
0
投票

当我尝试将函数中另一个文件夹中的 error_codes.py 文件导入到我的 init.py 文件时,我收到了相同的错误代码,请参阅以下内容:-

enter image description here

我的本地函数应用程序:-

init.py

import datetime
import logging
import sys, os

import azure.functions as func
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
from appname.error_codes import ErrorCode, ErrorMessages

def main(mytimer: func.TimerRequest) -> None:
    logging.info("Timer trigger function executed.")

    # Simulate an error
    error_code = ErrorCode.DATABASE_ERROR

    # Get the error message based on the error code
    error_message = ErrorMessages.get_message(error_code)
    
    logging.error(f"Error Code: {error_code}")
    logging.error(f"Error Message: {error_message}")

我的 error_codes.py 在 appname 文件夹中:-

class ErrorCode:
    GENERIC_ERROR = 100
    DATABASE_ERROR = 200
    API_ERROR = 300

class ErrorMessages:
    error_messages = {
        ErrorCode.GENERIC_ERROR: "A generic error occurred.",
        ErrorCode.DATABASE_ERROR: "A database error occurred.",
        ErrorCode.API_ERROR: "An API error occurred."
    }

    @classmethod
    def get_message(cls, error_code):
        return cls.error_messages.get(error_code, "Unknown error.")

函数执行成功:-

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.