Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Module executed twice when imported!

Reply
Thread Tools

Module executed twice when imported!

 
 
Michael Abbott
Guest
Posts: n/a
 
      06-28-2006
It seems to be an invariant of Python (insofar as Python has invariants)
that a module is executed at most once in a Python session. I have a
rather bizzare example that breaks this invariant: can anyone enlighten
me as to what is going on?

--- 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
$

Hmm. If the value __name__ is omitted from the dictionary, or if its
value doesn't match at least 'subtest.' then the message is printed only
once (for example, passing dict(__name__='subtest') produces one
"imported" message).

This happens with both python 2.4 and 2.3. I don't get it.
 
Reply With Quote
 
 
 
 
Simon Forman
Guest
Posts: n/a
 
      06-28-2006
Michael Abbott wrote:
> It seems to be an invariant of Python (insofar as Python has invariants)
> that a module is executed at most once in a Python session. I have a
> rather bizzare example that breaks this invariant: can anyone enlighten
> me as to what is going on?
>
> --- 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
> $
>
> Hmm. If the value __name__ is omitted from the dictionary, or if its
> value doesn't match at least 'subtest.' then the message is printed only
> once (for example, passing dict(__name__='subtest') produces one
> "imported" message).
>
> This happens with both python 2.4 and 2.3. I don't get it.


I don't get it either but there may be a clue in the docs for the
execfile() function
(http://docs.python.org/lib/built-in-funcs.html#l2h-24)

"It is different from the import statement in that it does not use the
module administration --"

I don't know why the globals dict you're passing in would change the
behavior though. Hopefully someone else can enlighten us both.

Peace,
~Simon

 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      06-29-2006
Michael Abbott a écrit :
> It seems to be an invariant of Python (insofar as Python has invariants)
> that a module is executed at most once in a Python session. I have a
> rather bizzare example that breaks this invariant: can anyone enlighten
> me as to what is going on?
>
> --- 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
> $
>
> Hmm. If the value __name__ is omitted from the dictionary, or if its
> value doesn't match at least 'subtest.' then the message is printed only
> once (for example, passing dict(__name__='subtest') produces one
> "imported" message).


During module evaluation, __name__ is usually set to the name of the
module *object*, not the name of the *file*. I suspect that it has
something to do with your observation.
 
Reply With Quote
 
Michael Abbott
Guest
Posts: n/a
 
      06-30-2006
Bump

In article <(E-Mail Removed)>,
Michael Abbott <(E-Mail Removed)> 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.
 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      06-30-2006
Simon Forman wrote:

> I don't get it either but there may be a clue in the docs for the
> execfile() function
> (http://docs.python.org/lib/built-in-funcs.html#l2h-24)
>
> "It is different from the import statement in that it does not use the
> module administration --"


Just after the above statement, it also says:

"it reads the file unconditionally and does not create a new module."

so perhaps that means that it isn't really being imported the second
time, just that the contents are being executed?
 
Reply With Quote
 
Michael Abbott
Guest
Posts: n/a
 
      06-30-2006
In article <05epg.2385$(E-Mail Removed)>,
John Salerno <(E-Mail Removed)> wrote:

> > (http://docs.python.org/lib/built-in-funcs.html#l2h-24)
> > "It is different from the import statement in that it does not use the
> > module administration --"

>
> Just after the above statement, it also says:
>
> "it reads the file unconditionally and does not create a new module."
>
> so perhaps that means that it isn't really being imported the second
> time, just that the contents are being executed?


Interesting thought.

I'm not convinced, though: firstly, I read that statement as describing
what happens to the file named in the execfile() statement; and
secondly, the problem *only* happens if the global dictionary passed to
execfile() has a '__name__' and if the value of that key is sufficiently
close to the name of the file being passed to execfile().

I found that passing __name__='whatever' resulted in normal import
behaviour, and so does __name__='subtest', but curiously enough passing
__name__='subtest.' results in the double import.
 
Reply With Quote
 
Peter Maas
Guest
Posts: n/a
 
      06-30-2006
Michael Abbott wrote:
> In article <(E-Mail Removed)>,
> Michael Abbott <(E-Mail Removed)> 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/...n-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
 
Reply With Quote
 
Georg Brandl
Guest
Posts: n/a
 
      06-30-2006
Michael Abbott wrote:
> In article <05epg.2385$(E-Mail Removed)>,
> John Salerno <(E-Mail Removed)> wrote:
>
>> > (http://docs.python.org/lib/built-in-funcs.html#l2h-24)
>> > "It is different from the import statement in that it does not use the
>> > module administration --"

>>
>> Just after the above statement, it also says:
>>
>> "it reads the file unconditionally and does not create a new module."
>>
>> so perhaps that means that it isn't really being imported the second
>> time, just that the contents are being executed?

>
> Interesting thought.
>
> I'm not convinced, though: firstly, I read that statement as describing
> what happens to the file named in the execfile() statement; and
> secondly, the problem *only* happens if the global dictionary passed to
> execfile() has a '__name__' and if the value of that key is sufficiently
> close to the name of the file being passed to execfile().
>
> I found that passing __name__='whatever' resulted in normal import
> behaviour, and so does __name__='subtest', but curiously enough passing
> __name__='subtest.' results in the double import.


That's because __name__ is normally set to the module's name in the package
hierarchy. When you set it to "some1.some2", Python thinks it's
in a subpackage and when it does "import imptest" Python searches for
the module "some1.imptest", not for "imptest", and finds it in the
current directory (which it assumes to be the package directory).

You can verify this by inspecting sys.modules after the execfile() call.

So it's not a bug, but expected behavior.

Georg
 
Reply With Quote
 
Georg Brandl
Guest
Posts: n/a
 
      06-30-2006
Peter Maas wrote:

> The docs tell us
> (http://www.python.org/doc/2.4.2/lib/...n-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.


This has nothing directly to do with the "problem" since the seemingly
double imported module is not the one that's execfile'd.

Georg
 
Reply With Quote
 
Michael Abbott
Guest
Posts: n/a
 
      06-30-2006
In article <e844h9$qkt$(E-Mail Removed)>,
Georg Brandl <(E-Mail Removed)> wrote:
> That's because __name__ is normally set to the module's name in the package
> hierarchy. When you set it to "some1.some2", Python thinks it's
> in a subpackage


Ahhhh.

So what I *should* have set it to is the module name *without*
extension, i.e. __name__='imptest'

Thank you. Now I think I understand it.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: code in a module is executed twice (cyclic import problems) ? Dave Angel Python 2 10-12-2009 12:03 PM
code in a module is executed twice (cyclic import problems) ? Stef Mientki Python 1 10-11-2009 09:35 AM
Page on a frame executed twice (on some clients) Alfons Puig ASP .Net 0 01-05-2005 01:02 PM
page_load is executed twice when using validation controls =?Utf-8?B?TWFnZXNo?= ASP .Net 0 08-25-2004 11:39 AM
Event executed twice Alberto ASP .Net 1 01-12-2004 04:51 PM



Advertisments