我正在尝试用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文件的文档。有人知道解决方案吗?
发布于 2020-09-11 04:45:22
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"])
https://stackoverflow.com/questions/63837315
复制相似问题