By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,220 Members | 1,711 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,220 IT Pros & Developers. It's quick & easy.

Module executed twice when imported!

P: n/a
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.
Jun 28 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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

Jun 28 '06 #2

P: n/a
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.
Jun 28 '06 #3

P: n/a
Bump

In article <mi***************************@europe.isp.giganews .com>,
Michael Abbott <mi*****@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.
Jun 30 '06 #4

P: n/a
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?
Jun 30 '06 #5

P: n/a
In article <05******************@news.tufts.edu>,
John Salerno <jo******@NOSPAMgmail.com> 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.
Jun 30 '06 #6

P: n/a
Michael Abbott wrote:
In article <mi***************************@europe.isp.giganews .com>,
Michael Abbott <mi*****@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/...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
Jun 30 '06 #7

P: n/a
Michael Abbott wrote:
In article <05******************@news.tufts.edu>,
John Salerno <jo******@NOSPAMgmail.com> 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
Jun 30 '06 #8

P: n/a
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
Jun 30 '06 #9

P: n/a
In article <e8**********@news.albasani.net>,
Georg Brandl <g.*************@gmx.net> 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.
Jun 30 '06 #10

P: n/a
Michael Abbott wrote:
In article <e8**********@news.albasani.net>,
Georg Brandl <g.*************@gmx.net> 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.


You're welcome. This is indeed not easy to look through.

Georg
Jul 1 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.