473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 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 1296
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.