Has*_*bas 8 python azure python-3.x azure-functions
我正在构建一个 python 函数,我需要在代码中添加我的自定义模块。我的天蓝色函数如下所示:
import logging
import sys
from sys import path
import os
import time
sys.path.insert(0, './myFunctionFolderName') // Try to insert it as a standard python library.
import azure.functions as func
from myCustomModule.models import someFunctionName
def main(req: func.HttpRequest) -> func.HttpResponse:
name = "My new function"
testing_variable = someFunctionName()
return func.HttpResponse(f"Function executed {name}")]
Run Code Online (Sandbox Code Playgroud)
我所做的是将我的函数文件夹作为标准路径插入,以便 python 也在该文件夹中查找库。该功能在使用 Visual Studio Code 的本地环境中完美运行。但是,当我部署并运行它时,它抛出myCustomModule
未找到。另一条信息是我无法访问 Kudu(终端),因为我的 Linux 消费计划不支持它。我不确定我错过了什么。请不要,因为它不是标准库,我无法将其添加到requirements.txt中。另请注意,我的函数文件夹包含函数脚本文件和自定义模块,因此它们与 azure python 模板中应位于同一文件夹中。
Has*_*bas 16
使用绝对路径而不是相对路径。
以下对我有用
import logging
import sys
from sys import path
import os
import time
dir_path = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, dir_path)
import azure.functions as func
from myCustomModule.models import someFunctionName
def main(req: func.HttpRequest) -> func.HttpResponse:
name = "My new function"
testing_variable = someFunctionName()
return func.HttpResponse(f"Function executed {name}")]
Run Code Online (Sandbox Code Playgroud)
小智 7
比此响应更简单的解决方案是显式告诉 python 使用本地导入,在自定义模块导入之前添加一个点,如下所示:
import logging
import sys
from sys import path
import os
import time
import azure.functions as func
from .myCustomModule.models import someFunctionName
Run Code Online (Sandbox Code Playgroud)
它与 Azure Functions 配合使用
归档时间: |
|
查看次数: |
8906 次 |
最近记录: |