Azure ML Studio中是否可以导出环境变量

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

Can environment variables be exported in Azure ML Studio

问题

我在Azure ML Studio中工作。我已经创建了一个Jupyter笔记本。我在笔记本中运行了Python代码。我想向代码提供一个值。与其将值硬编码,我想将其作为环境变量提供。我该如何做?

例如,代替:

API_KEY = "somevalue"

我想这样做:

API_KEY = os.getenv("AZURE_OPENAI_API_KEY")
英文:

I am working in Azure ML Studio. I have created a jupyter notebook. I am running a python code in the notebook. I want to provide a value to the code. Rather than hard-coding the value, I want to provide it as environment variable. How could I do this?

Eg., instead of

API_KEY = "somevalue"

I want to do

API_KEY = os.getenv("AZURE_OPENAI_API_KEY") 

答案1

得分: 1

你可以使用名为python-dotenv的Python包(python-dotenv),它可以加载.env文件中的变量,并且你可以像使用os.getenv()获取环境变量一样使用它们。

按照以下步骤进行操作:

首先,创建一个.env文件并像下面这样添加你的环境值。

接下来,将这个文件上传到你的笔记本工作空间中。
默认情况下,以点开头的文件名是隐藏的。

然后安装python-dotenv库。

%pip install python-dotenv

通过执行以下代码加载环境变量。

from dotenv import load_dotenv
load_dotenv()

现在,使用os.getenv()函数获取变量,如下所示。

import  os
print(os.getenv("YOUR_APT_KEY"),os.getenv("YOUR_HOST"))

在这里,你可以看到环境变量。

英文:

You can use the python package called python-dotenv(python-dotenv), which loads the variables from .env files and you use those as you get environment variables like os.getenv().

Follow below steps,

First, create a .env file and add your environment values like below.

Azure ML Studio中是否可以导出环境变量

Next, upload this file into your notebook workspace.
By default, the filename starts with .(dot) are hidden.

Azure ML Studio中是否可以导出环境变量
Then install the python-dotenv library.

%pip install python-dotenv

Load the environment variables by executing below code.

from dotenv import load_dotenv
load_dotenv()

Now, get the variables using os.getenv() function as below.

import  os
print(os.getenv("YOUR_APT_KEY"),os.getenv("YOUR_HOST"))

Azure ML Studio中是否可以导出环境变量

Here, you can see the environment variables.

答案2

得分: 0

我发现 Azure ML Studio 工作空间也有一个 shell。它运行 bash。我可以在那里导出变量并在笔记本中使用它们。

英文:

I discovered that Azure ML Studio workspace also has a shell. It runs bash. I could export the variables there and use them in notebook.

Azure ML Studio中是否可以导出环境变量

huangapple
  • 本文由 发表于 2023年4月11日 05:55:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981029.html
匿名

发表评论

匿名网友

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

确定