如何使用内置的 “os” 模块从 .env 文件中检索环境变量

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

How to retrieve environment variables from .env using built-in "os" module

问题

在我的.env文件中,如何获取我的密钥?

我的代码有什么问题(在macOS上)?dotenv可以正常工作,但是否可以只使用os模块?

.env文件中:

key = 123456

test.py文件中:

import os

actual_key = os.getenv('key')

print(actual_key)

最后控制台的输出是:

None
英文:

How can I get my key in my .env file?

What's the problem in my code (macOS)? The dotenv works fine, but is it possible to use only the os module?

In the .env:

key = 123456

and in the test.py:

import os

actual_key = os.getenv('key')

print(actual_key)

Finally the output in console was:

None

答案1

得分: 1

os.getenv('key') 寻找 环境变量 中的 'key'。默认情况下,.env 文件中的键值对不会加载到环境变量中。如果您想要以这种方式加载键,您首先必须将 .env 文件的内容添加到环境变量中。我建议使用 python-dotenv 库:

import os
from dotenv import load_dotenv

load_dotenv() # 将在本地文件夹中搜索 .env 文件并加载变量

KEY_VAR = os.getenv('KEY')
英文:

os.getenv('key') looks for 'key' in environmental variables. By default key-value pairs that are in .env file are not loaded into environment variables. If you want to load key this way you have to first add the contents of .env file to environment variables. I suggest using python-dotenv library:

import os
from dotenv import load_dotenv

load_dotenv() # will search for .env file in local folder and load variables 

KEY_VAR = os.getenv('KEY')

答案2

得分: 0

需要使用dotenv来从.env文件中加载环境变量,代码如下:

import os
from dotenv import load_dotenv

# 从 .env 文件中加载环境变量
load_dotenv()

actual_key = os.getenv('key')
print(actual_key)

可以使用pip install python-dotenv 来安装dotenv

英文:

You need to use dotenv to load the environment variables from a .env file, like so:

from dotenv import load_dotenv

# Load the environment variables from the .env file
load_dotenv()

actual_key = os.getenv('key')
print(actual_key)

dotenv can be installed with pip install python-dotenv

答案3

得分: 0

你无法做你想做的事情。os.getenv 无法直接从 .env 文件获取变量。
TLDR;

关于设置环境变量:

在 macOS 临时设置环境变量:

export [variable_name]=[variable_value]

要永久设置它:

  • 打开 ~/.bash-profile
  • 滚动到 .bash_profile 文件的末尾
  • 在末尾添加 export [variable_name]=[variable_value]
  • 保存对 .bash_profile 文件所做的任何更改
  • 通过重新启动终端窗口或使用 source ~/.bash-profile 来执行新的 .bash_profile

如果你想使用提到的 dotenv 库,你需要使用 .env 文件进行源化,并在运行使用 os.getenv 的 Python 脚本之前导出所有变量,参见此链接:https://stackoverflow.com/a/65694371/15923186

英文:

You cannot do what you want. The os.getenv cannot get variables straight from the .env file.
TLDR;

Regarding the setting of the env vars:

In macOS to set the env var temporarily:

export [variable_name]=[variable_value]

To do it permanently:

  • open ~/.bash-profile
  • scroll down to the end of the .bash_profile file
  • add export [variable_name]=[variable_value] at the end of it
  • save any changes you made to the .bash_profile file
  • execute the new .bash_profile by either restarting the terminal window or using source ~/.bash-profile

If you don't want to use the mentioned dotenv library you'll have to source the .env file first and export all the variables before running your python script which uses os.getenv, see this: https://stackoverflow.com/a/65694371/15923186

huangapple
  • 本文由 发表于 2023年3月7日 21:36:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662684.html
匿名

发表评论

匿名网友

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

确定