Changing Lutz's mydir from Edition 2, Learning Python

Chris Rebert clp2 at rebertia.com
Sun Jan 17 15:34:54 EST 2010


On Sun, Jan 17, 2010 at 12:11 PM, W. eWatson <wolftracks at invalid.com> wrote:
> See Subject. The code is below with a few changes I made at the bottom by
> inserting
>    import string
>    import numpy
>
>    module = raw_input("Enter module name: ")
>    listing(module)

As the error says, strings have no __name__ attribute; from this, one
can infer that listing() expects a module object, not a string which
is the name of a module.

Try instead:
    module = __import__(raw_input("Enter module name: "))
    listing(module)

Cheers,
Chris
--
http://blog.rebertia.com

> I thought I'd see if I could convert this to a program instead, which asks
> the user for the module.
>
> As prompt entry, it was meant to do this, for example:
>
> import math
> import pynum
> mydir.listing(numpy)
>
> As below, it fails with:
>
>
> Is it possible to get this to work?
> Enter module name: numpy
> ------------------------------
> name:
> Traceback (most recent call last):
>  File "C:/Sandia_Meteors/Sentinel_Development/Learn_Python/mydir_pgm.py",
> line 31, in <module>
>    listing(module)
>  File "C:/Sandia_Meteors/Sentinel_Development/Learn_Python/mydir_pgm.py",
> line 8, in listing
>    print "name:", module.__name__, "file:", module.__file__
> AttributeError: 'str' object has no attribute '__name__



More information about the Python-list mailing list