Mailing List Archive

A Python problem?
This script below illustrates what appears to be a problem with the
identification of a module in a package.

The example is borrowed from the linbot material.

Colin W.
''' A Python problem? .

'''
def main():
import sys
print 'Start:',dir()
s0= sys.modules
import plugins.rptlib
s1= sys.modules
if s0 == s1:
print 'sys.modules is unchanged'
print 'After attempt to import plugins.rptlib:', dir()
try:
print dir(plugins.rptlib)
except AttributeError:
import sys
print sys.exc_info()
from plugins.rptlib import *
print 'rptlib names now available:', dir()
main()

'''
------------------------ Resulting Display ----------------------------
>>> Start: ['sys']
sys.modules is unchanged
After attempt to import plugins.rptlib: ['plugins', 's0', 's1', 'sys']
(<class exceptions.AttributeError at 76bbc0>, <exceptions.AttributeError
instance at daa0e0>, <traceback object at dab2b0>)
rptlib names now available: [.'Link', 'add_problem', 'check_and_warn', 'config',
'debugio', 'doBotMain', 'doTopMain', 'get_title', 'linbot', 'linkList',
'main_index', 'make_link', 'nav_bar', 'open_file', 'opener', 'os', 'plugins',
'problem_db', 'proxies', 'read_registry', 's0', 's1', 'sort_by_age',
'sort_by_author', 'sort_by_rev_age', 'sort_by_size', 'string', 'stylesheet',
'sys', 'urllib', 'version']

'''
A Python problem? [ In reply to ]
From: "Colin J. Williams" <cjw@connection.com>

This script below illustrates what appears to be a problem with the
identification of a module in a package.

The example is borrowed from the linbot material.

Colin W.
''' A Python problem? .

'''
def main():
import sys
print 'Start:',dir()
s0= sys.modules
import plugins.rptlib
s1= sys.modules
if s0 == s1:
print 'sys.modules is unchanged'
print 'After attempt to import plugins.rptlib:', dir()
try:
print dir(plugins.rptlib)
except AttributeError:
import sys
print sys.exc_info()
from plugins.rptlib import *
print 'rptlib names now available:', dir()
main()

'''
------------------------ Resulting Display ----------------------------
>>> Start: ['sys']
sys.modules is unchanged
After attempt to import plugins.rptlib: ['plugins', 's0', 's1', 'sys']
(<class exceptions.AttributeError at 76bbc0>, <exceptions.AttributeError
instance at daa0e0>, <traceback object at dab2b0>)
rptlib names now available: [.'Link', 'add_problem', 'check_and_warn', 'config',
'debugio', 'doBotMain', 'doTopMain', 'get_title', 'linbot', 'linkList',
'main_index', 'make_link', 'nav_bar', 'open_file', 'opener', 'os', 'plugins',
'problem_db', 'proxies', 'read_registry', 's0', 's1', 'sort_by_age',
'sort_by_author', 'sort_by_rev_age', 'sort_by_size', 'string', 'stylesheet',
'sys', 'urllib', 'version']

'''
A Python problem? [ In reply to ]
Colin J. Williams writes:
> def main():
> import sys
> print 'Start:',dir()
> s0= sys.modules
> import plugins.rptlib
> s1= sys.modules
> if s0 == s1:
> print 'sys.modules is unchanged'

This will *always* print "sys.modules is unchanged"; sys.modules is a
dictionary, which is a mutable type.

If you want to see if sys.modules changed, try this:

s0 = sys.modules.copy()
import stuff
if sys.modules == s0:
print "it's unchanged"
A Python problem? [ In reply to ]
Colin J. Williams wrote:

> This script below illustrates what appears to be a problem with the
> identification of a module in a package.
>
> The example is borrowed from the linbot material.
>
> Colin W.
> ''' A Python problem? .
>
> '''
> def main():
> import sys
> print 'Start:',dir()
> s0= sys.modules
> import plugins.rptlib
> s1= sys.modules
> if s0 == s1:
> print 'sys.modules is unchanged'

Charles already caught that one.

> print 'After attempt to import plugins.rptlib:', dir()
> try:
> print dir(plugins.rptlib)
> except AttributeError:
> import sys
> print sys.exc_info()

Notice that your "import plugins.rptlib" yields something called
"rptlib". This is something done by the plugins package (probably in
it's __init__.py), not normal behavior. Normally, dir() would show
"plugins" and "dir(plugins.rptlib)" would show what you expect.
Unless there's a good reason for doing so, I'd call this a case of
"clever" turning out to be "stupid" <wink>.

> from plugins.rptlib import *
> print 'rptlib names now available:', dir()
> main()
>
> '''
> ------------------------ Resulting Display
> ----------------------------
> >>> Start: ['sys']
> sys.modules is unchanged
> After attempt to import plugins.rptlib: ['plugins', 's0', 's1',
> 'sys'] (<class exceptions.AttributeError at 76bbc0>,
> <exceptions.AttributeError instance at daa0e0>, <traceback object at
> dab2b0>) rptlib names now available: [.'Link', 'add_problem',
> 'check_and_warn', 'config', 'debugio', 'doBotMain', 'doTopMain',
> 'get_title', 'linbot', 'linkList', 'main_index', 'make_link',
> 'nav_bar', 'open_file', 'opener', 'os', 'plugins', 'problem_db',
> 'proxies', 'read_registry', 's0', 's1', 'sort_by_age',
> 'sort_by_author', 'sort_by_rev_age', 'sort_by_size', 'string',
> 'stylesheet', 'sys', 'urllib', 'version']
>
> '''
>
>
> --
> http://www.python.org/mailman/listinfo/python-list

- Gordon