Azure函数在Python中引发键错误。

huangapple go评论98阅读模式
英文:

Azure functions in python throws key error

问题

{
"translation": "我正在本地开发和测试一个使用Python的Azure函数。我已经将这些条目添加到我的local.settings.json文件中 -",
"translation_1": " {",
"translation_2": " "IsEncrypted": false,",
"translation_3": " "Values": {",
"translation_4": " "CONNECTION_STRING": "连接字符串",",
"translation_5": " "DEFAULT_AVATAR_URL": "测试头像"",
"translation_6": " }",
"translation_7": " }",
"translation_8": "当我在代码的另一部分访问底部的值时,我收到KeyError。导入已经正确添加,因为我能够访问其他环境变量。",
"translation_9": "以下是我的使用方式 -",
"translation_10": " import os",
"translation_11": " ",
"translation_12": " url = os.environ["DEFAULT_AVATAR_URL"]",
"translation_13": "请提供建议!"
}

英文:

I am locally developing and testing an Azure function in Python. I have these entries added to my local.settings.json file -

  1. {
  2. "IsEncrypted": false,
  3. "Values": {
  4. "CONNECTION_STRING": "Connection String",
  5. "DEFAULT_AVATAR_URL": "Test-avatar"
  6. }
  7. }

When I access the bottom value from another section of the code I get KeyError. The imports have been properly added as I do get to access other environment variables.

Below is how I use it -

  1. import os
  2. url = os.environ["DEFAULT_AVATAR_URL"]

Please advise!

答案1

得分: 1

I have reproduced in my environment and got expected results as below:

local.settings.json

  1. {
  2. "IsEncrypted": false,
  3. "Values": {
  4. "AzureWebJobsStorage": "",
  5. "FUNCTIONS_WORKER_RUNTIME": "python",
  6. "URL": "Test-avatar"
  7. }
  8. }

One way of doing is calling it in:
__init__.py

  1. import logging
  2. import os
  3. import azure.functions as func
  4. def main(req: func.HttpRequest) -> func.HttpResponse:
  5. logging.info('Python HTTP trigger function processed a request.')
  6. s = os.getenv("URL")
  7. print(s)
  8. name = req.params.get('name')
  9. if not name:
  10. try:
  11. req_body = req.get_json()
  12. except ValueError:
  13. pass
  14. else:
  15. name = req_body.get('name')
  16. if name:
  17. return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
  18. else:
  19. return func.HttpResponse(
  20. "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
  21. status_code=200
  22. )

Output:

Another way is you can call from a python script inside the app like below:

p1.py:

__init__.py

  1. import logging
  2. import os
  3. import sys
  4. import azure.functions as func
  5. import importlib.util
  6. def main(req: func.HttpRequest) -> func.HttpResponse:
  7. logging.info('Python HTTP trigger function processed a request.')
  8. rss('p1')
  9. name = req.params.get('name')
  10. if not name:
  11. try:
  12. req_body = req.get_json()
  13. except ValueError:
  14. pass
  15. else:
  16. name = req_body.get('name')
  17. if name:
  18. return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
  19. else:
  20. return func.HttpResponse(
  21. "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
  22. status_code=200
  23. )
  24. def rss(snn):
  25. sp = os.path.join(os.path.dirname(__file__), f'{snn}.py')
  26. ss = importlib.util.spec_from_file_location(snn, sp)
  27. mm = importlib.util.module_from_spec(ss)
  28. sys.modules[ss.name] = mm
  29. ss.loader.exec_module(mm)

Output:

These are the ways you can call or use them, and I didn't face any errors, and you will not too if you follow the above ways.

英文:

I have reproduced in my environment and got expected results as below:

local.settings.json

  1. {
  2. "IsEncrypted": false,
  3. "Values": {
  4. "AzureWebJobsStorage": "",
  5. "FUNCTIONS_WORKER_RUNTIME": "python",
  6. "URL": "Test-avatar"
  7. }
  8. }

Azure函数在Python中引发键错误。

One way of doing is calling it in :
__init__.py

  1. import logging
  2. import os
  3. import azure.functions as func
  4. def main(req: func.HttpRequest) -> func.HttpResponse:
  5. logging.info('Python HTTP trigger function processed a request.')
  6. s= os.getenv("URL")
  7. print(s)
  8. name = req.params.get('name')
  9. if not name:
  10. try:
  11. req_body = req.get_json()
  12. except ValueError:
  13. pass
  14. else:
  15. name = req_body.get('name')
  16. if name:
  17. return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
  18. else:
  19. return func.HttpResponse(
  20. "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
  21. status_code=200
  22. )

Output:

Azure函数在Python中引发键错误。

Another way is you can call from a python script inside the app like below:

p1.py:

Azure函数在Python中引发键错误。

__init__.py

  1. import logging
  2. import os
  3. import sys
  4. import azure.functions as func
  5. import importlib.util
  6. def main(req: func.HttpRequest) -> func.HttpResponse:
  7. logging.info('Python HTTP trigger function processed a request.')
  8. rss('p1')
  9. name = req.params.get('name')
  10. if not name:
  11. try:
  12. req_body = req.get_json()
  13. except ValueError:
  14. pass
  15. else:
  16. name = req_body.get('name')
  17. if name:
  18. return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
  19. else:
  20. return func.HttpResponse(
  21. "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
  22. status_code=200
  23. )
  24. def rss(snn):
  25. sp = os.path.join(os.path.dirname(__file__), f'{snn}.py')
  26. ss = importlib.util.spec_from_file_location(snn, sp)
  27. mm = importlib.util.module_from_spec(ss)
  28. sys.modules[ss.name] = mm
  29. ss.loader.exec_module(mm)

Output:

Azure函数在Python中引发键错误。

these are the ways you can call or use them and I didn't faced any errors and you will not too if you follow above ways.

huangapple
  • 本文由 发表于 2023年6月13日 11:00:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461449.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定