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

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

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 -

{
  "IsEncrypted": false,
  "Values": {
    "CONNECTION_STRING": "Connection String",
    "DEFAULT_AVATAR_URL": "Test-avatar"
  }
}

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 -

import os

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

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "URL": "Test-avatar"
  }
}

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

import logging
import os
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    s = os.getenv("URL")
    print(s)
    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:
        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
        )

Output:

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

p1.py:

__init__.py

import logging
import os
import sys
import azure.functions as func
import importlib.util

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    rss('p1')
    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:
        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
        )

def rss(snn):
    sp = os.path.join(os.path.dirname(__file__), f'{snn}.py')

    ss = importlib.util.spec_from_file_location(snn, sp)
    mm = importlib.util.module_from_spec(ss)
    sys.modules[ss.name] = mm
    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

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "URL": "Test-avatar"
  }
}

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

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

import logging
import os
import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    s= os.getenv("URL")
    print(s)
    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:
        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
        )

Output:

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

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

p1.py:

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

__init__.py

import logging
import os
import sys
import azure.functions as func
import importlib.util


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    rss('p1')
    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:
        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
        )
def rss(snn):
    sp = os.path.join(os.path.dirname(__file__), f'{snn}.py')

    ss = importlib.util.spec_from_file_location(snn, sp)
    mm = importlib.util.module_from_spec(ss)
    sys.modules[ss.name] = mm
    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:

确定