Does __import__ require a module to have a .py suffix?

Jean-Paul Calderone exarkun at
Wed Mar 12 21:02:54 CET 2008

On Wed, 12 Mar 2008 12:58:33 -0700 (PDT), George Sakkis <george.sakkis at> wrote:
>On Mar 12, 12:22 pm, mrstephengross <mrstevegr... at> wrote:
>> Hi all. I've got a python file called 'foo' (no extension). I want to
>> be able to load it as a module, like so:
>>   m = __import__('foo')
>> However, the interpreter tells me "No module named foo". If I rename
>> it, I can indeed import it. Is the extension required? Is there
>> any way to override that requirement?
>You can use execfile:
>foo = {}
>execfile('foo', foo)
>Apart from the different syntax in accessing the module globals
>(attributes with __import__ (foo.x) vs dict entries with execfile
>(foo['x'])), there are probably more subtle differences but I can't
>tell for sure. It would be nice if someone more knowledgeable can
>compare and contrast these two appraches.

Another difference is that when you import a module, its code is (usually)
only executed once.  Each import after the first just returns a reference
to the already-created module object.  When you use execfile, the code is
re-evaluated each time.


More information about the Python-list mailing list