Import and execfile()

Mitko Haralanov mitko at qlogic.com
Fri Jan 11 18:54:16 EST 2008


On Fri, 11 Jan 2008 14:05:11 -0800 (PST)
George Sakkis <george.sakkis at gmail.com> wrote:

> # 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 ?

This might be overly simplistic but you could have a load_config
function which takes the path to the config file and the variable where
to load the config as arguments.

In the load_config function, you could get the directory part of the
config file path, appending it to sys.path, load the config, and then
remove the newly added directory from sys.path.

-- 
Mitko Haralanov



More information about the Python-list mailing list