why import, reload, import again?

Peter Hansen peter at engcorp.com
Tue Jan 3 23:49:19 CET 2006


beliavsky at aol.com wrote:
> Near the beginning of file test_matrix.py from scipy are the lines
> 
> import scipy.base
> reload(scipy.base)
> from scipy.base import *
> del sys.path[0]
> 
> Could someone please explain why the first two lines were included? A
> similar script I wrote works fine without them. 

It would very likely be solely for testing purposes, and only because 
test_matrix.py could be run (after importing it, as opposed to via 
os.system) from another script which also runs other tests.

Doing the above has the effect of ensuring that scipy.base is reloaded 
prior to the "from scipy.base import *" line.  Without the first two, 
the test_matrix.py script could be getting various globals from 
scipy.base which were modified during a previous test.  (This smells a 
bit... tests shouldn't be polluting scipy.base, and scipy.base probably 
shouldn't be doing anything magic that requires that reload in the first 
place, but there may be good reasons for it.)

(I recognize that "idiom" from having had to do it at the interactive 
prompt with a module where I was doing "from xxx import *".)

 > Also, what is the
> purpose of the "del" line? (I understand the mechanics of what "del"
> does.) The scipy developers are skilled Python programmers, so I am
> trying to understand the idioms used in their codes.

I can only guess it's something similar... actually, I won't guess here 
as the only guesses I can think of would invalidate my theory above. :-)

-Peter




More information about the Python-list mailing list