简体   繁体   English

在python中访问全局模块

[英]Accessing a global module in python

I apologize if this is a basic question but I can't seem to find the answer here or on Google. 抱歉,这是一个基本问题,但似乎无法在此处或在Google上找到答案。 Basically I'm trying to create a single config module that would be available to all other modules imported in a python application. 基本上,我试图创建一个配置模块,该模块可用于python应用程序中导入的所有其他模块。 Of course it works if I have import config in each file but I would like to make my config dynamic based on the environment the application is running in and I'd prefer not to have to copy the logic into every file. 当然,如果我在每个文件中都import config ,它就可以工作,但是我想根据应用程序所运行的环境使配置动态化,因此我不想将逻辑复制到每个文件中。

Here's an example: 这是一个例子:

app.py: app.py:

import config
import submodule

# do other stuff

submodule.py: submodule.py:

print config.some_config_variable

But python of course complains that config isn't defined. 但是python当然会抱怨config没有定义。

I did find some stuff about global variables but that didn't seem to work either. 我确实找到了一些有关全局变量的东西,但这似乎也不起作用。 Here's what I tried: 这是我尝试过的:

Edit I changed this to show that I'd like the actual config being imported to be dynamic. 编辑我更改了此设置以表明我希望导入的实际配置是动态的。 However I do currently have a static config modle for my tests just to figure out how to import globally and then worry about that logic 但是我目前确实有一个用于测试的静态配置模块,以弄清楚如何全局导入,然后担心这种逻辑

app.py app.py

# logic here that defines some_dynamic_config
global config
config = __import__(some_dynamic_config)
import submodule

submodule.py submodule.py

print config.some_config_variable

But config still isn't defined. 但是配置仍未定义。

I'm aware that I could create a single config.py and place logic to set the variables but I dislike that. 我知道我可以创建一个config.py并放置逻辑来设置变量,但是我不喜欢这样。 I prefer the config file to just configuration and not contain a bunch of logic. 我更喜欢配置文件,而不是仅包含配置逻辑。

You've got to put your logic somewhere. 您必须将自己的逻辑放在某处。 Your config.py could be a module that determines which config files to load, something like this: 您的config.py可能是确定要加载哪些配置文件的模块,如下所示:

#config.py

import sys

from common_config import *

if sys.platform == 'darwin':
    from mac_config import *
elif sys.platform == 'win32':
    from win32_config import *
else:
    from linux_config import *

With this approach, you can put common settings in common_settings.py, and platform-specific settings in their respective files. 使用这种方法,您可以将常用设置放入common_settings.py中,并将平台特定的设置放在各自的文件中。 Since the platform-specific settings are imported after common_settings, you can also override anything in common_settings by defining it again in the platform-specific files. 由于特定于平台的设置是在common_settings之后导入的,因此您还可以通过在特定于平台的文件中再次定义它们来覆盖common_settings中的任何内容。

This is a common pattern used in Django settings files, and works quite well there. 这是Django设置文件中使用的一种常见模式,在该模式下效果很好。

You could also wrap each import call with try... except ImportError: blocks if need be. 您也可以使用try... except ImportError:包装每个import调用, try... except ImportError:块,如果需要的话。

Modules are shared, so each module can import config without issue. 模块是共享的,因此每个模块都可以import config而不会出现问题。

config.py itself can have logic to set it's global variables howver you like. config.py本身可以根据需要随意设置逻辑来设置全局变量。 As an example: 举个例子:

config.py: config.py:

import sys
if sys.platform == "win32":
    temp = "c:\\temp"
else:
    temp = "/tmp"

Now import config in any module to use it: 现在将config导入任何模块以使用它:

import config
print "Using tmp dir: %s" % config.temp

If you have a module that you know will be initialized before anything else, you can create an empty config.py, and then set it externally: 如果您知道某个模块将首先进行初始化,则可以创建一个空的config.py,然后在外部进行设置:

import config
config.temp = "c:\\temp"

But you'll need to run this code before anything else that uses it. 但是,您需要先运行此代码,然后再使用它。 The empty module can be used as a singleton. 空模块可以用作单例。

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

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