473,802 Members | 1,986 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1327
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.testm od(genshi.filte rs.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>So me 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.comw rites:
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.re covery |
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_inqui sition
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'spanish_inquis ition' is not defined
>>globals()['spanish_inquis ition'] = "NOBODY expects the Spanish
Inquisition!!!"
>>spanish_inqui sition
'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.cyb ersource.com.au wrote:
There are ways to bypass the import system. The most obvious would be to
write directly to globals.
>>>spanish_inqu isition
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'spanish_inquis ition' is not defined
>>>globals()['spanish_inquis ition'] = "NOBODY expects the Spanish
Inquisition!!!"
>>>spanish_inqu isition
'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+hate s-spam-AT-benfinney.id.au wrote:
David Abrahams <da**@boost-consulting.comw rites:
>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

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

Similar topics

0
6913
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
2588
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 wxGetFreeMemory()". Since wxGetFreeMemory() seemed effectively unused in gtk, I just commented it out in misc_wrap.cpp (didn't want to patch SWIG to regenerate the wrapper code, so hacked the generated cpp source), and replaced...
5
2483
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 kk in k:
7
1782
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') importedfiles = eval(c) 'importedfiles' should hold an object reference to the main() function within each imported file.
0
360
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 option to India. and the book is not in print, other online bookshops its double its original price..
2
1963
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() Dim DAL As New CLIP.DataAccess
2
2581
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 the mistake of running a test program from the same directory where I built and installed my package. Python uses the package from the current directory, which misses the pyd files, instead of the site-packages package which has them installed. ...
9
2126
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 another lump of code). The sample script is: # Sample script, simply create a new thread and run a # regular expression match in it. import re
176
5022
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 post. See for yourself what really happened in the 3 demolished buildings in the weeks before 9/11. Since 9-11 the American public has shown a "remarkable indifference to being deceived" (George Soros). But this is changing. As Hugo Chavez put it:
0
9699
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
9562
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
10538
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...
0
10305
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9115
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...
1
7598
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4270
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
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.