Azure函数-无法在调用的脚本中导入其他python模块
我在python中创建了一个简单的基于HTTP触发器的azure函数,它调用另一个python脚本在azure data lake gen 1中创建一个示例文件。我的解决方案结构如下所示:- Requirements.txt包含以下导入:-Azure函数-无法在调用的脚本中导入其他python模块,python,python-3.x,azure,azure-functions,Python,Python 3.x,Azure,Azure Functions,我在python中创建了一个简单的基于HTTP触发器的azure函数,它调用另一个python脚本在azure data lake gen 1中创建一个示例文件。我的解决方案结构如下所示:- Requirements.txt包含以下导入:- azure函数 azure管理资源 azure管理数据湖商店 azure数据湖商店 init.py import logging, os, sys import azure.functions as func import json def mai
- azure函数
- azure管理资源
- azure管理数据湖商店
- azure数据湖商店
import logging, os, sys
import azure.functions as func
import json
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:
full_path_to_script = os.path.join(os.path.dirname( __file__ ) + '/Test.py')
logging.info(f"Path: - {full_path_to_script}")
os.system(f"python {full_path_to_script}")
return func.HttpResponse(f"Hello {name}!")
else:
return func.HttpResponse(
"Please pass a name on the query string or in the request body",
status_code=400
)
import json
from azure.datalake.store import core, lib, multithread
directoryId = ''
applicationKey = ''
applicationId = ''
adlsCredentials = lib.auth(tenant_id = directoryId, client_secret = applicationKey, client_id = applicationId)
adlsClient = core.AzureDLFileSystem(adlsCredentials, store_name = '')
with adlsClient.open('stage1/largeFiles/TestFile.json', 'rb') as input_file:
data = json.load(input_file)
with adlsClient.open('stage1/largeFiles/Result.json', 'wb') as responseFile:
responseFile.write(data)
Test.py
import logging, os, sys
import azure.functions as func
import json
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:
full_path_to_script = os.path.join(os.path.dirname( __file__ ) + '/Test.py')
logging.info(f"Path: - {full_path_to_script}")
os.system(f"python {full_path_to_script}")
return func.HttpResponse(f"Hello {name}!")
else:
return func.HttpResponse(
"Please pass a name on the query string or in the request body",
status_code=400
)
import json
from azure.datalake.store import core, lib, multithread
directoryId = ''
applicationKey = ''
applicationId = ''
adlsCredentials = lib.auth(tenant_id = directoryId, client_secret = applicationKey, client_id = applicationId)
adlsClient = core.AzureDLFileSystem(adlsCredentials, store_name = '')
with adlsClient.open('stage1/largeFiles/TestFile.json', 'rb') as input_file:
data = json.load(input_file)
with adlsClient.open('stage1/largeFiles/Result.json', 'wb') as responseFile:
responseFile.write(data)
Test.py失败,错误为未找到azure.datalake.store模块
既然Test.py在同一目录中,为什么其他必需的模块不能为它工作
pip冻结输出:-
adal==1.2.2
azure-common==1.1.23
azure-datalake-store==0.0.48
azure-functions==1.0.4
azure-mgmt-datalake-nspkg==3.0.1
azure-mgmt-datalake-store==0.5.0
azure-mgmt-nspkg==3.0.2
azure-mgmt-resource==6.0.0
azure-nspkg==3.0.2
certifi==2019.9.11
cffi==1.13.2
chardet==3.0.4
cryptography==2.8
idna==2.8
isodate==0.6.0
msrest==0.6.10
msrestazure==0.6.2
oauthlib==3.1.0
pycparser==2.19
PyJWT==1.7.1
python-dateutil==2.8.1
requests==2.22.0
requests-oauthlib==1.3.0
six==1.13.0
urllib3==1.25.6
问题
os.system(f“python{full\u path\u to\u script}”)
来自您的functions项目导致了该问题
Azure Functions Runtime设置环境,同时修改进程级变量,如os.path
,以便您的函数可以加载任何依赖项。当您创建这样的子流程时,并非所有的信息都会通过。此外,您还将面临日志记录问题——除非明确处理,test.py
中的日志不会正确显示
导入可以在本地工作,因为您已经安装了所有的requirements.txt
模块,并且可用于test.py
。Azure中并非如此。作为发布的一部分进行远程构建后,您的模块将作为发布的代码包的一部分包含在内。它本身并不是在Azure环境中全局“安装”的
解决方案
你不应该那样运行你的脚本。在上面的示例中,您可以从\uuuu init\uuuuu.py
文件导入test.py
,其行为应该类似于调用python test.py
(至少在上述情况下是这样)。您希望在子进程中执行python test.py
而不是导入它,有什么原因吗
以下是有关如何构建应用程序以导入共享代码的官方指南--
旁注
我认为,一旦您解决了导入问题,您可能还会遇到adlsClient.open('stage1/largeFiles/TestFile.json','rb')的问题。我们建议按照上面的开发人员指南来组织项目,并使用\uuuuuuuuuuuuuuuuuuuuuuuuuuuuu
来获得绝对路径()
例如——
现在,如果您真的想让
os.system(f“python{full\u path\u to\u script}”)
工作,我们有一些解决导入问题的方法。但是,我不建议采用这种方法,除非您确实有迫切的需要。:) 问题
os.system(f“python{full\u path\u to\u script}”)
来自您的functions项目导致了该问题
Azure Functions Runtime设置环境,同时修改进程级变量,如os.path
,以便您的函数可以加载任何依赖项。当您创建这样的子流程时,并非所有的信息都会通过。此外,您还将面临日志记录问题——除非明确处理,test.py
中的日志不会正确显示
导入可以在本地工作,因为您已经安装了所有的requirements.txt
模块,并且可用于test.py
。Azure中并非如此。作为发布的一部分进行远程构建后,您的模块将作为发布的代码包的一部分包含在内。它本身并不是在Azure环境中全局“安装”的
解决方案
你不应该那样运行你的脚本。在上面的示例中,您可以从\uuuu init\uuuuu.py
文件导入test.py
,其行为应该类似于调用python test.py
(至少在上述情况下是这样)。您希望在子进程中执行python test.py
而不是导入它,有什么原因吗
以下是有关如何构建应用程序以导入共享代码的官方指南--
旁注
我认为,一旦您解决了导入问题,您可能还会遇到adlsClient.open('stage1/largeFiles/TestFile.json','rb')的问题。我们建议按照上面的开发人员指南来组织项目,并使用\uuuuuuuuuuuuuuuuuuuuuuuuuuuuu
来获得绝对路径()
例如——
现在,如果您真的想让
os.system(f“python{full\u path\u to\u script}”)
工作,我们有一些解决导入问题的方法。但是,我不建议采用这种方法,除非您确实有迫切的需要。:) 似乎您还没有导入“azure.datalake.store”,您的代码只是导入了“azure.datalake.store”的一些部分(核心、库、多线程)。请尝试在“Test.py”中添加一行“import azure.datalake.store”好吗?我已经尝试过了。不走运。相同错误-“ModuleNotFoundError:没有名为“azure”的模块”Hi-TechGuru,我可以知道屏幕截图是否包含您的所有代码吗?您的代码在本地运行成功吗?上述代码已完成。它在本地运行得很好。我刚才用你的代码测试了一下,它运行得很好。唯一的区别是我在“终端”中运行命令“pip freeze>requirements.txt”生成“requirements.txt”,然后部署到Azure。看起来你还没有导入“Azure.datalake.store”,你的代码只是导入“Azure.datalake.store”的一些部分(核心、库、多线程)。请尝试在“Test.py”中添加一行“import azure.datalake.store”好吗?我已经尝试过了。不走运。相同错误-“ModuleNotFoundError:没有名为“azure”的模块”Hi-TechGuru,我可以知道屏幕截图是否包含您的所有代码吗?您的代码在本地运行成功吗?上述代码已完成。它在本地运行得很好。我刚才用你的代码测试了一下,它运行得很好。唯一的区别是我在“TERMINAL”中运行命令“pip freeze>requirements.txt”生成“requirements.txt”,然后运行d