Azure Functions 使用 Python 运行时如何评估本地环境变量?

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

How are local environment variables evaluated by Azure Functions with Python runtime?

问题

...会在本地运行 Azure 函数时,从 local.settings.json 文件中提取“环境”变量。这类似于使用 .env 文件。

我知道在部署到 Azure 后,环境变量是从函数应用的“应用设置”中提取的。

问题

  • 当在本地运行函数时,如果我在终端设置了一个环境变量(例如:set DEBUG=true),而且这个变量也包含在 local.settings.json 文件中(例如:"DEBUG": false),函数代码如何知道要提取哪个环境变量?
英文:

I understand that...

import os

foo = os.getenv('ENV_VAR_NAME')

... will pull "environment" variables from the local.settings.json file when running an Azure Function locally. This is similar to using a .env file.

I know that when deployed to Azure, environment variables are pulled from the Function App's App Settings.

Question:

  • When running the Function locally, if I have an environment variable set using my terminal (Ex: set DEBUG=true), and this variable is also included in the local.settings.json file (Ex: "DEBUG": false), how does the Function code know which env var to pull in?

答案1

得分: 2

在 Azure 函数运行时环境中,它使用 os 模块和 os.environ[""]os.getenv("") 方法来评估本地环境变量,而在普通的 Python 项目中也是一样。

但在 Python 项目中,Python 运行时环境使用 python-dotenv 库来加载 .env 文件中的环境变量。

如果在 .envlocal.settings.json 文件中都定义了环境变量,在 Azure 函数环境中,Python 更倾向于从 local.settings.json 文件加载:

Azure Functions 使用 Python 运行时如何评估本地环境变量?

英文:

AFAIK,

In Azure Functions Runtime Context, it evaluates the local environment variables by using the os module and the method os.environ[""] or os.getenv("") and same in the Normal Python Projects.

But in the python projects, python runtime environment evaluates the .env file using the library python-dotenv for loading the environment variables.

If we define the environment variables in both the files .env and local.settings.json, Python prefers loading from the file local.settings.jsonin Azure Functions Context:

Azure Functions 使用 Python 运行时如何评估本地环境变量?

huangapple
  • 本文由 发表于 2023年2月18日 03:58:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488741.html
匿名

发表评论

匿名网友

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

确定