0

我有这样的文件布局:
settings/
----__init__.py
----common.py
----configs/
--------constants1.py
--------constants2.py
----debug/
--------include1&2.py
--------include1.py
--------include2.py

当我导入时settings.debug.include1,我希望设置文件执行/导入,然后用适当的常量文件common.py覆盖设置。common.py问题是,这没有发生。有没有办法以这种方式实现我的目标?

4

2 回答 2

2

否。使用from ... import *或加载适当的文件execfile()settings/__init__.py

于 2012-06-26T07:21:28.200 回答
0

由于大多数评论已经暗示您可以from module import *在适当的文件中使用。它们可能如下所示..

--

# settings/common.py

DEBUG = False

--

# settings/configs/constants1.py

CONSTANT_1 = 'One'

--

# settings/configs/constants2.py

CONSTANT_2 = 'Two'

--

# settings/debug/include1.py

from settings.common import *
from settings.configs.constants1 import *

# Override settings here
DEBUG = True
CONSTANT_1 = '1'

--

# settings/debug/include2.py

from settings.common import *
from settings.configs.constants2 import *

# Override settings here
DEBUG = True
CONSTANT_2 = '2'

并且对于 2 调试的组合包括

# settings/debug/include1and2.py

from settings.debug.include1 import *
from settings.debug.include2 import *

或者

# settings/debug/include1and2.py

from settings.common import *
from settings.configs.constants1 import *
from settings.configs.constants2 import *

# Override settings here
于 2012-06-26T12:38:10.060 回答