473,761 Members | 8,372 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Module executed twice when imported!

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('subte st.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
10 1896
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('subte st.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
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('subte st.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
Bump

In article <mi************ *************** @europe.isp.gig anews.com>,
Michael Abbott <mi*****@aranei dae.co.uk.inval id> wrote:
--- test.py ---
import imptest
execfile('subte st.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
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
In article <05************ ******@news.tuf ts.edu>,
John Salerno <jo******@NOSPA Mgmail.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__='whate ver' resulted in normal import
behaviour, and so does __name__='subte st', but curiously enough passing
__name__='subte st.' results in the double import.
Jun 30 '06 #6
Michael Abbott wrote:
In article <mi************ *************** @europe.isp.gig anews.com>,
Michael Abbott <mi*****@aranei dae.co.uk.inval id> wrote:
--- test.py ---
import imptest
execfile('subte st.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(filena me[, 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
Michael Abbott wrote:
In article <05************ ******@news.tuf ts.edu>,
John Salerno <jo******@NOSPA Mgmail.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__='whate ver' resulted in normal import
behaviour, and so does __name__='subte st', but curiously enough passing
__name__='subte st.' 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.some 2", 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
Peter Maas wrote:
The docs tell us
(http://www.python.org/doc/2.4.2/lib/...n-funcs.html):

------------------------- begin -------------------------------
execfile(filena me[, 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
In article <e8**********@n ews.albasani.ne t>,
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.some 2", 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__='impte st'

Thank you. Now I think I understand it.
Jun 30 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1564
by: Berteun Damman | last post by:
Hello, First I was trying to get PyOSD, but as soon as I did `import pyosd' Python received a SIGABRT. Then I wrote my own module, which looks like: #include <Python.h> static PyMethodDef testmod_methods = { {NULL, NULL} }; void initmymod(void) { Py_InitModule("mymod", testmod_methods);
1
1249
by: Alberto | last post by:
I have a linkButton with a method associated to the click event. The problem is that the code is executed twice. The causes validation property is set to false. What's happening? Thank you.
0
1129
by: Magesh | last post by:
Hi, I have a search screen with a textbox and a button (server controls). The page has to be submitted when the user hits the enter key in the textbox. The code i am using to do this is.. this.txtCorpId.Attributes = "if(event.keyCode == 13){ if (Page_ClientValidate()) document.getElementById('btnSearch').click();}"; But i find that the page_load event is executed twice when the enter key is
0
1017
by: Alfons Puig | last post by:
Hi, There is a page called GesDoc.aspx that is a container for two frames: Actions.aspx and Word.aspx. On some machines running IE, when loading the container, on some clients Word.aspx code behind is executed twice, producing a bad result. On the requests from clients that fail, I noticed: * Enabling trace on web.config file and looking at the trace.axd, on the second call the ASP.NET_SessionID is different. * Looking at the IIS logs...
8
4040
by: Marcel | last post by:
I have a problem with a PHP page that seems to get executed twice. I am running PHP5 ISAPI on 2003 server. The script is a PHP page with a form. When the form is submitted one record have to be inserted in the database but when i look at the database after submitting the form there are two(!) records inserted so it looks like the script is executed twice. Can this be a proxy-setting or an ISA`server related subject???
2
6692
by: Chris | last post by:
In SQL 2005 I have a stored procedure as below: @sub_no smallint OUTPUT BEGIN BEGIN TRANSACTION INSERT...INTO
3
5719
by: Ed Sonneveld | last post by:
Hi, I have hosted my webservice at a hosting company and it has been working fine for 2 years now. The webservice is called by winforms clients over the internet, using the proxy class generated by visual studio. What I do not understand, however, is that sometimes (say 1% of all calls) are being executed twice on the server side. I notice that because each call makes an entry in the database. This problem has been around as long as...
0
1086
by: Ed Sonneveld | last post by:
Hi, I have hosted my webservice at a hosting company and it has been working fine for 2 years now. The webservice is called by winforms clients over the internet, using the proxy class generated by visual studio. What I do not understand, however, is that sometimes (say 1% of all calls) are being executed twice on the server side. I notice that because each call makes an entry in the database. This problem has been around as long as...
3
28237
by: Johny | last post by:
Is it possible to unload a module after I've imported it. I have the main program from which I import several smaller programs. Some of these uses timeoutsocket module. But one of the smaller program can not work with the timeoutsocket module, so I must unload the timeoutsocket module, execute the program and then reload timeoutsocket module. So, s it possible to unload a module after I've imported it.. Thanks L.
0
9531
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8780
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.