我正在尝试使用 python 更新 .env 环境变量。和os.environ
我可以查看和更改本地环境变量,但我想更改 .env 文件。使用python-dotenv
我可以将 .env 条目加载到本地环境变量中
.env 文件
key=value
test.py
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv())
print(os.environ['key']) # outputs 'value'
os.environ['key'] = "newvalue"
print(os.environ['key']) # outputs 'newvalue'
.env 文件
key=value
.env 文件没有改变!仅更改本地环境变量。我找不到任何有关如何更新 .env 文件的文档。有谁知道解决方案吗?
Use dotenv.set_key https://saurabh-kumar.com/python-dotenv/reference/#dotenv.set_key.
import dotenv
dotenv_file = dotenv.find_dotenv()
dotenv.load_dotenv(dotenv_file)
print(os.environ["key"]) # outputs "value"
os.environ["key"] = "newvalue"
print(os.environ['key']) # outputs 'newvalue'
# Write changes to .env file.
dotenv.set_key(dotenv_file, "key", os.environ["key"])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)