Newbie: splitting dictionary definition across two .py files

Ben Cartwright bencvt at
Fri Mar 31 03:29:38 CEST 2006

kar1107 at wrote:
> I like to define a big dictionary in two
> files and use it my main file,
> I want the definition to go into and
> target_db = {}
> target_db['foo'] = 'bar'
> target_db['xyz'] = 'abc'
> In, I like to do
> from build_cfg_static import *
> from build_cfg import *
> use target_db to access all elements. The problem looks like, I
> can't
> have the definition of target_db split across two files. I think they
> reside in different name spaces?

Yes.  As it stands, will not compile to bytecode
(NameError: name 'target_db' is not defined).

Unless you're doing something ugly like exec() on the its contents, .py
files need to be valid before they can be imported.

> Is there any way I can have the same
> dictionary definition split across two files?

Try this:

target_db = {}
target_db['foo'] = 'bar'

target_db = {}
target_db['xyz'] = 'abc'

from build_cfg_static import target_db
from build_cfg import target_db as merge_db


More information about the Python-list mailing list