我部署了一个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/