英文:
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 thelocal.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
文件中的环境变量。
如果在 .env
和 local.settings.json
文件中都定义了环境变量,在 Azure 函数环境中,Python 更倾向于从 local.settings.json
文件加载:
英文:
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.json
in Azure Functions Context:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论