Import bug: Module executed twice when imported!

Peter Maas peter.maas at somewhere.com
Fri Jun 30 16:18:15 EDT 2006


Michael Abbott wrote:
> In article <michael-3B486D.22521428062006 at europe.isp.giganews.com>,
>  Michael Abbott <michael at araneidae.co.uk.invalid> wrote:
> 
>> --- test.py ---
>> import imptest
>> execfile('subtest.py', dict(__name__ = 'subtest.py'))
>> --- imptest.py ---
>> print 'Imptest imported'
>> --- subtest.py ---
>> import imptest
>> ---
>>
>>    $ python test.py
>>    Imptest imported
>>    Imptest imported
>>    $
> 
> I claim this as an unreported (and highly obscure) Python bug.

The docs tell us
(http://www.python.org/doc/2.4.2/lib/built-in-funcs.html):

------------------------- begin -------------------------------
execfile(filename[, globals[, locals]])

This function is similar to the exec statement, but parses a file
instead of a string. It is different from the import statement in that
it does not use the module administration -- it reads the file
unconditionally and does not create a new module.
------------------------- end ---------------------------------

I claim this as a well documented (and thus exspectable) Python behaviour.
execfile() just executes a file unconditionally without searching in
sys.modules. That's its purpose, otherwise it would be a synonym of
the import statement.

Peter Maas, Aachen



More information about the Python-list mailing list