472,989 Members | 3,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

import mysteries


I'm pretty comfortable with Python, but recently I'm constantly
finding mysterious issues with import. For example, looking at

http://genshi.edgewall.org/browser/t...s/transform.py

the examples use the symbol 'HTML' but it's not defined locally, it's
not explicitly imported, and there's no import *. Yet doctest will
test this module and it passes with flying colors. It turns out HTML
is defined in genshi.input. How do I know that? I grepped for it.
How does it become available to this module?

Another example: I was recently working on some code that did an
import from inside a class method. That import was failing. I moved
the import to the top of the file (at module scope) and it succeeded.
I'm fairly sure that nobody was monkeying around with sys.path in that
case. Can anyone think of a likely explanation?

TIA,

--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

The Astoria Seminar ==http://www.astoriaseminar.com

Jun 21 '07 #1
11 1279
David Abrahams schrieb:
I'm pretty comfortable with Python, but recently I'm constantly
finding mysterious issues with import. For example, looking at

http://genshi.edgewall.org/browser/t...s/transform.py

the examples use the symbol 'HTML' but it's not defined locally, it's
not explicitly imported, and there's no import *. Yet doctest will
test this module and it passes with flying colors.
It doesn't pass for me:

pydoctest.testmod(genshi.filters.transform)
************************************************** ********************
File "/usr/lib/python2.4/site-packages/genshi/filters/transform.py",
line 29, in genshi.filters.transform
Failed example:
html = HTML('''<html>
<head><title>Some Title</title></head>
<body>
Some <em>body</emtext.
</body>
</html>''')
Exception raised:
Traceback (most recent call last):
File "doctest.py", line 1248, in __run
compileflags, 1) in test.globs
File "<doctest genshi.filters.transform[1]>", line 1, in ?
html = HTML('''<html>
NameError: name 'HTML' is not defined

Regards,
Martin
Jun 21 '07 #2
David Abrahams wrote:
I'm pretty comfortable with Python, but recently I'm constantly
finding mysterious issues with import. For example, looking at

http://genshi.edgewall.org/browser/t...s/transform.py

the examples use the symbol 'HTML' but it's not defined locally, it's
not explicitly imported, and there's no import *. Yet doctest will
test this module and it passes with flying colors. It turns out HTML
is defined in genshi.input. How do I know that? I grepped for it.
How does it become available to this module?
Explicitly passed, see

http://genshi.edgewall.org/browser/t...s/transform.py
Another example: I was recently working on some code that did an
import from inside a class method. That import was failing. I moved
the import to the top of the file (at module scope) and it succeeded.
I'm fairly sure that nobody was monkeying around with sys.path in that
case. Can anyone think of a likely explanation?
Too vague, sorry.

Peter
Jun 21 '07 #3
David Abrahams <da**@boost-consulting.comwrites:
I'm pretty comfortable with Python, but recently I'm constantly
finding mysterious issues with import. For example, looking at

http://genshi.edgewall.org/browser/t...s/transform.py

the examples use the symbol 'HTML' but it's not defined locally, it's
not explicitly imported, and there's no import *. Yet doctest will
test this module and it passes with flying colors. It turns out HTML
is defined in genshi.input. How do I know that? I grepped for it.
How does it become available to this module?
That's a mystery to me too. I can't see by looking at the module where
this 'HTML' name comes from, and as you say there is no import of
'genshi.input'.

Whatever the explanation, it's a violation of one of the strengths of
Python: namespaces. Names that appear in the current namespace (as
happens with 'from foo import *', and as seems to be happening here)
are bad programming style, for exactly the reason that they make the
code more difficult to understand.

--
\ "bash awk grep perl sed, df du, du-du du-du, vi troff su fsck |
`\ rm * halt LART LART LART!" -- The Swedish BOFH, |
_o__) alt.sysadmin.recovery |
Ben Finney
Jun 22 '07 #4
On Thu, 21 Jun 2007 16:03:42 -0400, David Abrahams wrote:
I'm pretty comfortable with Python, but recently I'm constantly finding
mysterious issues with import. For example, looking at

http://genshi.edgewall.org/browser/t...s/transform.py

the examples use the symbol 'HTML' but it's not defined locally, it's
not explicitly imported, and there's no import *. Yet doctest will test
this module and it passes with flying colors. It turns out HTML is
defined in genshi.input. How do I know that? I grepped for it. How
does it become available to this module?
There are ways to bypass the import system. The most obvious would be to
write directly to globals.
>>spanish_inquisition
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'spanish_inquisition' is not defined
>>globals()['spanish_inquisition'] = "NOBODY expects the Spanish
Inquisition!!!"
>>spanish_inquisition
'NOBODY expects the Spanish Inquisition!!!'
Another example: I was recently working on some code that did an import
from inside a class method. That import was failing. I moved the
import to the top of the file (at module scope) and it succeeded. I'm
fairly sure that nobody was monkeying around with sys.path in that case.
Can anyone think of a likely explanation?
If it was a "from MODULE import *" then it will not work if it is nested
in a function or class. That's by design.

--
Steven.
Jun 22 '07 #5

on Fri Jun 22 2007, "Steven D'Aprano" <steven-AT-REMOVE.THIS.cybersource.com.auwrote:
There are ways to bypass the import system. The most obvious would be to
write directly to globals.
>>>spanish_inquisition
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'spanish_inquisition' is not defined
>>>globals()['spanish_inquisition'] = "NOBODY expects the Spanish
Inquisition!!!"
>>>spanish_inquisition
'NOBODY expects the Spanish Inquisition!!!'
Yeah, of course. I just don't think anything that perverse is
happening in these cases. Take, for another example,
http://trac.edgewall.org/ticket/5646#comment:3

--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

The Astoria Seminar ==http://www.astoriaseminar.com

Jul 3 '07 #6

on Thu Jun 21 2007, Ben Finney <bignose+hates-spam-AT-benfinney.id.auwrote:
David Abrahams <da**@boost-consulting.comwrites:
>I'm pretty comfortable with Python, but recently I'm constantly
finding mysterious issues with import. For example, looking at

http://genshi.edgewall.org/browser/t...s/transform.py

the examples use the symbol 'HTML' but it's not defined locally, it's
not explicitly imported, and there's no import *. Yet doctest will
test this module and it passes with flying colors. It turns out HTML
is defined in genshi.input. How do I know that? I grepped for it.
How does it become available to this module?

That's a mystery to me too. I can't see by looking at the module where
this 'HTML' name comes from, and as you say there is no import of
'genshi.input'.

Whatever the explanation, it's a violation of one of the strengths of
Python: namespaces. Names that appear in the current namespace (as
happens with 'from foo import *', and as seems to be happening here)
are bad programming style, for exactly the reason that they make the
code more difficult to understand.
Sometimes that's true, but I disagree with it as a general rule. Some
bindings are idiomatic, in widespread use throughout a project, and
would look dumb and make the code too verbose and hard-to-understand
if qualified (especially if fully qualified).

The practice may make it harder to understand when something goes wrong, but
that's a separate issue.

--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

The Astoria Seminar ==http://www.astoriaseminar.com

Jul 3 '07 #7

on Thu Jun 21 2007, Peter Otten <__peter__-AT-web.dewrote:
David Abrahams wrote:
>I'm pretty comfortable with Python, but recently I'm constantly
finding mysterious issues with import. For example, looking at

http://genshi.edgewall.org/browser/t...s/transform.py

the examples use the symbol 'HTML' but it's not defined locally, it's
not explicitly imported, and there's no import *. Yet doctest will
test this module and it passes with flying colors. It turns out HTML
is defined in genshi.input. How do I know that? I grepped for it.
How does it become available to this module?

Explicitly passed, see

http://genshi.edgewall.org/browser/t...s/transform.py
IIRC I ran doctest on the file I cited, not the one you're pointing
at. Is there some new magic doctest feature I should know about?
>Another example: I was recently working on some code that did an
import from inside a class method. That import was failing. I moved
the import to the top of the file (at module scope) and it succeeded.
I'm fairly sure that nobody was monkeying around with sys.path in that
case. Can anyone think of a likely explanation?

Too vague, sorry.
# this will succeed if I do it here
# import foo.bar

class X:
def y():
import foo.bar # but this fails
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

The Astoria Seminar ==http://www.astoriaseminar.com

Jul 3 '07 #8
Yeah, of course. I just don't think anything that perverse is
happening in these cases. Take, for another example,
http://trac.edgewall.org/ticket/5646#comment:3
[that perverse == putting a name into globals()]

When you do gettext.install(...), it will put the name _ into
the __builtins__. Apparently, the trac code relies on that.

Regards,
Martin
Jul 4 '07 #9
David Abrahams wrote:
>
on Thu Jun 21 2007, Peter Otten <__peter__-AT-web.dewrote:
>David Abrahams wrote:
>>I'm pretty comfortable with Python, but recently I'm constantly
finding mysterious issues with import. For example, looking at

http://genshi.edgewall.org/browser/t...s/transform.py

the examples use the symbol 'HTML' but it's not defined locally, it's
not explicitly imported, and there's no import *. Yet doctest will
test this module and it passes with flying colors. It turns out HTML
is defined in genshi.input. How do I know that? I grepped for it.
How does it become available to this module?

Explicitly passed, see

http://genshi.edgewall.org/browser/t...s/transform.py
>
IIRC I ran doctest on the file I cited, not the one you're pointing
at. Is there some new magic doctest feature I should know about?
Had you looked at it you'd seen that the file I pointed to is the driver
script for running the doctests in the file you pointed to -- unfortunately
they have the same name. [...]/tests/transform.py does indeed inject a HTML
object into the globals of [...]/filters/transform.py before it runs the
tests.
>>Another example: I was recently working on some code that did an
import from inside a class method. That import was failing. I moved
the import to the top of the file (at module scope) and it succeeded.
I'm fairly sure that nobody was monkeying around with sys.path in that
case. Can anyone think of a likely explanation?

Too vague, sorry.

# this will succeed if I do it here
# import foo.bar

class X:
def y():
import foo.bar # but this fails
Are threads involved? I vaguely remember a problem with Queue.Queue that
came up on this list some time ago.

Peter

Jul 4 '07 #10

on Wed Jul 04 2007, Peter Otten <__peter__-AT-web.dewrote:
>>Explicitly passed, see

http://genshi.edgewall.org/browser/t...s/transform.py
>>
IIRC I ran doctest on the file I cited, not the one you're pointing
at. Is there some new magic doctest feature I should know about?

Had you looked at it
Gimme a little credit, please! Of course I looked at it.
you'd seen that the file I pointed to is the driver
script for running the doctests in the file you pointed to
Yes, I saw that, but I don't know of any magic feature that causes the
driver script to get loaded when I invoke doctest directly on the file
I pointed to.
-- unfortunately they have the same name. [...]/tests/transform.py
does indeed inject a HTML object into the globals of
[...]/filters/transform.py before it runs the tests.
Yes, I saw that, but as I said...

Anyway, maybe I just got confused and doctest-ed the driver script.
That certainly would explain everything.
>>>Another example: I was recently working on some code that did an
import from inside a class method. That import was failing. I moved
the import to the top of the file (at module scope) and it succeeded.
I'm fairly sure that nobody was monkeying around with sys.path in that
case. Can anyone think of a likely explanation?

Too vague, sorry.

# this will succeed if I do it here
# import foo.bar

class X:
def y():
import foo.bar # but this fails

Are threads involved? I vaguely remember a problem with Queue.Queue that
came up on this list some time ago.
I don't know, honestly. This was probably in Trac somewhere. I don't
know if it's threaded.

--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

The Astoria Seminar ==http://www.astoriaseminar.com

Jul 5 '07 #11
David Abrahams wrote:
>
on Wed Jul 04 2007, Peter Otten <__peter__-AT-web.dewrote:
>>>Explicitly passed, see

http://genshi.edgewall.org/browser/t...s/transform.py
>>>
IIRC I ran doctest on the file I cited, not the one you're pointing
at. Is there some new magic doctest feature I should know about?

Had you looked at it

Gimme a little credit, please! Of course I looked at it.
Sorry.
>you'd seen that the file I pointed to is the driver
script for running the doctests in the file you pointed to

Yes, I saw that, but I don't know of any magic feature that causes the
driver script to get loaded when I invoke doctest directly on the file
I pointed to.
Nor do I.
>-- unfortunately they have the same name. [...]/tests/transform.py
does indeed inject a HTML object into the globals of
[...]/filters/transform.py before it runs the tests.

Yes, I saw that, but as I said...

Anyway, maybe I just got confused and doctest-ed the driver script.
That certainly would explain everything.
Compelling assumption because it does away with the mystery...

Peter
Jul 5 '07 #12

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

Similar topics

0
by: Stian Søiland | last post by:
all examples performed with: Python 2.3+ (#2, Aug 10 2003, 11:09:33) on linux2 (2, 3, 0, 'final', 1) This is a recursive import:
0
by: Vio | last post by:
Hi, I've been trying to embed (statically) wxPy alongside an embedded py interpreter on a linux/gtk box. At one point, for some reason misc.o linking reported "multiple definitions of...
5
by: Steve Holden | last post by:
This is even stranger: it makes it if I import the module a second time: import dbimp as dbimp import sys if __name__ == "__main__": dbimp.install() #k = sys.modules.keys() #k.sort() #for...
7
by: David Poundall | last post by:
importedfiles = {} for f in FileList f2 = f.split('.') # strip the .py, .pyc __import__(f2) s2 = f2+'.main()' # main is the top file in each import c = compile(s2, '', 'eval')...
0
by: Prem Mallappa | last post by:
hi everybody, i was searching for the book Obfuscated C and other mysteries by Don Libes does any one has a softcopy or any pointers thanks in advance prem PS: amazon doen't have shipping...
2
by: dbuchanan | last post by:
Help me understand the behavior of this code. If I run code 1 (below) the datagrid stays populated. === incidental code === Private dataSet1 As CLIP.dsTables private Sub FillDataSet()...
2
by: Jon | last post by:
It appears that (windows) python searches in the current working directory before looking in the local site-packages directory, or that '.' comes first in sys.path? The problem arises when I made...
9
by: rsoh.woodhouse | last post by:
Hi, I'm trying to work out some strange (to me) behaviour that I see when running a python script in two different ways (I've inherited some code that needs to be maintained and integrated with...
176
by: . | last post by:
9/11 Mysteries http://video.google.com/videoplay?docid=-8172271955308136871 http://www.911weknow.com Ignore those who would go to great effort and expend much of heir time in poo-pooing this...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.