python - Azure函数内的多个文件(python应用程序)

标签 python azure azure-functions

我部署了一个azure function python应用程序,它使用代码库中的多个python文件。 目前结构如下:

├── HttpExample
│   ├── __init__.py
│   └── function.json
├── getting_started.md
├── host.json
├── local.settings.json
├── requirements.txt
└── shared_code
    ├── __init__.py
    ├── prep_backup.py
    ├── final_execution.py
    ├── final_helper.py
    ├── file_info.py
    └── requirements.txt

在我的 HttpExample 的 init.py 中,我调用函数来完成以下工作:

import logging

import azure.functions as func
from shared_code import final_execution

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    final_execution.run_function() # fails here

    if name:
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )

但是final_execution使用了指向的导入语句

from prep_backup import func1,func2

但是上面的 block 失败了:

Result: Failure Exception: ModuleNotFoundError: No module named 'prep_backup'. Please check the requirements.txt file for the missing module. 

所有必需的公共(public)包都在requeirement.txt中列出。

有什么建议可以解决这个问题吗?

最佳答案

由于您的 prep_backup.py 与调用者模块(final_execution.py)位于同一文件夹(shared_code)中,因此您可以尝试像这样导入函数(在模块名称前面添加“.”)。对于 Python 3 及以上版本都是如此。

from .prep_backup import func1,func2

现有的 SO question 中已经提到了这一点.

关于python - Azure函数内的多个文件(python应用程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76897481/

相关文章:

python - 在 GAE 上启用带有 web2py 的设备

python - 通过删除 Python 中的子集来折叠短语列表

python - 从文本文件中删除空行、点和逗号

c# - Azure Functions 数据库连接字符串

c# - .Net 6 FunctionApp : No job functions found. 尝试将您的作业类和方法公开

azure - 如何使用 Visual Studio 2015 在 Azure 函数中定义连接字符串?

python - 解决 AWS Lambda 空间限制

azure - 是否需要有 azure 备份计划?

python - 如何使用 Azure Functions Blob 触发器和 Python 从 Blob 存储中读取 json 文件

c# - Azure应用程序功能(功能2.x): Create new document in CosmosDB via HTTP function