A Python problem?

Gordon McMillan gmcm at hypernet.com
Wed Jun 30 18:20:22 EDT 1999


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




More information about the Python-list mailing list