简体   繁体   English

在Python2.7中读取配置文件

[英]Reading Config File in Python2.7

I have a file named .env in my code directory .env has different configs 我的代码目录中有一个名为.env的文件。.env具有不同的配置

Eg : username = "xyz"
     password = "abcd"

In my code directory I have a folder called scripts 在我的代码目录中,有一个名为scripts的文件夹

Inside scripts I have a script named "control.py" which needs to read the config from .env 在脚本内部,我有一个名为“ control.py”的脚本,需要从.env读取配置

How can I achieve this? 我该如何实现?

The structure is something as below 结构如下

/var/www/html/code/.env
/var/www/html/code/scripts/control.py

.env has all the config stuffs and control.py needs to read these config. .env具有所有配置资料,control.py需要读取这些配置。

It would be better if you use decouple module for this purpose. 如果为此目的使用解耦模块会更好。 You can do pip install python-decouple. 您可以执行pip install python-decouple。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM