[Fixed]-Python: 'import *' vs execfile


Using execfile function will result in the evaluation of the Python source file (.py) every time the settings file is evaluated. You are executing the Python parser each time. Using import wouldn’t necessarily do this (might use the .pyc file). Generally the first time you run a project in Python (at least, cPython) it gets compiled to bytecode and not recompiled again. You are breaking that. This isn’t necessarily a problem, but you should be aware of it.

Using execfile will also result in all of the imports you may have in the settings_local.py file being re-evaluated in the module scope of the settings.py file. Using import * would have included all items in the settings_local.py module scope. The net effect is the same (all items included in settings_local.py module scope are included in settings.py) but the method is different.

Finally, it’s normal for modules to be executed as modules rather than included. It is reasonable for code to include things such as os.path.dirname(__file__). If any code did use this, you would confuse it as the code would no longer be executing in the module that the author might reasonably have expected.

In my experience, people use import not execfile. Django is very much ‘convention over configuration’. Follow the convention.



Another difference: execfile gets a context dictionary; the global context by default or
a specified dictionary. This could allow some strange things


# Probably not a good thing to do
z=x+1  # an expression that involves an un-defined field


from dont_do_this import *



execfile( 'dont_do_this.py', d )

is OK and results in d=={'x':1, 'z':2}

Note that

execfile( 'dont_do_this.py' )

is OK and results in the variable z being added to the globals.



The first version (from settings_local import *) is what everyone would expect to see. It will also let code analyzers find the module.

Leave a comment