Import and execfile()

George Sakkis george.sakkis at gmail.com
Fri Jan 11 17:05:11 EST 2008


I maintain a few configuration files in Python syntax (mainly nested
dicts of ints and strings) and use execfile() to read them back to
Python. This has been working great; it combines the convenience of
pickle with the readability of Python. So far each configuration is
contained in a single standalone file; different configurations are
completely separate files.

Now I'd like to factor out the commonalities of the different
configurations in a master config and specify only the necessary
modifications and additions in each concrete config file. I tried the
simplest thing that could possibly work:

======================
# some_config.py

# master_config.py is in the same directory as some_config.py
from master_config import *

# override non-default options
foo['bar']['baz] = 1
...

======================
# trying to set the configuration:
CFG = {}
execfile('path/to/some_config.py', CFG)

Traceback (most recent call last):
...
ImportError: No module named master_config


I understand why this fails but I'm not sure how to tell execfile() to
set the path accordingly. Any ideas ?

George



More information about the Python-list mailing list