[SciPy-user] Newbie and Old matlab user

Fernando Perez Fernando.Perez at colorado.edu
Thu Oct 13 11:22:10 CDT 2005


Fernando Perez wrote:
> Alan G Isaac wrote:

>>In any case, maybe the OP could just use execfile:
>>http://docs.python.org/lib/built-in-funcs.html#execfile
> 
> 
> Yes, the only problem with this is that execfile requires a full path, so it's 
> a bit less convenient than import.  But while developing/debugging, it can do 
> the trick.

I should clarify that execfile('/path/to/foo.py') is not quite equivalent to 
'from foo import *'.  In particular:

  - if foo declares an __all__ list of names, the import form only imports 
those names, while execfile brute-forces everything in the foo namespace up 
into that of the caller.

  - by default, python does NOT import underscore-prefixed names with 
underscore prefixes:


In [1]: cat foo.py
top = 0
_one = 1
__two = 2

In [2]: from foo import *

In [3]: _one
---------------------------------------------------------------------------
exceptions.NameError                                 Traceback (most recent 
call last)

/home/fperez/<console>

NameError: name '_one' is not defined

In [4]: __two
---------------------------------------------------------------------------
exceptions.NameError                                 Traceback (most recent 
call last)

/home/fperez/<console>

NameError: name '__two' is not defined

In [5]: execfile('foo.py')

In [6]: _one
Out[6]: 1

In [7]: __two
Out[7]: 2



So yes, for quick and dirty work execfile can be substituted for 'import *', 
but it's important to realize that they are NOT the same.  Sorry if my 
previous post was imprecise.

cheers,

f



More information about the SciPy-user mailing list