使用Python Azure Functions时新建了.py文件并执行了import 操作,运行时提示如下错误:
Result: Failure
[2021-01-25T05:16:11.553Z] Exception: ModuleNotFoundError: No module named 'helper'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound
Stack: File "/usr/local/Cellar/azure-functions-core-tools@3/3.0.3233/workers/python/3.8/OSX/X64/azure_functions_worker/dispatcher.py",
解决方案:
在__init__.py文件中增加:
import sys
from sys import path
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, dir_path)
增加完成后的默认模版建立的http trigger目录如下图所示:
修改后的__init__.py文件:
import logging
import sys
from sys import path
import os
import azure.functions as func
dir_path = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, dir_path)
from helper import my_func
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
result = my_func(name)
return func.HttpResponse(f"Hello, {result}. 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
)
新增的helper.py代码如下:
def my_func(str):
result = str+'_this msg is added by my_func'
return result
运行调试的结果如下:
在浏览器中打开:http://localhost:7071/api/HttpTrigger1?name=test
方法2:
如下图使用文件的完整路径: