[英]How to use variables from an environment file Python?
I have a project that I'm working on in which I need to store sensitive information into an environment file as variables that can later be called in my code.我有一个正在处理的项目,我需要将敏感信息作为变量存储到环境文件中,以便稍后在我的代码中调用。 I'm having issues with it working and so I've dumbed it down to the simplest test I can think of.
我在使用它时遇到问题,所以我把它简化为我能想到的最简单的测试。
I have create a test.py file and a var.env file within the same directory.我在同一目录中创建了一个 test.py 文件和一个 var.env 文件。 They are the only files in this directory.
它们是此目录中的唯一文件。
Here is my test.py that simply tried to print the value这是我的 test.py,它只是试图打印值
#test.py
import os
from dotenv import load_dotenv
print(os.getenv('PROJECT'))
Here is environment file saved as var.env这是保存为 var.env 的环境文件
#.env test file
PROJECT='newproject1234'
When I run test.py I get a response of "none".当我运行 test.py 时,我得到的响应是“无”。 I know I've gotta be missing something simple here.
我知道我必须在这里遗漏一些简单的东西。 Any help is appreciated.
任何帮助表示赞赏。
You need to call load_dotenv
first.您需要先调用
load_dotenv
。
#test.py
import os
from dotenv import load_dotenv
load_dotenv('var.env')
print(os.getenv('PROJECT'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.