Changing Lutz's mydir from Edition 2, Learning Python

W

W. eWatson

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)

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__
============================mydir start===========
# a module that lists the namespaces of other modules

verbose = 1

def listing(module):
if verbose:
print "-"*30
print "name:", module.__name__, "file:", module.__file__
print "-"*30

count = 0
for attr in module.__dict__.keys(): # scan namespace
print "%02d) %s" % (count, attr),
if attr[0:2] == "__":
print "<built-in name>" # skip __file__, etc.
else:
print getattr(module, attr) # same as .__dict__[attr]
count = count+1

if verbose:
print "-"*30
print module.__name__, "has %d names" % count
print "-"*30

if __name__ == "__main__":
import mydir
import string
import numpy

module = raw_input("Enter module name: ")
listing(module)
#listing(mydir) # self-test code: list myself
===================end==========
 
A

Arnaud Delobelle

W. eWatson said:
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: ")

replace this line with:

module_name = raw_input("Enter module name: ")
module = __import__(module_name)
listing(module)

HTH
 
C

Chris Rebert

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
 
W

W. eWatson

Chris said:
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
Very cool. Thanks to both of you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top