Python: execfile from another working directory?

I have code that loads a default configuration file, and then allows users to supply their own Python files as an additional optional configuration or overriding the default values:

# foo.py def load(cfg_path=None): # load default configuration exec(default_config) # load user-specific configuration if cfg_path: execfile(cfg_path) 

However, there is a problem: execfile() executes the directives in the file specified by cfg_path , as if it were in the working directory foo.py , and not in its own working directory. Thus, import directives can fail if the cfg_path file does, say, from m import x , where m is a module in the same directory as cfg_path .

How did I execfile() from the working directory of its argument or otherwise achieve the equivalent result? Also, I was told that the execfile deprecated in Python 3 and that I should use exec , so if there is a better way I should do this, I’m all ears.

Note. I do not think that the solutions that just modify the working directory are correct. As far as I can tell, these modules will not place these modules in the path to the interpreter module.

+7
python working-directory execfile
source share
1 answer

os.chdir allows you to change the working directory as you wish (you can extract the working directory cfg_path using os.path.dirname ); remember to get the current os.getcwd directory first if you want to restore it when you finish exec'ing cfg_path .

Python 3 really removes the execfile (in favor of the sequence in which you read the file, compile contents, and then exec them), but you don’t have to worry about this if you are currently coding in Python 2.6, since the source 2to3 source process all this is smooth and fluid.

Edit : The OP reports in a comment that execfile starts a separate process and does not respect the current working directory. This is not true, and here is an example showing that it is:

 import os def makeascript(where): f = open(where, 'w') f.write('import os\nprint "Dir in file:", os.getcwd()\n') f.close() def main(): where = '/tmp/bah.py' makeascript(where) execfile(where) os.chdir('/tmp') execfile(where) if __name__ == '__main__': main() 

Running this on my computer produces output, for example:

 Dir in file: /Users/aleax/stko Dir in file: /private/tmp 

clearly shows that execfile does continue to use the working directory installed at the time execfile executed. (If the executable changes the working directory that will be displayed after the execfile - precisely because everything happens in the same process!).

Thus, any problems that the OP is still observing are not tied to the current working directory (it is difficult to diagnose what they may actually be without seeing the code and accurate information about the problems observed;).

+7
source share

All Articles