Python 命令行应用程序的应用程序上下文中的变量

Python 命令行应用程序的应用程序上下文中的变量,python,python-2.7,configuration-files,pytest,Python,Python 2.7,Configuration Files,Pytest,我正在构建一个用Python编写的命令行应用程序。我想使用配置文件的思想,其中每个配置文件都有一些不同的应用程序中使用的值 我创建了一个properties.ini文件,并创建了一个config\u parser.py文件,其中包含一个函数,该函数接收配置文件并将属性加载到一些变量中。然后我在每个需要使用它们的文件中导入它 我希望在执行应用程序时将概要文件作为参数传递,加载正确的属性并将它们放在整个应用程序的上下文中,避免到处都是import config\u parser 例如,在我的prop

我正在构建一个用Python编写的命令行应用程序。我想使用配置文件的思想,其中每个配置文件都有一些不同的应用程序中使用的值

我创建了一个
properties.ini
文件,并创建了一个
config\u parser.py
文件,其中包含一个函数,该函数接收配置文件并将属性加载到一些变量中。然后我在每个需要使用它们的文件中导入它

我希望在执行应用程序时将概要文件作为参数传递,加载正确的属性并将它们放在整个应用程序的上下文中,避免到处都是
import config\u parser

例如,在我的
properties.ini
中,我有:

[DEFAULT]
 GLOBAL_EMAIL=example@email.com

[e2e_testing]
 GLOBAL_EMAIL=another_email@email.com
import configparser

config = configparser.ConfigParser()
config.read('properties.ini')

GLOBAL_EMAIL = ''

def load_config(profile):
    global GLOBAL_EMAIL

    GLOBAL_EMAIL = config[profile]['GLOBAL_EMAIL']
config_parser.py
中,我有:

[DEFAULT]
 GLOBAL_EMAIL=example@email.com

[e2e_testing]
 GLOBAL_EMAIL=another_email@email.com
import configparser

config = configparser.ConfigParser()
config.read('properties.ini')

GLOBAL_EMAIL = ''

def load_config(profile):
    global GLOBAL_EMAIL

    GLOBAL_EMAIL = config[profile]['GLOBAL_EMAIL']
对于每个脚本,我需要使用我所做的属性:

from application import config_parser as CFG

def method(profile):
    CFG.load_config(profile)
    print CFG.GLOBAL_EMAIL
理想情况下,我希望在执行
main.py
(这是我的应用程序的入口点)期间加载此配置,然后在该包中共享accross模块,因此我不需要到处导入
config\u解析器,也不需要传递
profile
变量

我读到了
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,但是我不确定如何使用Pytest进行单元测试,因为
\uuuuuu builtin\uuuuu
将只对源文件夹中的模块可用,我需要以某种方式模拟它


提前谢谢

您是否计划在一个流程(或一组流程)中运行一个配置文件。如果是,您可以使用环境变量:

# main.py
import os
def load_config():
    os.environ["GLOBAL_EMAIL"] = config[profile]['GLOBAL_EMAIL']

# module.py
def modfunc():
    email = os.environ["GLOBAL_EMAIL"]

为什么不在main.py中解析一次配置并将结果传递给所有模块?